ITeye博客

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

liliang_xf · 2007-12-07 19:28 · Spring · 非技术 · Java · XML · MySQL · Hibernate · Bean · Markdown

vo层

java 代码

  1. package org.show;
  2. /**
  3. * Account generated by MyEclipse - Hibernate Tools
  4. */
  5. @SuppressWarnings("serial")
  6. public class Account  implements java.io.Serializable {
  7. // Fields
  8. private Integer accountid;
  9. private String accountname;
  10. private Integer money;
  11. // Constructors
  12. /** default constructor */
  13. public Account() {
  14. }
  15. /** full constructor */
  16. public Account(String accountname, Integer money) {
  17. this.accountname = accountname;
  18. this.money = money;
  19. }
  20. // Property accessors
  21. public Integer getAccountid() {
  22. return this.accountid;
  23. }
  24. public void setAccountid(Integer accountid) {
  25. this.accountid = accountid;
  26. }
  27. public String getAccountname() {
  28. return this.accountname;
  29. }
  30. public void setAccountname(String accountname) {
  31. this.accountname = accountname;
  32. }
  33. public Integer getMoney() {
  34. return this.money;
  35. }
  36. public void setMoney(Integer money) {
  37. this.money = money;
  38. }
  39. }

DAO层

java 代码

  1. package org.show;
  2. import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
  3. public class AccountDAO extends HibernateDaoSupport {
  4. public void addMoney(Integer accountId,int money){
  5. Account account = (Account)getHibernateTemplate().get(Account.class,accountId);
  6. account.setMoney(account.getMoney() + money);
  7. getHibernateTemplate().saveOrUpdate(account);
  8. }
  9. public void subMoney(Integer accountId,int money){
  10. Account account = (Account)getHibernateTemplate().get(Account.class,accountId);
  11. account.setMoney(account.getMoney() - money);
  12. getHibernateTemplate().saveOrUpdate(account);
  13. }
  14. }

Service层

java 代码

  1. package org.show;
  2. public class AccountService {
  3. private AccountDAO accountDAO;
  4. public void setAccountDAO(AccountDAO _accountDAO){
  5. accountDAO = _accountDAO;
  6. }
  7. public void transfer(Integer fromAccountId, Integer toAccountId,int money){
  8. accountDAO.subMoney(fromAccountId, money);
  9. accountDAO.addMoney(toAccountId, money);
  10. }
  11. }

测试方法

java 代码

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

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)

暂无评论。

评论功能维护中,当前仅展示历史评论。