Spring整合mybatis--事务

类似于aop操作(自定义类)_仰望星空的快乐的博客-CSDN博客

把事务当做是方法的增强,配置好之后直接添加到方法上

配置好之后应该是如果被增强的类中的第几个方法有错误,则前面的方法的执行全部回滚。

如果没有错误则可以正常提交。

步骤:

1.在xml文件中声明事务管理器DataSourceTransactionManager,有个属性是前面的datasource。

    <bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="dataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

2.在1的基础上进一步细化,

设置DataSourceTransactionManager给被增强类中的哪个方法添加事务 

    <!--    2.结合aop实现事务的切入-->
    <!--    2.1配置事务的通知,通知是给方法实际增加的部分,把事务当做是增加的方法,添加事务就相当于做增强-->
    <tx:advice id="plus" transaction-manager="dataSourceTransactionManager">
        <tx:attributes>
            <!--           给被增强类中的哪个方法配置事务-->
            <tx:method name="*"/>
        </tx:attributes>
    </tx:advice>

3. 配置事务切入

 <!--    配置事务切入-->
    <aop:config>
        <!--        被增强的方法-->
        <aop:pointcut id="beiadd" expression="execution(* com.rj1192.zyk521.service.*.*(..))"/>
        <!--        增强的方法-->
        <aop:advisor advice-ref="plus" pointcut-ref="beiadd"></aop:advisor>
    </aop:config>

总的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:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.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
       http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
    <!--     关联数据库的资源文件-->
    <context:property-placeholder location="classpath:database.properites"/>
    <!--    获取数据源-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"></property>
        <property name="jdbcUrl" value="${jdbc.url}"></property>
        <property name="user" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
        <!-- c3p0连接池的私有属性 -->
        <property name="maxPoolSize" value="30"/>
        <property name="minPoolSize" value="10"/>
        <!-- 关闭连接后不自动commit -->
        <property name="autoCommitOnClose" value="false"/>
        <!-- 获取连接超时时间 -->
        <property name="checkoutTimeout" value="10000"/>
        <!-- 当获取连接失败重试次数 -->
        <property name="acquireRetryAttempts" value="2"/>
    </bean>

    <!--    配置sqlsession进ioc容器-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <!--        方式一类似于 spring使用jdbctample实现事务,将配置好的DataSourceTransactionManager直接丢进去,但是这种方法老师不建议使用。-->
        <!--        <property name="transactionFactory">-->
        <!--           <bean class="org.apache.ibatis.transaction.managed.ManagedTransactionFactory"></bean>-->
        <!--        </property>-->
    </bean>

    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg index="0" ref="sqlSessionFactory"></constructor-arg>
    </bean>

    <!--    开启注解扫描-->
    <context:component-scan base-package="com.rj1192.zyk521"></context:component-scan>
    <!--    开启代理类对象-->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>


    <!--配置声明式事务-->
    <!-- 1.   开启注解驱动-->
    <mvc:annotation-driven/>
    <bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="dataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!--    2.结合aop实现事务的切入-->
    <!--    2.1配置事务的通知,通知是给方法实际增加的部分,把事务当做是增加的方法,添加事务就相当于做增强-->
    <tx:advice id="plus" transaction-manager="dataSourceTransactionManager">
        <tx:attributes>
            <!--           给被增强类中的哪个方法配置事务-->
            <tx:method name="*"/>
        </tx:attributes>
    </tx:advice>

    <!--    配置事务切入-->
    <aop:config>
        <!--        被增强的方法-->
        <aop:pointcut id="beiadd" expression="execution(* com.rj1192.zyk521.service.*.*(..))"/>
        <!--        增强的方法-->
        <aop:advisor advice-ref="plus" pointcut-ref="beiadd"></aop:advisor>
    </aop:config>
</beans>

 被增强类,insert_one会报错,如果事务起了作用,则del_one(6)应不能删除对应记录

package com.rj1192.zyk521.service;

import com.rj1192.zyk521.Pojo.User;
import com.rj1192.zyk521.mapper.UserMapperImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

//在service层调用usermapperimpl的实现类
@Service
public class Userservice {
    @Autowired
    public UserMapperImpl userMapper;

    public void add_one_userservice() {
        userMapper.del_one(6);
        userMapper.insert_one(new User(200, "name2", 100, "123123"));
    }
}

调用时正常写即可,和使用aop实现方法增强一样

package com.rj1192.zyk521;

import com.rj1192.zyk521.service.Userservice;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.lang.annotation.Target;

public class TestUserService {
    ApplicationContext applicationContext=new ClassPathXmlApplicationContext("Spring-dao.xml");
    @Test
    public void test(){
      Userservice userservice=  applicationContext.getBean(Userservice.class);
      userservice.add_one_userservice();
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值