声明式事务处理(转账示例)

vo层
java 代码
  1. package org.show;   
  2.   
  3.   
  4.   
  5. /**  
  6.  * Account generated by MyEclipse - Hibernate Tools  
  7.  */  
  8.   
  9. @SuppressWarnings("serial")   
  10. public class Account  implements java.io.Serializable {   
  11.   
  12.   
  13.     // Fields       
  14.   
  15.      private Integer accountid;   
  16.      private String accountname;   
  17.      private Integer money;   
  18.   
  19.   
  20.     // Constructors   
  21.   
  22.     /** default constructor */  
  23.     public Account() {   
  24.     }   
  25.   
  26.        
  27.     /** full constructor */  
  28.     public Account(String accountname, Integer money) {   
  29.         this.accountname = accountname;   
  30.         this.money = money;   
  31.     }   
  32.   
  33.       
  34.     // Property accessors   
  35.   
  36.     public Integer getAccountid() {   
  37.         return this.accountid;   
  38.     }   
  39.        
  40.     public void setAccountid(Integer accountid) {   
  41.         this.accountid = accountid;   
  42.     }   
  43.   
  44.     public String getAccountname() {   
  45.         return this.accountname;   
  46.     }   
  47.        
  48.     public void setAccountname(String accountname) {   
  49.         this.accountname = accountname;   
  50.     }   
  51.   
  52.     public Integer getMoney() {   
  53.         return this.money;   
  54.     }   
  55.        
  56.     public void setMoney(Integer money) {   
  57.         this.money = money;   
  58.     }   
  59.       
  60.   
  61. }  

DAO层

java 代码
  1. package org.show;   
  2.   
  3. import org.springframework.orm.hibernate3.support.HibernateDaoSupport;   
  4.   
  5. public class AccountDAO extends HibernateDaoSupport {   
  6.   
  7.     public void addMoney(Integer accountId,int money){   
  8.   
  9.         Account account = (Account)getHibernateTemplate().get(Account.class,accountId);   
  10.         account.setMoney(account.getMoney() + money);   
  11.         getHibernateTemplate().saveOrUpdate(account);   
  12.     }   
  13.   
  14.     public void subMoney(Integer accountId,int money){   
  15.   
  16.         Account account = (Account)getHibernateTemplate().get(Account.class,accountId);   
  17.         account.setMoney(account.getMoney() - money);   
  18.         getHibernateTemplate().saveOrUpdate(account);   
  19.     }   
  20. }  
Service层
java 代码
  1. package org.show;   
  2.   
  3. public class AccountService {   
  4.   
  5.     private AccountDAO accountDAO;   
  6.     public void setAccountDAO(AccountDAO _accountDAO){   
  7.            
  8.         accountDAO = _accountDAO;   
  9.     }   
  10.     public void transfer(Integer fromAccountId, Integer toAccountId,int money){   
  11.            
  12.         accountDAO.subMoney(fromAccountId, money);   
  13.         accountDAO.addMoney(toAccountId, money);   
  14.     }   
  15. }   

测试方法

java 代码

  1. package org.show;   
  2.   
  3. import org.springframework.context.ApplicationContext;   
  4. import org.springframework.context.support.FileSystemXmlApplicationContext;   
  5.   
  6. public class Main {   
  7.   
  8.     /**  
  9.      * @param args  
  10.      */  
  11.     public static void main(String[] args) {   
  12.            
  13.         ApplicationContext act = new FileSystemXmlApplicationContext("src/applicationContext.xml");   
  14.         AccountService accountService = (AccountService)act.getBean("accountService");   
  15.         try{   
  16.                
  17.             accountService.transfer(121);   
  18.                
  19.         }catch(Exception e){   
  20.                
  21.             System.out.println("转帐失败!");   
  22.         }   
  23.     }   
  24.   
  25. }   

Account.hbm.xml映射文件

xml 代码
xml version="1.0"?>   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">      <hibernate-mapping>       <class name="org.show.Account" table="account"  >           <id name="accountid" type="java.lang.Integer">               <column name="accountid" />               <generator class="native" />           id>           <property name="accountname" type="java.lang.String">               <column name="accountname" length="20" not-null="true" />           property>           <property name="money" type="java.lang.Integer">               <column name="money" not-null="true" />           property>       class>   hibernate-mapping>  

 

xml 代码
xml version="1.0" encoding="UTF-8"?>   >      <beans>          <bean id="dataSource"           class="org.apache.commons.dbcp.BasicDataSource">                      <property name="driverClassName">               <value>com.mysql.jdbc.Drivervalue>           property>                      <property name="url">               <value>jdbc:mysql://localhost:3306/thjvalue>           property>                      <property name="username">               <value>rootvalue>           property>                      <property name="password">               <value>1234value>           property>       bean>              <bean id="sessionFactory"           class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">                      <property name="dataSource">               <ref bean="dataSource" />           property>           <property name="hibernateProperties">               <props>                   <prop key="hibernate.dialect">                       org.hibernate.dialect.MySQLDialect                    prop>                   <prop key="hibernate.show_sql">trueprop>               props>           property>           <property name="mappingResources">               <list>                   <value>org/show/Account.hbm.xmlvalue>               list>           property>       bean>              <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">                  <property name="sessionFactory" ref="sessionFactory">property>       bean>          <bean id="accountDAO" class="org.show.AccountDAO">           <property name="sessionFactory" ref="sessionFactory">property>       bean>   Spring配置文件            <bean id="accountService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">                      <property name="transactionManager" ref="transactionManager">property>                      <property name="target">                              <bean class="org.show.AccountService">                   <property name="accountDAO" ref="accountDAO">property>               bean>           property>                      <property name="transactionAttributes">               <props>                                      <prop key="transfer">PROPAGATION_REQUIREDprop>               props>           property>       bean>   beans>  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值