spring学习10之JDBCTemplate事务(AOP方式)

这里的service层没有使用spring的 jdbcTemplate

第一步:引入jar包(详见spring学习8之JDBC的CRUD

第二步:配置配置文件(后面用到再写)

第三步、在com.pp包下创建一个demo3,然后创建一个AccountDao接口类

package com.pp.demo3;
public interface AccountDao {
    void outMoney(String out, double money);
    void inMoney(String in, double money);
}

第四步:新建一个AccountDaoImpl实现类,实现AccountDao的方法。

package com.pp.demo3;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {
    public void outMoney(String out, double money) {
        this.getJdbcTemplate().update("update t_account set money=money-? where name=?",money,out);
    }
    public void inMoney(String in, double money) {
        this.getJdbcTemplate().update("update t_account set money=money+? where name=?",money,in);
    }
}

第五步:创建AccountService接口类

package com.pp.demo3;
public interface AccountService {
  void pay(String out, String in, double money);
}

*第六步:创建AccountServiceImpl实现 AccountService的接口

package com.pp.demo3;
public class AccountServiceImpl implements AccountService {
    private AccountDao accountDao;
    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }
    public void pay(final String out, final String in, final double money) {
       accountDao.outMoney(out,money);
        int i=6;
        int d=i/0;
       accountDao.inMoney(in,money);
    }
}

第七步:配置applicationContext.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:p="http://www.springframework.org/schema/p"
       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
">
    <!--注解扫描-->
    <context:component-scan base-package="com.pp"/>
   <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
       <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
       <property name="url" value="jdbc:mysql:///spring_jdbc"/>
       <property name="username" value="root"/>
       <property name="password" value="123"/>
    </bean>
     <!--使Dao层被spring管理-->
    <bean id="accountDao" class="com.pp.demo3.AccountDaoImpl">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!--使Service层被spring管理-->
    <bean id="accountService" class="com.pp.demo3.AccountServiceImpl" >
        <property name="accountDao" ref="accountDao"/>
    </bean>
    <!--平台的事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!--声明性事务-->   
    <tx:advice id="myAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="pay" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>
     <!--AOP编程-->  
    <aop:config>
        <aop:advisor advice-ref="myAdvice" pointcut="execution(public * com.pp.demo3.AccountServiceImpl.pay(..))"/>
    </aop:config>
    
</beans>

1.配置事务的传播性行为
propagation=“REQUIRED” 支持当前事务,如果不存在就新建一个(默认)
propagation=“SUPPORTS” 支持当前事务,如果不存在,就不适用事务
propagation=“MANDATORY” 支持当前事务,如果不存在,抛出异常。
tx:advice中的id就是配置切面编程的id;transaction-manager这里填写平台事务管理器的transaction-manager配置。tx:methodname配置前面service层实现类的方法,这里使用的是pay方法,这个方法将来要调用事务;propagation参数填写REQUIRED,即如果没有事务,就新建一个事务。aop:config中advice-ref和上面tx:advice参数是一样的,pointcut切入点execution(public * com.pp.demo3.AccountServiceImpl.pay(…))中com.pp.demo3.AccountServiceImpl.pay(…)为具体的类中的方法,括号内的…代表可以传入多个参数。

第八步:编写测试类TransDmeo

package com.pp.demo3;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.Resource;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext2.xml")
public class TransDmeo {
    @Resource(name = "accountService")
    private AccountService service;
    @Test
    public void run(){
        service.pay("a","b",1000);
    }
}

源码地址:https://gitee.com/yangforever/project-learning.git

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值