数据库使用Mysql,数据库脚本:
1create database if not exists `mydb`;
2
3USE `mydb`;
4
5/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
6/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
7
8/*Table structure for table `card` */
9
10DROP TABLE IF EXISTS `card`;
11
12CREATE TABLE `card` (
13 `id` int(11) NOT NULL auto_increment,
14 `name` varchar(50) default NULL,
15 `money` int(11) default NULL,
16 PRIMARY KEY (`id`)
17) ENGINE=InnoDB DEFAULT CHARSET=utf8;
18
19/*Data for the table `card` */
20
21insert into `card`(`id`,`name`,`money`) values (1,'tom',5000),(2,'jack',5000);
创建DAO接口,目的是可以以多种方式实现,比如jdbc和Hibernate两种实现方式:2
3USE `mydb`;
4
5/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
6/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
7
8/*Table structure for table `card` */
9
10DROP TABLE IF EXISTS `card`;
11
12CREATE TABLE `card` (
13 `id` int(11) NOT NULL auto_increment,
14 `name` varchar(50) default NULL,
15 `money` int(11) default NULL,
16 PRIMARY KEY (`id`)
17) ENGINE=InnoDB DEFAULT CHARSET=utf8;
18
19/*Data for the table `card` */
20
21insert into `card`(`id`,`name`,`money`) values (1,'tom',5000),(2,'jack',5000);
1package com.kay.db.dao.Interface;
2
3import java.util.List;
4
5import com.kay.db.po.Card;
6
7public interface CardDAO {
8
9 public void save(Card card);
10 public void delete(Card card);
11 public Card findById(Integer id);
12 public List findAll();
13 public void update(Card card);
14}
15
CardDaoImpl类实现Dao接口,并继承自org.springframework.orm.hibernate3.support.HibernateDaoSupport中的HibernateDaoSupport类,这样可以使用Spring中的HibernateTemplate:2
3import java.util.List;
4
5import com.kay.db.po.Card;
6
7public interface CardDAO {
8
9 public void save(Card card);
10 public void delete(Card card);
11 public Card findById(Integer id);
12 public List findAll();
13 public void update(Card card);
14}
15
1 package com.kay.db.dao.impl;
2
3 import java.util.List;
4
5 import com.kay.db.dao.Interface.CardDAO;
6 import com.kay.db.po.Card;
7 import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
8
9 public class CardDaoImpl extends HibernateDaoSupport implements CardDAO {
10
11 public void delete(Card card) {
12 getHibernateTemplate().delete(card);
13 }
14
15 public List findAll() {
16 String hql = "FROM Card";
17 List list = getHibernateTemplate().find(hql);
18 return list;
19 }
20
21 public Card findById(Integer id) {
22 Card card = (Card) getHibernateTemplate().get(Card.class, id);
23 return card;
24 }
25
26 public void save(Card card) {
27 getHibernateTemplate().save(card);
28
29 }
30
31 public void update(Card card) {
32 getHibernateTemplate().update(card);
33
34 }
35
36 }
37
在CardService中,封装业务实现,这个类继承了CardServiceFace类,CardServiceFace类中有CardDaoImpl类的属性,并生成get和set方法:2
3 import java.util.List;
4
5 import com.kay.db.dao.Interface.CardDAO;
6 import com.kay.db.po.Card;
7 import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
8
9 public class CardDaoImpl extends HibernateDaoSupport implements CardDAO {
10
11 public void delete(Card card) {
12 getHibernateTemplate().delete(card);
13 }
14
15 public List findAll() {
16 String hql = "FROM Card";
17 List list = getHibernateTemplate().find(hql);
18 return list;
19 }
20
21 public Card findById(Integer id) {
22 Card card = (Card) getHibernateTemplate().get(Card.class, id);
23 return card;
24 }
25
26 public void save(Card card) {
27 getHibernateTemplate().save(card);
28
29 }
30
31 public void update(Card card) {
32 getHibernateTemplate().update(card);
33
34 }
35
36 }
37
1 package com.kay.biz.service.interfaces;
2
3 import com.kay.db.dao.impl.CardDaoImpl;
4
5 public class CardServiceFace {
6
7 protected CardDaoImpl dao;
8
9 public CardDaoImpl getDao() {
10 return dao;
11 }
12
13 public void setDao(CardDaoImpl dao) {
14 this.dao = dao;
15 }
16 }
CardService类:2
3 import com.kay.db.dao.impl.CardDaoImpl;
4
5 public class CardServiceFace {
6
7 protected CardDaoImpl dao;
8
9 public CardDaoImpl getDao() {
10 return dao;
11 }
12
13 public void setDao(CardDaoImpl dao) {
14 this.dao = dao;
15 }
16 }
1 package com.kay.biz.service;
2
3 import com.kay.biz.service.interfaces.CardServiceFace;
4
5 import com.kay.db.po.Card;
6
7 public class CardService extends CardServiceFace{
8
9 /**
10 * 转帐方法
11 * @param from 转出帐号
12 * @param to 转入帐号
13 * @param money 交易金额
14 */
15 public void tran(int from,int to ,int money){
16 Card fromcard = dao.findById(from);
17 fromcard.setMoney(fromcard.getMoney() - money);
18 Card tocard = dao.findById(to);
19 tocard.setMoney(tocard.getMoney() + money);
20 }
21
22
23
24 }
Action类:2
3 import com.kay.biz.service.interfaces.CardServiceFace;
4
5 import com.kay.db.po.Card;
6
7 public class CardService extends CardServiceFace{
8
9 /**
10 * 转帐方法
11 * @param from 转出帐号
12 * @param to 转入帐号
13 * @param money 交易金额
14 */
15 public void tran(int from,int to ,int money){
16 Card fromcard = dao.findById(from);
17 fromcard.setMoney(fromcard.getMoney() - money);
18 Card tocard = dao.findById(to);
19 tocard.setMoney(tocard.getMoney() + money);
20 }
21
22
23
24 }
1package com.kay.struts.action;
2
3import javax.servlet.http.HttpServletRequest;
4import javax.servlet.http.HttpServletResponse;
5
6import org.apache.struts.action.Action;
7import org.apache.struts.action.ActionForm;
8import org.apache.struts.action.ActionForward;
9import org.apache.struts.action.ActionMapping;
10import org.springframework.web.struts.ContextLoaderPlugIn;
11
12import com.kay.biz.service.CardService;
13import com.kay.struts.form.TranForm;
14
15
16public class TranAction extends Action {
17 private CardService cs;
18 public ActionForward execute(ActionMapping mapping, ActionForm form,
19 HttpServletRequest request, HttpServletResponse response) {
20
21 TranForm tf = (TranForm)form;
22 cs.tran(tf.getFrom(), tf.getTo(), tf.getMoney());
23
24 return mapping.findForward("ok");
25 }
26 public CardService getCs() {
27 return cs;
28 }
29 public void setCs(CardService cs) {
30 this.cs = cs;
31 }
32}
配置文件:2
3import javax.servlet.http.HttpServletRequest;
4import javax.servlet.http.HttpServletResponse;
5
6import org.apache.struts.action.Action;
7import org.apache.struts.action.ActionForm;
8import org.apache.struts.action.ActionForward;
9import org.apache.struts.action.ActionMapping;
10import org.springframework.web.struts.ContextLoaderPlugIn;
11
12import com.kay.biz.service.CardService;
13import com.kay.struts.form.TranForm;
14
15
16public class TranAction extends Action {
17 private CardService cs;
18 public ActionForward execute(ActionMapping mapping, ActionForm form,
19 HttpServletRequest request, HttpServletResponse response) {
20
21 TranForm tf = (TranForm)form;
22 cs.tran(tf.getFrom(), tf.getTo(), tf.getMoney());
23
24 return mapping.findForward("ok");
25 }
26 public CardService getCs() {
27 return cs;
28 }
29 public void setCs(CardService cs) {
30 this.cs = cs;
31 }
32}
Spring.xml:
1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
3
4 <beans>
5
6
7 <!-- 数据源 -->
8 <bean id="datasource"
9 class="org.apache.commons.dbcp.BasicDataSource">
10 <property name="driverClassName">
11 <value>com.mysql.jdbc.Driver</value>
12 </property>
13 <property name="url">
14 <value>jdbc:mysql://localhost:3306/mydb</value>
15 </property>
16 <property name="username">
17 <value>root</value>
18 </property>
19 <property name="password">
20 <value>root</value>
21 </property>
22 </bean>
23
24 <!-- SessionFactory -->
25 <bean id="sessionFactory"
26 class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
27 <property name="dataSource">
28 <ref bean="datasource" />
29 </property>
30 <property name="configLocation">
31 <value>/WEB-INF/hibernate.cfg.xml</value>
32 </property>
33 <property name="hibernateProperties">
34 <props>
35 <prop key="hibernate.dialect">
36 org.hibernate.dialect.MySQLDialect
37 </prop>
38 </props>
39 </property>
40 </bean>
41
42 <!-- HibernateTemplate -->
43 <bean id="hibernateTemplate"
44 class="org.springframework.orm.hibernate3.HibernateTemplate">
45 <property name="sessionFactory">
46 <ref bean="sessionFactory" />
47 </property>
48 </bean>
49
50 <!-- CardDAO -->
51 <bean id="cardDao" class="com.kay.db.dao.impl.CardDaoImpl">
52 <property name="hibernateTemplate">
53 <ref bean="hibernateTemplate" />
54 </property>
55 </bean>
56
57
58 <!-- CardService -->
59 <bean id="CardService" class="com.kay.biz.service.CardService">
60 <property name="dao">
61 <ref bean="cardDao" />
62 </property>
63 </bean>
64
65 <!-- Spring事物管理器 -->
66 <bean id="transactionManager"
67 class="org.springframework.orm.hibernate3.HibernateTransactionManager">
68 <property name="sessionFactory">
69 <ref bean="sessionFactory" />
70 </property>
71 </bean>
72
73 <!-- Spring 事务切面 -->
74 <bean id="cardServiceTm"
75 class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
76 <!-- 被代理类 -->
77 <property name="target">
78 <ref bean="CardService" />
79 </property>
80 <!-- Spring 事务管理器 -->
81 <property name="transactionManager">
82 <ref bean="transactionManager" />
83 </property>
84 <!-- 事务规则 -->
85 <property name="transactionAttributes">
86 <props>
87 <prop key="*">PROPAGATION_REQUIRED</prop>
88 </props>
89 </property>
90 </bean>
91
92
93 <!-- Action -->
94 <bean name="/tran" class="com.kay.struts.action.TranAction">
95 <property name="cs">
96 <ref bean="cardServiceTm" />
97 </property>
98 </bean>
99 </beans>
100
Struts-congif.xml:2 <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
3
4 <beans>
5
6
7 <!-- 数据源 -->
8 <bean id="datasource"
9 class="org.apache.commons.dbcp.BasicDataSource">
10 <property name="driverClassName">
11 <value>com.mysql.jdbc.Driver</value>
12 </property>
13 <property name="url">
14 <value>jdbc:mysql://localhost:3306/mydb</value>
15 </property>
16 <property name="username">
17 <value>root</value>
18 </property>
19 <property name="password">
20 <value>root</value>
21 </property>
22 </bean>
23
24 <!-- SessionFactory -->
25 <bean id="sessionFactory"
26 class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
27 <property name="dataSource">
28 <ref bean="datasource" />
29 </property>
30 <property name="configLocation">
31 <value>/WEB-INF/hibernate.cfg.xml</value>
32 </property>
33 <property name="hibernateProperties">
34 <props>
35 <prop key="hibernate.dialect">
36 org.hibernate.dialect.MySQLDialect
37 </prop>
38 </props>
39 </property>
40 </bean>
41
42 <!-- HibernateTemplate -->
43 <bean id="hibernateTemplate"
44 class="org.springframework.orm.hibernate3.HibernateTemplate">
45 <property name="sessionFactory">
46 <ref bean="sessionFactory" />
47 </property>
48 </bean>
49
50 <!-- CardDAO -->
51 <bean id="cardDao" class="com.kay.db.dao.impl.CardDaoImpl">
52 <property name="hibernateTemplate">
53 <ref bean="hibernateTemplate" />
54 </property>
55 </bean>
56
57
58 <!-- CardService -->
59 <bean id="CardService" class="com.kay.biz.service.CardService">
60 <property name="dao">
61 <ref bean="cardDao" />
62 </property>
63 </bean>
64
65 <!-- Spring事物管理器 -->
66 <bean id="transactionManager"
67 class="org.springframework.orm.hibernate3.HibernateTransactionManager">
68 <property name="sessionFactory">
69 <ref bean="sessionFactory" />
70 </property>
71 </bean>
72
73 <!-- Spring 事务切面 -->
74 <bean id="cardServiceTm"
75 class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
76 <!-- 被代理类 -->
77 <property name="target">
78 <ref bean="CardService" />
79 </property>
80 <!-- Spring 事务管理器 -->
81 <property name="transactionManager">
82 <ref bean="transactionManager" />
83 </property>
84 <!-- 事务规则 -->
85 <property name="transactionAttributes">
86 <props>
87 <prop key="*">PROPAGATION_REQUIRED</prop>
88 </props>
89 </property>
90 </bean>
91
92
93 <!-- Action -->
94 <bean name="/tran" class="com.kay.struts.action.TranAction">
95 <property name="cs">
96 <ref bean="cardServiceTm" />
97 </property>
98 </bean>
99 </beans>
100
1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
3
4 <struts-config>
5 <data-sources />
6 <form-beans>
7 <form-bean name="tranForm" type="com.kay.struts.form.TranForm" />
8
9 </form-beans>
10
11 <global-exceptions>
12 <exception key="hh" type="java.lang.RuntimeException"></exception>
13 </global-exceptions>
14 <global-forwards />
15 <action-mappings>
16 <action attribute="tranForm" name="tranForm" path="/tran"
17 scope="request" type="com.kay.struts.action.TranAction">
18 <forward name="ok" path="/index.jsp" />
19 </action>
20
21
22 </action-mappings>
23
24 <controller
25 processorClass="org.springframework.web.struts.DelegatingRequestProcessor">
26 </controller>
27
28 <message-resources parameter="com.kay.struts.ApplicationResources" />
29
30 <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
31 <set-property property="contextConfigLocation" value="/WEB-INF/spring.xml" />
32 </plug-in>
33 </struts-config>
34
35
hibernate.cfg.xml:2 <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
3
4 <struts-config>
5 <data-sources />
6 <form-beans>
7 <form-bean name="tranForm" type="com.kay.struts.form.TranForm" />
8
9 </form-beans>
10
11 <global-exceptions>
12 <exception key="hh" type="java.lang.RuntimeException"></exception>
13 </global-exceptions>
14 <global-forwards />
15 <action-mappings>
16 <action attribute="tranForm" name="tranForm" path="/tran"
17 scope="request" type="com.kay.struts.action.TranAction">
18 <forward name="ok" path="/index.jsp" />
19 </action>
20
21
22 </action-mappings>
23
24 <controller
25 processorClass="org.springframework.web.struts.DelegatingRequestProcessor">
26 </controller>
27
28 <message-resources parameter="com.kay.struts.ApplicationResources" />
29
30 <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
31 <set-property property="contextConfigLocation" value="/WEB-INF/spring.xml" />
32 </plug-in>
33 </struts-config>
34
35
1 <?xml version='1.0' encoding='UTF-8'?>
2 <!DOCTYPE hibernate-configuration PUBLIC
3 "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
4 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
5
6 <!-- Generated by MyEclipse Hibernate Tools. -->
7 <hibernate-configuration>
8
9 <session-factory>
10 <property name="connection.username">root</property>
11 <property name="connection.url">jdbc:mysql://localhost:3306/mydb</property>
12 <property name="dialect">
13 org.hibernate.dialect.MySQLDialect
14 </property>
15 <property name="myeclipse.connection.profile">mysql</property>
16 <property name="connection.password">root</property>
17 <property name="connection.driver_class">
18 com.mysql.jdbc.Driver
19 </property>
20 <property name="show_sql">true</property>
21 <mapping resource="com/kay/db/po/Card.hbm.xml" />
22
23 </session-factory>
24
25 </hibernate-configuration>
2 <!DOCTYPE hibernate-configuration PUBLIC
3 "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
4 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
5
6 <!-- Generated by MyEclipse Hibernate Tools. -->
7 <hibernate-configuration>
8
9 <session-factory>
10 <property name="connection.username">root</property>
11 <property name="connection.url">jdbc:mysql://localhost:3306/mydb</property>
12 <property name="dialect">
13 org.hibernate.dialect.MySQLDialect
14 </property>
15 <property name="myeclipse.connection.profile">mysql</property>
16 <property name="connection.password">root</property>
17 <property name="connection.driver_class">
18 com.mysql.jdbc.Driver
19 </property>
20 <property name="show_sql">true</property>
21 <mapping resource="com/kay/db/po/Card.hbm.xml" />
22
23 </session-factory>
24
25 </hibernate-configuration>