1、表結構及數據
2、使用的jar包
3、service、Dao層接口與實現類:
Dao接口:
//轉賬案例持久層接口 public interface AccountDao { /** * @param out :轉出賬號 * @param money :轉賬金額 */ public void outMoney(String out,Double money); /** * @param in :轉入賬號 * @param money :轉賬金額 */ public void inMoney(String in,Double money); }
Dao實現類:
//轉賬案例持久層實現類 public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao { /** * @param out :轉出賬號 * @param money :轉賬金額 */ @Override public void outMoney(String out, Double money) { String sql = "update account set money = money - ? where name = ?"; this.getJdbcTemplate().update(sql,money,out); } /** * @param in :轉入賬號 * @param money :轉賬金額 */ @Override public void inMoney(String in, Double money) { String sql = "update account set money = money + ? where name = ?"; this.getJdbcTemplate().update(sql,money,in); } }
service接口:
//轉賬案例業務層接口 public interface AccountService { /** * @param out :轉出賬號 * @param in :轉入賬號 * @param money :轉賬金額 */ public void transfer(String out,String in,Double money); }
service實現類:
使用@Transactional事物注解,根據自身需求可以使用不同的注解屬性propagation、isolation、readOnly、rollbackFor、noRollbackFor
//轉賬案例業務層實現類 @Transactional(propagation=Propagation.REQUIRED)//使用注解實現事務管理,根據需求使用不同的注解屬性 public class AccountServiceImpl implements AccountService { //注入轉賬的Dao @Resource private AccountDao accountDao; /** * @param out :轉出賬號 * @param in :轉入賬號 * @param money :轉賬金額 */ @Override public void transfer( String out, String in, Double money) { //把業務操作放入內部類中----在一個事物里面(同成功,同失敗) accountDao.outMoney(out, money); int i = 1/0; //異常測試 accountDao.inMoney(in, money); } }
4、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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" 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-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> <!-- 引入外部屬性文件 --> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- 配置c3p0連接池 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driverClass}"></property> <property name="jdbcUrl" value="${jdbc.url}"></property> <property name="user" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </bean> <!-- 配置業務層類 --> <bean id="accountService" class="cn.xl.spring.demo4.AccountServiceImpl"> </bean> <!-- 配置持久層類 --> <bean id="accountDao" class="cn.xl.spring.demo4.AccountDaoImpl"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 事物管理器配置 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 開啟注解事物 --> <tx:annotation-driven transaction-manager="transactionManager"/> </beans>
5、測試類:
//spring聲明式事物管理的方式二的測試類:基於AspectJ的XML配置 @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext4.xml") public class SpringDemo4 { //需注入業務層代理對象 @Resource(name="accountService") private AccountService accountService; @Test public void demo4(){ accountService.transfer("aaa", "bbb", 200d); } }