转账案例-Spring的手动管理事务【了解】讲解 -需要配置事务模板
dao
package com.mu.transaction.dao;
public interface IAcountDao {
/**
* 扣钱
* @param outer
* @param money
*/
void out(String outer,Integer money);
/**
* 加钱
* @param inner
* @param money
*/
void in(String inner,Integer money);
}
daoImpl
package com.mu.transaction.dao.impl;
import com.mu.transaction.dao.IAcountDao;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
/**
* Speing-事务
*/
public class IAcountDaoImpl extends JdbcDaoSupport implements IAcountDao {
@Override
public void out(String outer, Integer money) {
getJdbcTemplate().update("UPDATE acount set money = money - ? where username = ?", money, outer);
}
@Override
public void in(String inner, Integer money) {
getJdbcTemplate().update("UPDATE acount set money = money + ? where username = ?", money, inner);
}
}
service
package com.mu.transaction.service;
public interface IAountService {
/**
* 转账
* @param outer 扣钱
* @param inner 进钱
* @param money 钱数
*/
void transfer(String outer,String inner,Integer money);
}
serviceImpl
package com.mu.transaction.service.impl;
import com.mu.transaction.dao.IAcountDao;
import com.mu.transaction.service.IAountService;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;
public class IAountServiceImpl implements IAountService {
private IAcountDao iAcountDao;
/**
* spring 配置事务模板
*/
private TransactionTemplate transactionTemplate;
public void setTransactionTemplate(TransactionTemplate transactionTemplate) {
this.transactionTemplate = transactionTemplate;
}
public void setiAcountDao(IAcountDao iAcountDao) {
this.iAcountDao = iAcountDao;
}
@Override
public void transfer(String outer, String inner, Integer money) {
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus transactionStatus) {
iAcountDao.out(outer, money);
int a = 10 / 0;
iAcountDao.in(inner, money);
}
});
}
}
配置文件
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
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">
<!--
dbcp数据源
<bean id="datesource" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql:///studb"></property>
<property name="username" value="root"></property>
<property name="password" value="106854"></property>
</bean>-->
<!--
使用配置文件
-->
<context:property-placeholder location="classpath:druid.properties"></context:property-placeholder>
<!--c3p0数据源-->
<bean id="datesource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${driverClassName}"></property>
<property name="jdbcUrl" value="${url}"></property>
<property name="user" value="${username1}"></property>
<property name="password" value="${password}"></property>
</bean>
<!--配置事务模板-->
<bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
<property name="transactionManager" ref="txManger"></property>
</bean>
<!--配置事务管理器-->
<bean id="txManger" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="datesource"></property>
</bean>
<!---->
<bean id="iAcountDao" class="com.mu.transaction.dao.impl.IAcountDaoImpl">
<property name="dataSource" ref="datesource"></property>
</bean>
<bean id="iAountService" class="com.mu.transaction.service.impl.IAountServiceImpl">
<property name="iAcountDao" ref="iAcountDao"></property>
<!--配置事务模板-->
<property name="transactionTemplate" ref="transactionTemplate"></property>
</bean>
</beans>
数据库相关-druid.properties
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql:///studb
username1=root
password=106854
initialSize=5
maxActive=10
maxWait=3000
测试
package com.mu.transaction.test;
import com.mu.transaction.service.IAountService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test1 {
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("beans18.xml");
IAountService iAountService = (IAountService) context.getBean("iAountService");
iAountService.transfer("zhangsan","lisi",20);
}
}
数据库表