Spring 声明式事务管理----基于AspectJ的XML方式

2019-04-14 17:06发布

 使用案例代码:
#数据模型层 package com.sunline.entity; /** * Account entity. @author MyEclipse Persistence Tools */ public class Account implements java.io.Serializable { // Fields private Integer id; private String name; private Double money; // Constructors /** default constructor */ public Account() { } /** minimal constructor */ public Account(String name) { this.name = name; } /** full constructor */ public Account(String name, Double money) { this.name = name; this.money = money; } // Property accessors public Integer getId() { return this.id; } public void setId(Integer id) { this.id = id; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } public Double getMoney() { return this.money; } public void setMoney(Double money) { this.money = money; } }


#配置文件 applicationContext.xml org.hibernate.dialect.MySQLDialect org.hibernate.dialect.MySQLDialect true true com/sunline/entity/Account.hbm.xml
#数据访问层 AccountDao.java
package com.sunline.dao; import javax.annotation.Resource; import org.hibernate.SessionFactory; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import org.springframework.stereotype.Repository; @Repository(value="accountDao") public class AccountDao extends HibernateDaoSupport { /* * 使用注解必须添加以下方式 */ @Resource public void setSessionFacotry(SessionFactory sessionFacotry) { super.setSessionFactory(sessionFacotry); } /* * @param out :转出账号 * @param money :转账金额 */ public void outMoney(String out, double money){ String hsql ="update Account set money = money-? where name = ?"; System.out.println("成功转出金额!"); this.getHibernateTemplate().bulkUpdate(hsql, new Object[]{money,out}); } /* * @param in :转入账号 * @param money :转账金额 */ public void inMoney(String in, Double money) { String hsql = "update Account set money = money+? where name = ?"; System.out.println("成功转入金额!"); this.getHibernateTemplate().bulkUpdate(hsql, new Object[]{money,in}); } }
#业务逻辑层 AccountBiz3.java
package com.sunline.biz; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Service; import com.sunline.dao.AccountDao; @Service(value="accountBizThree") public class AccountBiz3 { /* * 转账 */ @Autowired @Qualifier("accountDao") //使用@Qualifier注解来说明使用哪一个实现类 AccountDao accountDao; /* * @param out :转出账号 * @param in :转入账号 * @param money :转账金额 */ public void transfer(String out, String in, Double money) { accountDao.outMoney(out, money); int i = 1/0; accountDao.inMoney(in, money); } }
#测试类
package com.sunline.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.sunline.biz.AccountBiz3; public class TestThree { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); AccountBiz3 accountBiz3 = (AccountBiz3) ctx.getBean("accountBizThree"); accountBiz3.transfer("海哥", "杨旭", 200d); } }