1)dao层
package cn.spring.transaction.dao; public interface MoneyDao { //加钱的方法 void addMoney(double money); //减钱的方法 void subMoney(double money); }
(2)daoImpl层
package cn.spring.transaction.dao.impl; import cn.spring.transaction.dao.MoneyDao; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.support.JdbcDaoSupport; public class MoneyDaoImpl extends JdbcDaoSupport implements MoneyDao { JdbcTemplate jdbcTemplate=this.getJdbcTemplate(); @Override public void addMoney(double money) { String sql="update accounts set balance=balance+? where accountname='小明'"; this.getJdbcTemplate().update(sql,money); } @Override public void subMoney(double money) { String sql="update accounts set balance=balance-? where accountname='小李'"; this.getJdbcTemplate().update(sql,money); } }
(3)service层
package cn.spring.transaction.service; public interface MoneyService { //转账的方法 void transferMoney(double money); }
(4)serviceImpl层
package cn.spring.transaction.service.impl; import cn.spring.transaction.dao.MoneyDao; import cn.spring.transaction.service.MoneyService; public class MoneyServiceImpl implements MoneyService { MoneyDao moneyDao; @Override public void transferMoney(double money) { //金额先减后加 moneyDao.subMoney(money); moneyDao.addMoney(money); } public MoneyDao getMoneyDao() { return moneyDao; } public void setMoneyDao(MoneyDao moneyDao) { this.moneyDao = moneyDao; } }
(5)applicationContext.xml配置文件
<?xml version="1.0" encoding="UTF-8" ?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--1.配置数据源 spring内置的数据源--> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${jdbc.driver}"></property> <property name="url" value="${jdbc.url}"></property> <property name="username" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </bean> <!--2.引入属性文件--> <context:property-placeholder location="jdbc.properties"></context:property-placeholder> <!--3.构建jdbcTemplate--> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean> <!--4.dao--> <bean id="moneyDaoImpl" class="cn.spring.transaction.dao.impl.MoneyDaoImpl"> <!--为jdbcTemplate配置数据源--> <property name="jdbcTemplate" ref="jdbcTemplate"></property> </bean> <!--5.service--> <bean id="moneyServiceImpl" class="cn.spring.transaction.service.impl.MoneyServiceImpl"> <property name="moneyDao" ref="moneyDaoImpl"></property> </bean> <!--扫描注解:包扫描器--> <context:component-scan base-package="cn.spring"></context:component-scan> <!--开启AOP注解支持--> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> </beans>
(6)测试类
package cn.spring; import cn.spring.transaction.service.MoneyService; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TransactionTest { public static void main(String[] args) { ApplicationContext context=new ClassPathXmlApplicationContext("app01.xml"); MoneyService moneyService = context.getBean(MoneyService.class); //实现转账 moneyService.transferMoney(1000); } }
(7)效果展示
转账前 转账后
1、使用代理工厂实现事务
<!--1.配置数据源 spring内置的数据源--> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${jdbc.driver}"></property> <property name="url" value="${jdbc.url}"></property> <property name="username" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </bean> <!--2.引入属性文件--> <context:property-placeholder location="jdbc.properties"></context:property-placeholder> <!--3.构建jdbcTemplate--> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean> <!--配置Spring的事务管理器,默认在发生异常的情况下回滚,否则提交--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!--第一种事务--> <!--配置Spring事务的代理工厂--> <bean id="transactionFactory" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <!--事务管理器--> <property name="transactionManager" ref="transactionManager"></property> <!--目标对象--> <property name="target" ref="moneyServiceImpl"></property> <!--设置方法--> <property name="transactionAttributes"> <props> <!--方法对应的隔离级别和传播行为--> <prop key="transferMoney">ISOLATION_READ_COMMITTED</prop> </props> </property> </bean> <!--扫描注解:包扫描器--> <context:component-scan base-package="cn.spring"></context:component-scan> <!--开启AOP注解支持--> <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
2、使用AOP实现事务
<!--1.配置数据源 spring内置的数据源--> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${jdbc.driver}"></property> <property name="url" value="${jdbc.url}"></property> <property name="username" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </bean> <!--2.引入属性文件--> <context:property-placeholder location="jdbc.properties"></context:property-placeholder> <!--3.构建jdbcTemplate--> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean> <!--配置Spring的事务管理器,默认在发生异常的情况下回滚,否则提交--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!--第二种事务AOP事务--> <tx:advice id="transactionAdvice" transaction-manager="transactionManager" > <tx:attributes> <tx:method name="transferMoney" propagation="REQUIRED" isolation="READ_COMMITTED"></tx:method> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut id="pointcut" expression="execution(* *..service.impl.*.*(..))"/> <aop:advisor advice-ref="transactionAdvice" pointcut-ref="pointcut"></aop:advisor> </aop:config> <!--扫描注解:包扫描器--> <context:component-scan base-package="cn.spring"></context:component-scan> <!--开启AOP注解支持--> <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
3、使用注解实现事务
<!--第三种事务注解事务--> <tx:annotation-driven></tx:annotation-driven>