Spring事务及Spring整合MyBatis

SM整合的步骤

 

1,建库建表

2,新建maven模块

3,修改maven目录

4,修改pom.xml文件,添加依赖

5,添加MyBaits模板(SqlMapperConfig.xml和XXXMapper.xml文件)

6,添加ApplicationContext_mapper.xml

7,添加ApplicationContext_seervice.xml

8,添加pojo数据表对应的实体类

9,添加mapper包,添加添加 XXXMapper.java 和 XXXMapper.xml文件,并开发

10,添加service包,添加UserService接口和UserServiceImpl实现类

11,添加测试类

1,在业务层的applicationContext_service.xml文件中添加事务配置信息

 <!--添加事务-->
    <!--1,添加事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!--配置数据源,因为事务必须关联数据库处理-->
        <property name="dataSource" ref="datasource"></property>
    </bean>
    <!--2,添加事务的注解驱动-->
    <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>

2,在业务实现类中添加事务注解@Transactional(propagation = Propagation.REQUIRED),出现异常,事务就会撤销事务。

3,常见的数据库事务设置

//该注解可以添加在类上和方法上,来控制作用范围
@Transactional(
        propagation = Propagation.REQUIRED,//事务的传播特性
        noRollbackForClassName = {("ArithmeticException"),("ArithmeticException")},//设置那些异常不回滚事务
        noRollbackFor = ArithmeticException.class, //设置那些异常不回滚,按照指定异常的类型
        rollbackForClassName = {},//发生那些异常必须回滚
        rollbackFor = ArithmeticException.class,//设置那些异常出现必须回滚,按照异常的类型
        timeout = -1,//连接超时设置,默认为-1,永不超时
        readOnly = false,//默认是false,如果查询操作,必须设置为true
        isolation = Isolation.DEFAULT//设置事务当前数据库自己的隔离级别
         
) //添加事务注解处理方式

spring事务添加的两种方式:

1,注解式事务:使用注解在类或者方法上添加事务,麻烦且存在弊端,所以采用声明式事务

2,声明式事务:

在配置文件中添加一次,整个项目遵循事务的设定。

要求项目中方法名有规范

1)完成增加的操作包含:add,save,insert,set字符

2)完成删除的操作包含:delete,drop,remove,clear字符

3)完成更新的操作包含:update,change,modify字符

4)完成查询的操作包含:select,get,find,search字符

当在配置事务切面时,可以使用通配符 * 来匹配所有方法。

<?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:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">


    <!--导入数据访问层mapper文件-->
    <import resource="applicationContext_mapper.xml"></import>

    <!--开启包组件扫描-->
    <context:component-scan base-package="com.spring.service.impl"></context:component-scan>

    <!--添加事务管理器-->
    <bean id = "transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!--绑定数据源-->
        <property name="dataSource" ref="datasource"></property>
        <!--配置事务切面-->
        <tx:advice id="myTrans" transaction-manager="transactionManager">
            <tx:attributes>
                <tx:method name="*select*" read-only="true"/><!--将所有方法名中包含select,get等方法设置只读-->
                <tx:method name="*get*" read-only="true"/>
                <tx:method name="*find*" read-only="true"/>
                <tx:method name="*search*" read-only="true"/>
                <tx:method name="*add*" propagation="REQUIRED"/><!--将add,modify添加数据的方法隔离级别设置为REQUIRED-->
                <tx:method name="*modify*" propagation="REQUIRED"/>
                <tx:method name="*save*" propagation="REQUIRED" no-rollback-for="ArithmeticException"/><!--算术异常不回滚-->
                <tx:method name="*insert*" propagation="REQUIRED" no-rollback-for="ArithmeticException"/>
                <tx:method name="*delete*" propagation="REQUIRED"/><!--delete,remove删除数据的方法隔离级别设置为REQUIRED-->
                <tx:method name="*drop*" propagation="REQUIRED"/>
                <tx:method name="*remove*" propagation="REQUIRED"/>
                <tx:method name="*update*" propagation="REQUIRED"/><!--update,change添加数据的方法隔离级别设置为REQUIRED-->
                <tx:method name="*change*" propagation="REQUIRED"/>
                <tx:method name="*" propagation="SUPPORTS"/><!--将所有方法设置为支持事务-->
            </tx:attributes>
        </tx:advice>

        <!--绑定切面和切入点-->
        <aop:config>
            <!--配置切入点,指定发个包下的那些方法被加强-->
            <aop:pointcut id="myCut" expression="execution(* com.spring.service.impl.*.*(..))"/>
            <!--绑定切入点和切面-->
            <aop:advisor advice-ref="myTrans" pointcut-ref="myCut"></aop:advisor>
        </aop:config>

    </bean>
    
</beans>

这里配置的全局的事务设计,可以在方法或者类上添加注解,顶替掉全局的事务配置

 <!--添加事务的注解驱动-->
    <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>

事务的传播特性:不同事务之间的合并与互斥

 项目中的所有事务,必须添加到业务逻辑层上

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值