• Spring 声明式事务管理方式


    声明式事务管理,基于AOP对目标代理,添加环绕通知,比编码方案优势,不具有侵入式,不需要修改原来的代码.

    1.基于XML配置的声明式事务管理方案(案例)

         接口Service

    public interface IAccountService {
        public void account(String outname,String inname,double money);
    }

    service实现类

    //@Transactional()注解时使用
    public class AccountServiceImpl implements IAccountService{
    @Autowired
    private IAccountDao accountDao;

    public void setAccountDao(IAccountDao accountDao) {
    this.accountDao = accountDao;
    }
    //转账操作的方法
    @Override
    public void account(String outname, String inname, double money) {
    //从outname转出
    accountDao.accountOut(outname,money);
    int a=10/0;
    //从inname转入
    accountDao.accountIn(inname,money);
    //setter注入
    }

    Dao接口

    public interface IAccountDao {
    
        public void accountOut(String outname, double money);
    
        public void accountIn(String inname, double money);
    
    }

    Dao实现类

     1 //import org.springframework.jdbc.core.JdbcTemplate;
     2 import org.springframework.jdbc.core.support.JdbcDaoSupport;
     3 //由于JdbcDaoSupport的父类继承了Daosupport,它创建了JdbcTemplate,前提我们先注入一个dataSource
     4 public class AccountDaoImpl extends JdbcDaoSupport implements IAccountDao{
     5     //private JdbcTemplate jdbcTemplate;
     6     //从outname中取钱
     7     @Override
     8     public void accountOut(String outname, double money) {
     9         this.getJdbcTemplate().update("update account set money=money-? where name= ?",money,outname);
    10     }
    11     //向inname中存钱
    12     @Override
    13     public void accountIn(String inname, double money) {
    14         this.getJdbcTemplate().update("update account set money=money+? where name= ?",money,inname);
    15     }
    16 
    17 }

    测试类

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations="classpath:applicationContext.xml")
    public class accountServiceTest {
        @Autowired
        private IAccountService accountService;
        @Test
        public void test1() {
            accountService.account("tom", "fox", 500);
        }
    }

    applicationContext.xml配置文件

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
     4     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
     5     xsi:schemaLocation="
     6         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
     7         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
     8          http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
     9           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
    10 
    11     <!-- 引入外部的properties文件 -->
    12     <context:property-placeholder location="classpath:db.properties" />
    13     <!-- 创建c3p0连接滨 -->
    14     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    15         <property name="driverClass" value="${jdbc.driverClass}" />
    16         <property name="jdbcUrl" value="${jdbc.url}" />
    17         <property name="user" value="${jdbc.username}" />
    18         <property name="password" value="${jdbc.password}" />
    19     </bean>
    20     <!-- service -->
    21     <bean id="accountService" class="cn.itcast.service.AccountServiceImpl">
    22         <property name="accountDao" ref="accountDao"></property>
    23     </bean>
    24     <!-- dao -->
    25     <bean id="accountDao" class="cn.itcast.dao.AccountDaoImpl">
    26         <!-- 当注入dataSource后,底层会自动创建一个JdbcTemplate   Template入门案例 -->
    27         <!-- c3p0和DriverManagerDatasource都可以创建datasource -->
    28         <property name="dataSource" ref="dataSource"></property>
    29     </bean>
    30 
    31     <!-- 配置事务管理器 -->
    32     <bean id="transactionManager"
    33         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    34         <property name="dataSource" ref="dataSource"></property>
    35     </bean>
    36     
    37     <!-- 配置通知 -->
    38     <tx:advice id="txAdvice" transaction-manager="transactionManager">
    39         <tx:attributes>
    40             <!-- 
    41                 name:必须的,对哪些方法进行事务控制
    42                  isolation 可选 设置事务隔离级别 默认是DEFAULT 
    43                  propagation:可选 设置事务传播 默认值 REQUIRED
    44                  timeout 可选 超时时间 默认值-1 
    45                  read-only 可选 默认值是false 如果不是只读,它可以对insert update delete操作,如果是只读不可以。
    46                  rollback-for 可选 可以设置一个异常,如果产生这个异常,触发事务回滚
    47                  no-rolback-for 可选 可以设置一个异常,如果产生这个异常,不会触发事务回滚
    48              -->
    49             <tx:method name="account" />
    50                         
    51         </tx:attributes>
    52     </tx:advice>
    53     
    54     <!-- 配置切面 -->
    55     <aop:config>
    56         <aop:pointcut expression="execution(* cn.itcast.service.IAccountService.account(..))" id="txPointcut"/>
    57         <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
    58     </aop:config>
    59     <!-- 开启事务管理器 注解时使用-->
    60     <tx:annotation-driven transaction-manager="transactionManager"/>
    61 </beans> 
    62     

    db.properties----------当我们需要频繁修改其中的内容,写到这个配置文件便于管理

    jdbc.driverClass=com.mysql.jdbc.Driver
    jdbc.url=jdbc:mysql:///springtest
    jdbc.username=root
    jdbc.password=123
  • 相关阅读:
    剧集更新表
    Pyhton资源
    JAVA资源
    012 循环
    011 条件判断
    010 使用list和tuple
    009 字符串和编码
    007 Python基础
    python 内置函数
    python 获取当前运行的类名函数名inspect.stack()[1][3]
  • 原文地址:https://www.cnblogs.com/wwwzzz/p/7922166.html
Copyright © 2020-2023  润新知