Spring事务的学习(一)

最近跟着视频学习了Spring中事务的知识。谈一谈自己的理解,接下来涉及到的代码等资料均来自B站上的视频资料,只是自己在敲得过程中有一些自己的理解。有误的地方欢迎及时指出。

  • 事务最常见的应用场景就是银行转账的例子。当A账户向B账户转账过程中,代码出现了异常,导致A扣钱成功,B未接收到金额。出现这种场景的原因就是没有将整个转账的过程用同一个事务进行统一管理。
  • 首先,先准备出账户的实体类Account:
package com.myself.domain;

import java.io.Serializable;

public class Account implements Serializable {
    private Integer id;
    private String name;
    private Float money;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Float getMoney() {
        return money;
    }

    public void setMoney(Float money) {
        this.money = money;
    }

    @Override
    public String toString() {
        return "Account{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", money=" + money +
                '}';
    }
}

Account实体类中包含了账户姓名,账户余额两个属性和一个数据可自增长的id。接下来咱们分别从持久层、业务层和测试类一步步分析。
创建完实体类之后,咱们开始对数据库中的account进行CRUD操作。首先创建dao层的接口IAccountDao:

package com.myself.dao;

import com.myself.domain.Account;

import java.util.List;

public interface IAccountDao {
    List<Account> findAllAccount();
    Account findById(Integer id);
    Account findByName(String name);
    void update(Account account);
    void delete(Integer id);
    void save(Account account);
}

主要定义了一些CRUD的接口方法,并且实现了该接口AccountDaoImpl:

package com.myself.dao.impl;

import com.myself.dao.IAccountDao;
import com.myself.domain.Account;
import com.myself.utils.ConnectionUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class AccountDaoImpl implements IAccountDao {
    private QueryRunner runner;

    public void setRunner(QueryRunner runner) {
        this.runner = runner;
    }

    private ConnectionUtils connectionUtils;

    public void setConnectionUtils(ConnectionUtils connectionUtils) {
        this.connectionUtils = connectionUtils;
    }

    @Override
    public List<Account> findAllAccount() {
        try {
            return runner.query(connectionUtils.getThreadConnection(),"select * from account",new BeanListHandler<Account>(Account.class));
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Account findById(Integer id) {
        try {
            return runner.query(connectionUtils.getThreadConnection(),"select * from account where id = ?",new BeanHandler<Account>(Account.class),id);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Account findByName(String name) {
        try {
            return runner.query(connectionUtils.getThreadConnection(),"select * from account where name = ?",new BeanHandler<Account>(Account.class),name);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void update(Account account) {
        try {
            runner.update(connectionUtils.getThreadConnection(),"update account set name = ? ,money = ?",account.getName(),account.getMoney());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void delete(Integer id) {
        try {
            runner.update(connectionUtils.getThreadConnection(),"delete from account where id = ?",id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void save(Account account) {
        try {
            runner.update(connectionUtils.getThreadConnection(),"insert into account(name,money) values(?,?)",account.getName(),account.getMoney());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

AccountDaoImpl 中出现了一个QueryRuner类和ConnectionUtils工具类。现在开始分析:首先QueryRuner是DbUtils第三方Jar包提供的封装CRUD操作的模板类吧,常用的两个方法是query和update,通过QueryRuner类可以实现对account表的CRUD操作。但是,要想对数据库操作,得需要dataSource数据源对象。QueryRuner类的源码中,有个构造方法可以传入数据源对象:

public QueryRunner(DataSource ds) {
        super(ds);
    }

至此,我们Spring的bean.xml文件暂时需要配置如下一些信息:

<!--配置AccountDaoImpl-->
    <bean id="accountDao" class="com.myself.dao.impl.AccountDaoImpl">
        <property name="runner" ref="runner"></property>
        <property name="connectionUtils" ref="connectionUtils"></property>
    </bean>

<!--配置QueryRunner-->
    <bean id="runner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype">
        <constructor-arg name="ds" ref="dataSource"></constructor-arg>
    </bean>

    <!--配置数据源对象-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="dataSourceName" value="com.mysql.jdbc.Driver"></property>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/mystudy"></property>
        <property name="user" value="root"></property>
        <property name="password" value="root"></property>
    </bean>

暂时先不要理会connectionUtils类,需要关注的是:1、数据源对象,我这里用的是c3p0连接池;2、QueryRunner类注入数据源是通过构造方法注入的;3、AccountDaoImpl类中注入了QueryRunner和ConnectionUtils;4、QueryRunner设置成多例的,每次使用QueryRunner就会获得一个新的连接。

  • 接下来,需要编写业务层的接口和实现类:
package com.myself.service;

import com.myself.domain.Account;

import java.util.List;

public interface IAccountService {
    List<Account> findAllAccount();
    Account findById(Integer id);
    Account findByName(String name);
    void update(Account account);
    void delete(Integer id);
    void save(Account account);
    void transfer(String sourceName,String targetName,Float money);
}

package com.myself.service.impl;

import com.myself.dao.IAccountDao;
import com.myself.domain.Account;
import com.myself.service.IAccountService;

import java.util.List;

public class AccountServiceImpl implements IAccountService {
	    private IAccountDao accountDao;
	    public void setAccountDao(IAccountDao accountDao) {
	        this.accountDao = accountDao;
	    }
	
	    @Override
	    public List<Account> findAllAccount() {
	        return accountDao.findAllAccount();
	    }
	
	    @Override
	    public Account findById(Integer id) {
	        return accountDao.findById(id);
	    }
	
	    @Override
	    public Account findByName(String name) {
	        return accountDao.findByName(name);
	    }
	
	    @Override
	    public void update(Account account) {
	        accountDao.update(account);
	    }
	
	    @Override
	    public void delete(Integer id) {
	        accountDao.delete(id);
	    }
	
	    @Override
	    public void save(Account account) {
	        accountDao.save(account);
	    }

	    @Override
	    public void transfer(String sourceName, String targetName, Float money) {
	        Account source = accountDao.findByName(sourceName);
	        Account target = accountDao.findByName(targetName);
	        source.setMoney(source.getMoney()-money);
	        target.setMoney(target.getMoney()+money);
	        accountDao.update(source);
	        int i = 1/0;
	        accountDao.update(target);
	    }
    }

业务层是对持久层的每个方法进行了调用,其中特别加入了一个transfer(),用于一会演示事务。
业务层添加后,更新bean.xml文件:

<!--配置AccountService-->
    <bean id="accountService" class="com.myself.service.impl.AccountServiceImpl">
        <property name="accountDao" ref="accountDao"></property>
    </bean>

业务层中调用了持久层的方法,所以需要注入accountDao。

到此,捋一捋思路:首先,创建Account实体类,然后编写dao层的CRUD方法,在AccountDaoImpl实现类中,需要引入一个叫QueryRunner的类,进行对数据库的CRUD方法的执行操作。在QueryRunner类中,通过构造方法注入了数据源(c3p0),这样就与数据库建立了连接。这里需要注意的是,在QueryRunner中的query方法中,有BeanListHandle和一个BeanHandle两个参数,这是Spring提供的对查询数据库的结果集进行封装的模板类。然后业务层的实现类调用了持久层的每个方法。

  • 接下来,咱们引入事务。

业务层提供了一个transfer(),模拟了一个转账的操作,三个参数分别是转出账户的姓名,转入账户的姓名,转账金额。首先查出转出账户和转入账户,然后分别更新两个账户的金额,transfer方法中,一共调用了4次dao层的方法。也就是4个独立的事务。这时,在A账户转出金额后,也就是 accountDao.update(source);执行后,程序因为异常出现中断,但是此时accountDao.update(source)的事务已经提交,但是accountDao.update(target);没有执行,所以A账户少了钱,B账户没有增加。此时就需要将这整个过程用同一个事务进行管理。
此时,我们的事务是挂在持久层的,需要将事务迁移到业务层。根据学习资料中的思路,首先我们将连接绑定在线程上,为了在同一线程的任意地方可以使用同一连接,方便实现事务,这里就用ConnectionUtils实现线程绑定:

package com.itheima.utils;

import javax.sql.DataSource;
import java.sql.Connection;

/**
 * 连接的工具类,它用于从数据源中获取一个连接,并且实现和线程的绑定
 */
public class ConnectionUtils {

    private ThreadLocal<Connection> tl = new ThreadLocal<Connection>();

    private DataSource dataSource;

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    /**
     * 获取当前线程上的连接
     * @return
     */
    public Connection getThreadConnection() {
        try{
            //1.先从ThreadLocal上获取
            Connection conn = tl.get();
            //2.判断当前线程上是否有连接
            if (conn == null) {
                //3.从数据源中获取一个连接,并且存入ThreadLocal中
                conn = dataSource.getConnection();
                tl.set(conn);
            }
            //4.返回当前线程上的连接
            return conn;
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    /**
     * 把连接和线程解绑
     */
    public void removeConnection(){
        tl.remove();
    }
}

ConnectionUtils类中,使用了ThreadLocal将连接绑定在线程上。这里,按我的理解就是将数据源连接对象从QueryRunner对象中解耦出来,注入到ConnectionUtils中,此时,QueryRunner类中就不需要注入数据源连接对象了,所以,bean.xml更新为:

<!-- 配置Connection的工具类 ConnectionUtils -->
    <bean id="connectionUtils" class="com.itheima.utils.ConnectionUtils">
        <!-- 注入数据源-->
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <bean id="runner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype">
        <!--<constructor-arg name="ds" ref="dataSource"></constructor-arg>-->
    </bean>

接下来需要实现事务管理器,事务管理器主要有开启事务、提交事务、回滚事务、释放连接:

package com.itheima.utils;

/**
 * 和事务管理相关的工具类,它包含了,开启事务,提交事务,回滚事务和释放连接
 */
public class TransactionManager {

    private ConnectionUtils connectionUtils;

    public void setConnectionUtils(ConnectionUtils connectionUtils) {
        this.connectionUtils = connectionUtils;
    }

    /**
     * 开启事务
     */
    public  void beginTransaction(){
        try {
            connectionUtils.getThreadConnection().setAutoCommit(false);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 提交事务
     */
    public  void commit(){
        try {
            connectionUtils.getThreadConnection().commit();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 回滚事务
     */
    public  void rollback(){
        try {
            connectionUtils.getThreadConnection().rollback();
        }catch (Exception e){
            e.printStackTrace();
        }
    }


    /**
     * 释放连接
     */
    public  void release(){
        try {
            connectionUtils.getThreadConnection().close();//还回连接池中
            connectionUtils.removeConnection();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

TransactionManager类中为了将事务与连接关联起来,需要将ConnectionUtils注入进来,此时更新bean.xml:

<!-- 配置事务管理器-->
    <bean id="txManager" class="com.itheima.utils.TransactionManager">
        <!-- 注入ConnectionUtils -->
        <property name="connectionUtils" ref="connectionUtils"></property>
    </bean>

为了将事务迁移到业务层,需要改造业务层实现类的方法:给AccountServiceImpl添加事务的支持:

package com.itheima.service.impl;

import com.itheima.dao.IAccountDao;
import com.itheima.domain.Account;
import com.itheima.service.IAccountService;
import com.itheima.utils.TransactionManager;

import java.util.List;

/**
 * 账户的业务层实现类
 *
 * 事务控制应该都是在业务层
 */
public class AccountServiceImpl_OLD implements IAccountService{

    private IAccountDao accountDao;
    private TransactionManager txManager;

    public void setTxManager(TransactionManager txManager) {
        this.txManager = txManager;
    }

    public void setAccountDao(IAccountDao accountDao) {
        this.accountDao = accountDao;
    }

    @Override
    public List<Account> findAllAccount() {
        try {
            //1.开启事务
            txManager.beginTransaction();
            //2.执行操作
            List<Account> accounts = accountDao.findAllAccount();
            //3.提交事务
            txManager.commit();
            //4.返回结果
            return accounts;
        }catch (Exception e){
            //5.回滚操作
            txManager.rollback();
            throw new RuntimeException(e);
        }finally {
            //6.释放连接
            txManager.release();
        }

    }

    @Override
    public Account findAccountById(Integer accountId) {
        try {
            //1.开启事务
            txManager.beginTransaction();
            //2.执行操作
            Account account = accountDao.findAccountById(accountId);
            //3.提交事务
            txManager.commit();
            //4.返回结果
            return account;
        }catch (Exception e){
            //5.回滚操作
            txManager.rollback();
            throw new RuntimeException(e);
        }finally {
            //6.释放连接
            txManager.release();
        }
    }

    @Override
    public void saveAccount(Account account) {
        try {
            //1.开启事务
            txManager.beginTransaction();
            //2.执行操作
            accountDao.saveAccount(account);
            //3.提交事务
            txManager.commit();
        }catch (Exception e){
            //4.回滚操作
            txManager.rollback();
        }finally {
            //5.释放连接
            txManager.release();
        }

    }

    @Override
    public void updateAccount(Account account) {
        try {
            //1.开启事务
            txManager.beginTransaction();
            //2.执行操作
            accountDao.updateAccount(account);
            //3.提交事务
            txManager.commit();
        }catch (Exception e){
            //4.回滚操作
            txManager.rollback();
        }finally {
            //5.释放连接
            txManager.release();
        }

    }

    @Override
    public void deleteAccount(Integer acccountId) {
        try {
            //1.开启事务
            txManager.beginTransaction();
            //2.执行操作
            accountDao.deleteAccount(acccountId);
            //3.提交事务
            txManager.commit();
        }catch (Exception e){
            //4.回滚操作
            txManager.rollback();
        }finally {
            //5.释放连接
            txManager.release();
        }

    }

    @Override
    public void transfer(String sourceName, String targetName, Float money) {
        try {
            //1.开启事务
            txManager.beginTransaction();
            //2.执行操作

            //2.1根据名称查询转出账户
            Account source = accountDao.findAccountByName(sourceName);
            //2.2根据名称查询转入账户
            Account target = accountDao.findAccountByName(targetName);
            //2.3转出账户减钱
            source.setMoney(source.getMoney()-money);
            //2.4转入账户加钱
            target.setMoney(target.getMoney()+money);
            //2.5更新转出账户
            accountDao.updateAccount(source);

            int i=1/0;

            //2.6更新转入账户
            accountDao.updateAccount(target);
            //3.提交事务
            txManager.commit();

        }catch (Exception e){
            //4.回滚操作
            txManager.rollback();
            e.printStackTrace();
        }finally {
            //5.释放连接
            txManager.release();
        }
    }
}

AccountServiceImpl实现类中为了支持事务,需要将TransactionManager注入进来,更新bean.xml:

<!-- 配置Service -->
    <bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl">
        <!-- 注入dao -->
        <property name="accountDao" ref="accountDao"></property>
        <!-- 注入事务管理器 -->
        <property name="txManager" ref="txManager"></property>
    </bean>

通过将连接绑定在线程上,再将事务迁移到业务层,就实现了transfer()方法中四次调用dao方法使用同一个事务进行管理。

  • 最后,重点来了!!!
    经过了上述的操作,虽然将事务从持久层迁移到了业务层,但是业务层的每个方法都添加了开启、提交、回滚、释放的事务操作,让代码显得臃肿。在Java中,给一些方法动态的增强可以使用动态代理模式实现。事务其实就是在调用持久层方法前后进行提交。回滚等控制,所以可以使用动态代理模式,对业务层的方法进行增强:
    我们新添一个BeanFactory类用来对AccountServiceImpl类进行增强:
package com.itheima.factory;

import com.itheima.service.IAccountService;
import com.itheima.utils.TransactionManager;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * 用于创建Service的代理对象的工厂
 */
public class BeanFactory {

    private IAccountService accountService;

    private TransactionManager txManager;

    public void setTxManager(TransactionManager txManager) {
        this.txManager = txManager;
    }


    public final void setAccountService(IAccountService accountService) {
        this.accountService = accountService;
    }

    /**
     * 获取Service代理对象
     * @return
     */
    public IAccountService getAccountService() {
        return (IAccountService)Proxy.newProxyInstance(accountService.getClass().getClassLoader(),
                accountService.getClass().getInterfaces(),
                new InvocationHandler() {
                    /**
                     * 添加事务的支持
                     *
                     * @param proxy
                     * @param method
                     * @param args
                     * @return
                     * @throws Throwable
                     */
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

                        if("test".equals(method.getName())){
                            return method.invoke(accountService,args);
                        }

                        Object rtValue = null;
                        try {
                            //1.开启事务
                            txManager.beginTransaction();
                            //2.执行操作
                            rtValue = method.invoke(accountService, args);
                            //3.提交事务
                            txManager.commit();
                            //4.返回结果
                            return rtValue;
                        } catch (Exception e) {
                            //5.回滚操作
                            txManager.rollback();
                            throw new RuntimeException(e);
                        } finally {
                            //6.释放连接
                            txManager.release();
                        }
                    }
                });
    }
}

这里使用了jdk动态代理模式,Proxy.newProxyInstance()方法中的三个参数分别为:被代理对象的类加载器,被代理对象的接口,以及实现了InvocationHandler代理接口的实现类。这样就将AccountServiceImpl类中的每个方法进行了增强。此时,更新bean.xml:

<!--配置代理的service-->
    <bean id="proxyAccountService" factory-bean="beanFactory" factory-method="getAccountService"></bean>

    <!--配置beanfactory-->
    <bean id="beanFactory" class="com.itheima.factory.BeanFactory">
        <!-- 注入service -->
        <property name="accountService" ref="accountService"></property>
        <!-- 注入事务管理器 -->
        <property name="txManager" ref="txManager"></property>
    </bean>

这里需要注意的是:1、BeanFactory类是对AccountServiceImpl进行增强,所以需要将其注入进来,然后需要使用getAccountService()返回的代理类作为真正的AccountServiceImpl类,所以也需要将代理类的实例配置在容器中,即为proxyAccountService,该实例是通过BeanFactory的getAccountService()返回得到的,所以使用factory-bean=“beanFactory” factory-method=“getAccountService”。
通过使用动态代理模式进行事务配置后,AccountServiceImpl又可以恢复成之前的样子:

package com.itheima.service.impl;

import com.itheima.dao.IAccountDao;
import com.itheima.domain.Account;
import com.itheima.service.IAccountService;

import java.util.List;

/**
 * 账户的业务层实现类
 *
 * 事务控制应该都是在业务层
 */
public class AccountServiceImpl implements IAccountService{

    private IAccountDao accountDao;

    public void setAccountDao(IAccountDao accountDao) {
        this.accountDao = accountDao;
    }

    @Override
    public List<Account> findAllAccount() {
       return accountDao.findAllAccount();
    }

    @Override
    public Account findAccountById(Integer accountId) {
        return accountDao.findAccountById(accountId);

    }

    @Override
    public void saveAccount(Account account) {
        accountDao.saveAccount(account);
    }

    @Override
    public void updateAccount(Account account) {
        accountDao.updateAccount(account);
    }

    @Override
    public void deleteAccount(Integer acccountId) {
        accountDao.deleteAccount(acccountId);
    }

    @Override
    public void transfer(String sourceName, String targetName, Float money) {
        System.out.println("transfer....");
            //2.1根据名称查询转出账户
            Account source = accountDao.findAccountByName(sourceName);
            //2.2根据名称查询转入账户
            Account target = accountDao.findAccountByName(targetName);
            //2.3转出账户减钱
            source.setMoney(source.getMoney()-money);
            //2.4转入账户加钱
            target.setMoney(target.getMoney()+money);
            //2.5更新转出账户
            accountDao.updateAccount(source);

//            int i=1/0;

            //2.6更新转入账户
            accountDao.updateAccount(target);
    }
}

接下来编写测试类:

package com.itheima.test;

import com.itheima.service.IAccountService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 * 使用Junit单元测试:测试我们的配置
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:bean.xml")
public class AccountServiceTest {

    @Autowired
    @Qualifier("proxyAccountService")
    private IAccountService as;

    @Test
    public void testTransfer(){
        as.transfer("aaa","bbb",100f);
    }

}

注意!!!此时其实需要使用增强类proxyAccountService实例,但是现在容器中存在两个AccountServiceImpl类型的实例,而@Autowired注解是按类型进行注入的,此时必须再次使用@Qualifier(“proxyAccountService”)注解按名称注入,或者直接使用@Resource注解。

到这里,就说完啦~将完整的bean.xml文件和pom.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--配置代理的service-->
    <bean id="proxyAccountService" factory-bean="beanFactory" factory-method="getAccountService"></bean>

    <!--配置beanfactory-->
    <bean id="beanFactory" class="com.itheima.factory.BeanFactory">
        <!-- 注入service -->
        <property name="accountService" ref="accountService"></property>
        <!-- 注入事务管理器 -->
        <property name="txManager" ref="txManager"></property>
    </bean>

     <!-- 配置Service -->
    <bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl">
        <!-- 注入dao -->
        <property name="accountDao" ref="accountDao"></property>

    </bean>

    <!--配置Dao对象-->
    <bean id="accountDao" class="com.itheima.dao.impl.AccountDaoImpl">
        <!-- 注入QueryRunner -->
        <property name="runner" ref="runner"></property>
        <!-- 注入ConnectionUtils -->
        <property name="connectionUtils" ref="connectionUtils"></property>
    </bean>

    <!--配置QueryRunner-->
    <bean id="runner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype">
        <!--<constructor-arg name="ds" ref="dataSource"></constructor-arg>-->
    </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://localhost:3306/test"></property>
        <property name="user" value="root"></property>
        <property name="password" value="root"></property>
    </bean>

    <!-- 配置Connection的工具类 ConnectionUtils -->
    <bean id="connectionUtils" class="com.itheima.utils.ConnectionUtils">
        <!-- 注入数据源-->
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!-- 配置事务管理器-->
    <bean id="txManager" class="com.itheima.utils.TransactionManager">
        <!-- 注入ConnectionUtils -->
        <property name="connectionUtils" ref="connectionUtils"></property>
    </bean>
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.itheima</groupId>
    <artifactId>day03_eesy_01account</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>commons-dbutils</groupId>
            <artifactId>commons-dbutils</artifactId>
            <version>1.4</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
        </dependency>

        <dependency>
            <groupId>c3p0</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.1.2</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
    </dependencies>

</project>

以上理解均从视频资料中学习的,附上链接:https://www.bilibili.com/video/BV1mE411X7yp
下一次再根据事务谈谈Spring中的AOP~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值