这几天感冒 带病强更 有点水分 后期补上
aop事务管理看似简单 搞了我好久 看了好多博客 今天终于矸出来了
首先写一个bean.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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.lwh"></context:component-scan>
<bean id="accountService" class="com.lwh.service.impl.AccountServiceImpl">
<property name="accountDao" ref="accountDao"></property>
</bean>
<bean id="accountDao" class="com.lwh.dao.impl.AccountDaoImpl">
<property name="runner" ref="runner"></property>
<property name="connectionUtil" ref="connectionUtil"></property>
</bean>
<bean id="runner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype">
<constructor-arg name="ds" ref="dataSource"></constructor-arg>
</bean>
<bean id="connectionUtil" class="com.lwh.utils.ConnectionUtil">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql:///spring"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
</bean>
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<bean id="txManager" class="com.lwh.utils.TransactionManager">
<property name="connectionUtil" ref="connectionUtil"></property>
</bean>
<aop:config>
<aop:pointcut id="pt1" expression="execution(* com.lwh.service.*.*(..))"/>
<aop:aspect id="txAdvice" ref="txManager">
<aop:before method="beginTransaction" pointcut-ref="pt1"></aop:before>
<aop:after-returning method="commit" pointcut-ref="pt1"></aop:after-returning>
<aop:after-throwing method="rollback" pointcut-ref="pt1"></aop:after-throwing>
<aop:after method="release" pointcut-ref="pt1"></aop:after>
</aop:aspect>
</aop:config>
<!-- <tx:jta-transaction-manager></tx:jta-transaction-manager>-->
</beans>
然后写一个ConenctionUtil和transactionUtil工具类
package com.lwh.utils;
import org.springframework.beans.factory.annotation.Autowired;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
public class ConnectionUtil {
private ThreadLocal<Connection> tl=new ThreadLocal<Connection>();
// @Autowired
private DataSource dataSource;
public void setDataSource(DataSource dataSource){
this.dataSource=dataSource;
}
public Connection getThreadConnection(){
Connection conn=tl.get();
try{
if (conn==null){
conn=dataSource.getConnection();
tl.set(conn);
}
}
catch (SQLException throwables) {
throwables.printStackTrace();
}
return conn;
}
public void removeConnection(){
tl.remove();
}
}
package com.lwh.utils;
import org.aspectj.lang.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.sql.SQLException;
public class TransactionManager {
private ConnectionUtil connectionUtil;
private void pt1(){}
public void setConnectionUtil(ConnectionUtil connectionUtil) {
this.connectionUtil = connectionUtil;
}
/*开启事务*/
public void beginTransaction(){
try{
connectionUtil.getThreadConnection().setAutoCommit(false);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
/*提交事务*/
public void commit() throws SQLException {
connectionUtil.getThreadConnection().commit();
}
/*回滚事务*/
public void rollback() throws SQLException {
connectionUtil.getThreadConnection().rollback();
}
/*释放连接*/
public void release() throws SQLException {
connectionUtil.getThreadConnection().close();
connectionUtil.removeConnection();
}
}
然后写一个accountDaoimpl和AccountServiceImpl
package com.lwh.dao.impl;
import com.lwh.dao.AccountDao;
import com.lwh.pojo.Account;
import com.lwh.utils.ConnectionUtil;
import com.mchange.v2.c3p0.DataSources;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import java.sql.SQLException;
import java.util.List;
//@Repository("accountDao")
public class AccountDaoImpl implements AccountDao
{
//@Autowired
private ConnectionUtil connectionUtil;
public void setConnectionUtil(ConnectionUtil connectionUtil) {
this.connectionUtil = connectionUtil;
}
//@Autowired
private QueryRunner runner;
public void setRunner(QueryRunner runner) {
this.runner = runner;
}
@Override
public Account findByName(String name) throws SQLException {
List<Account> accounts= runner.query("select * from account1 where username=?",new BeanListHandler<Account>(Account.class),name);
if (accounts==null||accounts.size()==0 ){
return null;
}
if (accounts.size()>1){
throw new RuntimeException("结果不唯一");
}
else{
return accounts.get(0);
}
}
@Override
public void updateAccount(Account account) throws SQLException {
runner.update("update account1 set username=?,balance=? where id=?", account.getUsername(), account.getBalance(), account.getId());
}
@Override
public void transfer(String sourceName, String targetName, double money) throws SQLException {
Account source=findByName(sourceName);
Account target=findByName(targetName);
//int i=1/0;
source.setBalance(source.getBalance()-money);
target.setBalance(target.getBalance()+money);
updateAccount(source);
updateAccount(target);
System.out.println("转账成功");
}
}
package com.lwh.service.impl;
import com.lwh.dao.AccountDao;
import com.lwh.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import java.sql.SQLException;
//@Service("accountService")
public class AccountServiceImpl implements AccountService {
//@Autowired
AccountDao accountDao;
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
@Override
public void transfer(String sourceName, String targetName, double money) throws SQLException {
accountDao.transfer(sourceName,targetName,100);
}
}
最后测试
public class Test2 {
public static void main(String[] args) throws SQLException {
ApplicationContext ap=new ClassPathXmlApplicationContext("bean.xml");
AccountService accountService = (AccountService) ap.getBean("accountService");
accountService.transfer("root","admin",100);
}
}