spring学习11之JDBCTemplate事务(AOP注解方式)

第一步:配置pom文件,加载jar包(略)

第二步:配置applicationContext3.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="userDao" class="com.pp.demo1.UserDaoImpl"/>
    <bean id="myAspectXml" class="com.pp.demo1.MyAspectXml"/>-->
   <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层注解-->
    <bean id="accountDao" class="com.pp.demo4.AccountDaoImpl">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!--service层注解-->
    <bean id="accountService" class="com.pp.demo4.AccountServiceImpl" >
        <property name="accountDao" ref="accountDao"/>
    </bean>
    
    <!--平台的事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!--配置注解驱动-->
    <tx:annotation-driven transaction-manager="transactionManager"/>


</beans>

这里前面spring学习9之JDBCTemplate事务(AOP方式)不同的是,此处没有配置事务的传播性行为,而是使用 tx:annotation-driven配置注解驱动。

第三步:在com.pp下创建demo4,然后创建AccountDao接口(略)

第四步:创建AccountDaoImpl类继承和实现AccountDao接口(略)

第五步:创建AccountService接口类(略)

第六步:创建AccountServiceImpl类继承和实现AccountService接口

package com.pp.demo4;

import org.springframework.transaction.annotation.Transactional;

@Transactional
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);
    }
}

我们在前面的配置文件中开启类注解事务的配置,所以在这里我们只需要在类前面使用@Transactional代表开启事务。

第七步:创建TransDmeo测试类(略)

package com.pp.demo4;

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:applicationContext3.xml")
public class TransDmeo {
    @Resource(name = "accountService")
    private AccountService service;
    @Test
    public void run(){
        service.pay("a","b",1000);
    }
}

这里的代码和前面几次的案例代码一致,不一样的是,加载文件使用的是applicationContext3.xml.

第八步:测试事务是否开启

通过service层的实现类,int i=6; int d=i/0;该代码先不用注释,然后执行测试类,查看数据库是否数据有变化;然后将其注释,然后再运行测试类,查看数据库数据是否有变化。
源码地址:https://gitee.com/yangforever/project-learning.git

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值