Spring的事务管理

来源:互联网 发布:erp进销存软件 编辑:程序博客网 时间:2024/06/01 18:55
Spring事务管理两种方式
  • 编程式事务管理(不用)

    • 通过代码实现
  • 声明式事务

    • 基于xml配置文件实现
    • 基于注解实现(重点)
  • 主要API

    • 事务管理器:PlatformTransactionManager接口
    • 针对不同的Dao框架,Spring使用不同的实现

      • DataSourceTransactionManager Spring JDBC /MyBatis
      • HibernateTransactionManager Hibernate
  • 实现步骤

    • 情景:转账操作

    • Step1: 导入jar包及其配置约束

    • Step2: 配置事务管理器
    • Step3: 配置事务增强
    • Step4: 配置切面

基于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: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">     <!-- 开启注解扫描         1. 到包里面扫描类、方法、属性上面是否有注解    -->    <context:component-scan base-package="com.jeff"></context:component-scan>    <!-- 配置c3p0 -->    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">        <!-- 注入属性值 -->        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>        <property name="jdbcUrl" value="jdbc:mysql:///jeff"></property>        <property name="user" value="root"></property>        <property name="password" value="123456"></property>    </bean>    <!-- 配置模板 -->    <bean id="jdbcTempalte" class="org.springframework.jdbc.core.JdbcTemplate">        <!-- 注入数据源 -->        <property name="dataSource" ref="dataSource"></property>    </bean>    <!-- 配置事务管理器 -->    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">        <!-- 注入数据源 ,指定需要管理哪个数据库-->        <property name="dataSource" ref="dataSource"></property>    </bean>    <!-- 配置事务增强 -->    <tx:advice id="txAdvice" transaction-manager="transactionManager">        <!-- 事务操作 -->        <tx:attributes>            <!-- 设置需要进行事务的方法                1. 可以直接写方法名字                2. 可以写通配符             -->            <tx:method name="account*" propagation="REQUIRED"/>             </tx:attributes>        </tx:advice>    <!-- 开启aop操作 -->    <!-- <aop:aspectj-autoproxy></aop:aspectj-autoproxy> -->    <!-- 配置切面 -->    <aop:config>        <!-- 切入点 -->        <aop:pointcut expression="execution(* com.jeff.transaction.service.OrderService.*(..))" id="pointcut1"/>        <!-- 切面 -->        <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut1"/>           </aop:config></beans>
@Repository("orderDao")public class OrderDao {    @Autowired    private JdbcTemplate jdbcTemplate;    //支出    public int cost(Double costMoney,String accountName){        String sql = "UPDATE account SET salary = salary - ? WHERE account_name = ?";        int rows = jdbcTemplate.update(sql, costMoney,accountName);        return rows;    }    //收入    public int income(Double incomeMoney,String accountName){        String sql = "UPDATE account SET salary = salary + ? WHERE account_name = ?";        int rows = jdbcTemplate.update(sql, incomeMoney,accountName);        return rows;    }}@Service("orderService")public class OrderService {    @Autowired    private OrderDao orderDao;    public void accountTrade(){        Double accountMoney = 1000.0;        //支出        orderDao.cost(accountMoney,"云帆");        /*int i = 10/0;*/        //收入        orderDao.income(accountMoney,"黎明");        System.out.println("转账¥"+accountMoney);    }}@Testpublic void testAccountTrade() {    ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml");    OrderService orderService = (OrderService) ctx.getBean("orderService");    orderService.accountTrade();}

注解管理

  • Step1: 配置事务管理器

    <!-- 配置事务管理器 --><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">    <!-- 注入数据源 ,指定需要管理哪个数据库-->    <property name="dataSource" ref="dataSource"></property></bean>
  • Step2: 配置事务注解

    <tx:annotation-driven transaction-manager="transactionManager"/>
  • Step3: 在使用事务方法所在类上面添加注解

    @Service("orderService")@Transactionalpublic class OrderService {    @Autowired    private OrderDao orderDao;    public void accountTrade(){        Double accountMoney = 1000.0;        //支出        orderDao.cost(accountMoney,"云帆");        /*int i = 10/0;*/        //收入        orderDao.income(accountMoney,"黎明");        System.out.println("转账¥"+accountMoney);    }}
0 0
原创粉丝点击