title: spring中基于annotation的事务管理转账案例
date: 2018-3-1 15:10:27
categories: Spring
tags: Spring
xl_echo编辑整理,欢迎转载,转载请声明文章来源。更多案例、资料请联系QQ:1280023003
spring中基于annotation的事务管理转账案例
业务层代码
package com.echo.account;
public interface AccountService {
public void account(String outname, String inname, double money);
}
package com.echo.account;
public class AccountServiceImp implements AccountService {
private AccountDao accountDao;
public AccountDao getAccountDao() {
return accountDao;
}
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
@Transactional
@Override
public void account(String outname, String inname, double money) {
//先转出
accountDao.outaccount(outname, money);
int a = 10/0;
//再转入
accountDao.inaccount(inname, money);
}
}
dao层代码
package com.echo.account;
public interface AccountDao {
public void outaccount(String outname, double money);
public void inaccount(String inname, double money);
}
package com.echo.account;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
public class AccountDaoImp extends JdbcDaoSupport implements AccountDao {
@Override
//转出
public void outaccount(String outname, double money) {
this.getJdbcTemplate().update("update springaccount set money = money-? where name = ?", money, outname);
}
@Override
//转入
public void inaccount(String inname, double money) {
this.getJdbcTemplate().update("update springaccount set money = money+? where name = ?", money, inname);
}
}
测试类代码
package com.echo.account;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class AccountTest {
@Autowired
private AccountService accountService;
@Test
public void Test(){
accountService.account("tom", "echo", 500);
}
}
核心配置文件
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
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/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- bean definitions here -->
<!-- spring内置连接池 -->
<bean id="driverManagerDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/myxlsorry"></property>
<property name="username" value="root"></property>
<property name="password" value="20263800mq"></property>
</bean>
<!-- 创建c3p0连接池 -->
<!-- <bean id="c3p0DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/myxlsorry"></property>
<property name="user" value="root"></property>
<property name="password" value="20263800mq"></property>
</bean> -->
<!-- 引入外部属性文件的值db.properties -->
<context:property-placeholder location="classpath:db.properties"/>
<bean id="c3p0DataSource" 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="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="c3p0DataSource"></property>
</bean>
<!-- 配置dao -->
<bean id="accountDao" class="com.echo.account.AccountDaoImp">
<!-- 当注入DataSource后,底层会自动创建jdbcTemplate,不需要再去创建 -->
<property name="dataSource" ref="c3p0DataSource" />
</bean>
<!-- 配置service -->
<bean id="accountService" class="com.echo.account.AccountServiceImp">
<property name="accountDao" ref="accountDao"></property>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="c3p0DataSource"></property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>