使用xml配置声明式事务

版权声明:如需转载,请注明出处,谢谢! https://blog.csdn.net/qq_41172416/article/details/82285160

 注意:以下配置基于上次博客,《使用 MapperScannerConfigurer 注入映射器》的基础上操作的。

链接:https://blog.csdn.net/qq_41172416/article/details/82258021

dao层接口

package com.bdqn.dao.user;

import java.util.List;

import com.bdqn.pojo.User;

public interface UserMapper {

	/**
	 * 按照用户名和用户角色查找用户列表
	 * @param user
	 * @return
	 */
	List<User> getUsers(User user);
	
}

dao层UserMapper文件

<!DOCTYPE mapper
    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.bdqn.dao.user.UserMapper">
	<select id="getUsers" resultType="User" parameterType="User">
		SELECT * FROM `smbms_user` 
		<where>
			<if test="userName!=null and userName!='' ">
				and userName LIKE CONCAT('%',#{userName},'%') 
			</if>
			<if test="userRole!=null">
			 	AND `userRole` =#{userRole}
			</if>
		</where> 
	</select>
</mapper>

 applicationContext.xml文件

注意:需要引入tx和aop两个命名空间下的标签。

<?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: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/aop
         http://www.springframework.org/schema/aop/spring-aop.xsd
          http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context.xsd
         http://www.springframework.org/schema/tx
         http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
    <!-- 配置数据源 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url"
            value="jdbc:mysql://127.0.0.1:3306/smbms?useUnicode=true&amp;characterEncoding=utf-8" />
        <property name="username" value="root" />
        <property name="password" value="root" />
    </bean>
    <!-- 获得sqlSessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 映射数据源 -->
        <property name="dataSource" ref="dataSource" />
        <!-- 映射mybatis核心配置文件 -->
        <property name="configLocation" value="classpath:mybatis-config.xml" />
        <!-- 映射mapper文件 -->
        <property name="mapperLocations">
            <list>
                <value>classpath:com/bdqn/dao/**/*.xml</value>
            </list>
        </property>
    </bean>
    <!--使用(MapperScannerConfigurer)创建的所有映射器实现都会被自动注入sqlSessionFactory实例 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 配置了多个sqlSessionFactory时,需要指定所依赖的sqlSessionFactory实例 -->
        <property name="basePackage" value="com.bdqn.dao" />
    </bean>
    <!--配置扫描注解定义的业务 Bean -->
    <context:component-scan base-package="com.bdqn.service" />

    <!-- 定义事务管理器 -->
    <bean id="txManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
    <!--通过<tx:advice>标签为指定的事务管理器设置事务属性 -->
    <tx:advice id="txAdvice" transaction-manager="txManager">
        <!--定义属性,声明事务规则 -->
        <tx:attributes>
            <tx:method name="*" propagation="REQUIRED" />
        </tx:attributes>
    </tx:advice>
    <!-- 定义切面 -->
    <aop:config>
        <!-- 定义切入点 -->
        <aop:pointcut id="serviceMethod" expression="execution(* com.bdqn.service..*.*(..))" />
        <!-- 将事务增强与切入点组合 -->
        <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod" />
    </aop:config>
</beans>

注意1: 配置DataSourceTransactionManager时,要为其注入事先定义好的数据源组件(即:dataSource)。

对以上部分组件讲解

transaction-manager属性

注意:transactionManager属性的Default Value(默认值)是transactionManager,也就是说,如果定义的事务管理器Bean(即:此处的 id="txManager")的名称是transactionManager,则可以不设置该属性。

 < tx:method >标签

注意:name属性是必须的,用于指定匹配的方法。

propagation属性:事务传播机制,该属性可选的值有如下几种。

属性值 解释
REQUIRED 所需

默认值,表示如果存在一个事务,则支持当前事务;如果当前没有事务,则开启一个新的事务。

REQUIRES_NEW
表示开启一个新的事务。如果一个事务已经存在,则将这个事务挂起,开启新事务执行该方法。
MANDATORY 强制性
表示如果存在一个事务,则支持当前事务;如果当前没有一个活动的事务,则抛出异常。
NESTED 套叠的 / 嵌套的
表示如果存在一个活动的事务,则创建一个事务作为当前事务的嵌套事务运行;如果没有当前事务,该取值与REQUIRED相同。
SUPPORTS 支持 / 资助 / 供养 / 维持
表示如果存在一个事务,则支持当前事务;如果当前没有事务,则按非事务方式执行。
NOT_SUPPORTED 不支持
表示总是以非事务方式执行。如果一个事务已经存在,则将这个存在的事务挂起,然后执行该方法。
NEVER 从不 / 根本不 / 没有

表示总是以非事务方式执行。如果当前存在一个活动的事务,则抛出异常。  

 isolation属性:事务隔离等级。即当前事务和其他事务的隔离程度,在并发事务处理的情况下需要考虑它的设置。

属性 解释
DEFAULT 默认值,表示使用数据库默认的事务隔离级别
READ_UNCOMMITTED 未提交读
READ_COMMITTED 提交读
REPEATABLE_READ 可重复读
SERIALIZABLE 串行读

timeout: 事务超时时间。允许事务运行的最长时间,以为单位,超过给定的时间自动回滚,防止事务执行时间过长而影响系统性能。该属性需要底层的实现支持。默认值为-1,表示不超时。

read-only:事务是否为只读,默认值为false。对于只执行查询功能的事务,把它设置为true,能提高事务处理的性能。

rollback-for:设定能够触发 回滚的异常类型。Spring默认中在抛出 Rutime Exception时才标识事务回滚。可以通过全限定类名自行指定需要回滚事务的异常,多个类名用英文逗号隔开。

no-rollback-for:设定不触发回滚的异常类型。Spring默认 checked Exception 不会触发事务回滚。可以通过全限定类名自行指定不需要回滚事务的异常,多个类名用英文逗号隔开。

总结配置的步骤

1、 导入tx 和 aop 命名空间

2、定义事务管理器 Bean,并为其注入数据源 Bean

3、通过<tx:advice>配置事务增强,绑定事务管理器并针对不同方法定义事务规则

4、配置切面,将事务增强与方法切入点组合

 

猜你喜欢

转载自blog.csdn.net/qq_41172416/article/details/82285160