spring事务和myBatis事务的设置

spring和myBatis的事务是怎么设置的

Spring配置文件中关于事务配置总是由三个组成部分,分别是DataSource、TransactionManager和代理机制这三部分,


无论哪种配置方式,一般变化的只是代理机制这部分。DataSource、TransactionManager这两部分只是会根据数据访问


方式有所变化,比如使用myBatis进行数据访问时,DataSource实际为org.apache.commons.dbcp.BasicDataSource,TransactionManager的实现为


org.springframework.jdbc.datasource.DataSourceTransactionManager。


spring事务的设置

spring.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: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-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
	
	<!-- 
		 isolation 表示事务的隔离级别
                DEFAULT 是获取数据库的隔离界别
                READ_UNCOMMITTED 读取到其他spring方法未提交的数据 引发脏读问题
                READ_COMMITTED 只能读取到其他事务已经提交的数据  默认
                REPEATABLE_READ 可重复读  解决脏读不可重复读  引发幻读
	            SERIALIZABLE    串行化 解决了所有问题
	            隔离级别越高 需要消耗更多的资源去处理 效率低下
	            
	     propagation 传播特性 方法和方法之间的调用  事务是否能够传播
				- REQUIRED (spring 默认的传播特性)
				   必须存在一个事务 如果没有事务 创建一个事务  如果父方法存在事务使用父方法的事务
				- REQUIRES_NEW   
				 必须存在一个事务 不管有没有事务都要自己创建一个事务  
				- SUPPORTS
				不会创建事务 如果有事务在事务中运行 没有事务 不使用事务
				- MANDATORY(必须存在事务)
				不会创建事务 有事务 使用当前事务  没有事务跑出 错误状态异常
				- NEVER (不能存在事务)
				不会创建事务 没有事务正常运行 有事务抛出异常 
				- NOT_SUPPORTED
				不支持事务 如果存在事务就挂起 没有事务正常运行
				- NESTED (少用)
				嵌套异常 不同的数据源之间的事务处理   相同的数据 就是 REQUIRED
				
			spring tx事务处理中 只有运行时异常才会自动回滚数据	
			  rollback-for 指定需要回滚的非运行时异常
			  no-rollback-for="" 指定不需要回滚的运行时异常
			  
			timeout="-1" 会一直等待数据操作完成
			 默认的-1一直等待
			 单位s秒
			  
			read-only="true" 该方法不使用事务  
	 -->
	
	
	<!-- 读取jdbc.properties文件 -->
	<context:property-placeholder location="classpath:/jdbc.properties"></context:property-placeholder>
	
	<!-- 数据的连接  数据源 -->
	<bean id="dataSource"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="url" value="${url}"></property>
		<property name="driverClassName" value="${driverClass}"></property>
		<property name="username" value="${account}"></property>
		<property name="password" value="${password}"></property>
	</bean>
	
	<!-- 事务管理器 spring帮助我们控制事务 -->
	<bean id="transManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
	</bean>

	<!-- 当切点拦截到某个操作的方法  发送通知给tx定义的通知管理  调用事务管理器 提交和回滚 -->
	<tx:advice id="myAdvice" transaction-manager="transManager">
		<tx:attributes>
			<!-- 默认的配置 -->
			<tx:method name="add*" propagation="REQUIRED" />
			<tx:method name="query*" propagation="REQUIRED" />
			<tx:method name="update*" propagation="REQUIRED" />
			<tx:method name="delete*" propagation="REQUIRED" />
			<tx:method name="*" read-only="true" />
		</tx:attributes>
	</tx:advice>

	<!-- aop切面 -->
	<aop:config>
		<aop:pointcut id="myPointCut"
			expression="execution(* cn.*..*.service.EmpService.*(..))" />
		<aop:advisor advice-ref="myAdvice" pointcut-ref="myPointCut" />
	</aop:config>
	
	<!-- 创建jdbcTemplate对象 -->
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
</beans>


dao层中的自动装配

	
	//自动装配
	@Autowired
	private JdbcTemplate jdbcTemplate;


myBatis事务的设置

spring.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:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:mvc="http://www.springframework.org/schema/mvc"

	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
		">
	
	
	<!-- springmvc的配置只能扫描控制层  spring配置文件不能扫描控制层 -->
	<context:component-scan base-package="cn.et">
		<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
	</context:component-scan>
	
	<!-- 读取jdbc.properties文件 -->
	<context:property-placeholder 
	location="classpath:/cn/et/mybatis/lesson06/utils/jdbc.properties" />
	
	<!-- 数据库连接池 -->
	<bean id="dataSource"
		class="org.apache.commons.dbcp.BasicDataSource">
		<property name="url" value="${url}"></property>
		<property name="driverClassName" value="${driverClass}"></property>
		<property name="username" value="${account}"></property>
		<property name="password" value="${password}"></property>
		<!-- 
			initialSize   10
			默认生成10个连接,那么要用到连接的时候就直接拿一条出来用就可以了,
			就不用等到用的时候再去产生连接
		 -->
		<property name="initialSize" value="10"></property>
		<!-- 发起一条测试的sql语句去连接一下数据库,看是否可以正常连接数据库 -->
	</bean>
	
	<!-- 事务管理器 spring帮助我们控制事务 -->
	<bean id="transManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
	</bean>

	<!-- 需要导入架包aspectjweaver 事务架包 -->
	<!-- 当切点拦截到某个操作的方法  发送通知给tx定义的通知管理  调用事务管理器 提交和回滚 -->
	<tx:advice id="myAdvice" transaction-manager="transManager">
		<tx:attributes>
			<!-- 默认的配置 -->
			<tx:method name="add*" propagation="REQUIRED" />
			<tx:method name="query*" propagation="REQUIRED" />
			<tx:method name="update*" propagation="REQUIRED" />
			<tx:method name="delete*" propagation="REQUIRED" />
			<!-- *代表了除了上面配置的方法都不使用事务 -->
			<tx:method name="*" read-only="true" />
		</tx:attributes>
	</tx:advice>

	<!-- aop切面 -->
	<aop:config>
		<aop:pointcut id="myPointCut" expression="execution(* cn.*..*.service.EmpService.*(..))" />
		<aop:advisor advice-ref="myAdvice" pointcut-ref="myPointCut" />
	</aop:config>
	
	<!-- 创建一个jdbc模板SqlSessionFactoryBean -->
	<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	
	
	<!-- 这里一定要用sqlSessionFactoryBeanName,不然加载不了driverClass -->
	<!-- 扫描接口映射和注解和xml文件 -->
	<bean id="scanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="sqlSessionFactoryBeanName" value="sessionFactory"></property>
		<!-- 
			如果映射的是Mapper接口直接放到mapper包里面扫描就发好了
			如果映射的是xml配置文件需要把配置文件名改成对应的接口一样的名称,并都要放到mapper包下
		 -->
		<property name="basePackage" value="cn.*..*.mapper"></property>
	</bean>
	
</beans>



dao层或service层的自动装载

	@Autowired
	private EmpMapper mapper;

这里的装配可以是dao层,也可以是service层,因为mapper层映射了需要实现的所有sql语句,还可以实现动态的sql语句,

所以mapper可以完全替代dao层。那么这里就可以直接装配到service层也是一样的。









  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值