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

[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"  
  4.     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"  
  5.   
  6.   
  7.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd  
  8. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd  
  9. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd  
  10. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">  
  11.       
  12.     <!--   
  13.          isolation 表示事务的隔离级别  
  14.                 DEFAULT 是获取数据库的隔离界别  
  15.                 READ_UNCOMMITTED 读取到其他spring方法未提交的数据 引发脏读问题  
  16.                 READ_COMMITTED 只能读取到其他事务已经提交的数据  默认  
  17.                 REPEATABLE_READ 可重复读  解决脏读不可重复读  引发幻读  
  18.                 SERIALIZABLE    串行化 解决了所有问题  
  19.                 隔离级别越高 需要消耗更多的资源去处理 效率低下  
  20.                   
  21.          propagation 传播特性 方法和方法之间的调用  事务是否能够传播  
  22.                 - REQUIRED (spring 默认的传播特性)  
  23.                    必须存在一个事务 如果没有事务 创建一个事务  如果父方法存在事务使用父方法的事务  
  24.                 - REQUIRES_NEW     
  25.                  必须存在一个事务 不管有没有事务都要自己创建一个事务    
  26.                 - SUPPORTS  
  27.                 不会创建事务 如果有事务在事务中运行 没有事务 不使用事务  
  28.                 - MANDATORY(必须存在事务)  
  29.                 不会创建事务 有事务 使用当前事务  没有事务跑出 错误状态异常  
  30.                 - NEVER (不能存在事务)  
  31.                 不会创建事务 没有事务正常运行 有事务抛出异常   
  32.                 - NOT_SUPPORTED  
  33.                 不支持事务 如果存在事务就挂起 没有事务正常运行  
  34.                 - NESTED (少用)  
  35.                 嵌套异常 不同的数据源之间的事务处理   相同的数据 就是 REQUIRED  
  36.                   
  37.             spring tx事务处理中 只有运行时异常才会自动回滚数据    
  38.               rollback-for 指定需要回滚的非运行时异常  
  39.               no-rollback-for="" 指定不需要回滚的运行时异常  
  40.                 
  41.             timeout="-1" 会一直等待数据操作完成  
  42.              默认的-1一直等待  
  43.              单位s秒  
  44.                 
  45.             read-only="true" 该方法不使用事务    
  46.      -->  
  47.       
  48.       
  49.     <!-- 读取jdbc.properties文件 -->  
  50.     <context:property-placeholder location="classpath:/jdbc.properties"></context:property-placeholder>  
  51.       
  52.     <!-- 数据的连接  数据源 -->  
  53.     <bean id="dataSource"  
  54.         class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
  55.         <property name="url" value="${url}"></property>  
  56.         <property name="driverClassName" value="${driverClass}"></property>  
  57.         <property name="username" value="${account}"></property>  
  58.         <property name="password" value="${password}"></property>  
  59.     </bean>  
  60.       
  61.     <!-- 事务管理器 spring帮助我们控制事务 -->  
  62.     <bean id="transManager"  
  63.         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  64.         <property name="dataSource" ref="dataSource"></property>  
  65.     </bean>  
  66.   
  67.     <!-- 当切点拦截到某个操作的方法  发送通知给tx定义的通知管理  调用事务管理器 提交和回滚 -->  
  68.     <tx:advice id="myAdvice" transaction-manager="transManager">  
  69.         <tx:attributes>  
  70.             <!-- 默认的配置 -->  
  71.             <tx:method name="add*" propagation="REQUIRED" />  
  72.             <tx:method name="query*" propagation="REQUIRED" />  
  73.             <tx:method name="update*" propagation="REQUIRED" />  
  74.             <tx:method name="delete*" propagation="REQUIRED" />  
  75.             <tx:method name="*" read-only="true" />  
  76.         </tx:attributes>  
  77.     </tx:advice>  
  78.   
  79.     <!-- aop切面 -->  
  80.     <aop:config>  
  81.         <aop:pointcut id="myPointCut"  
  82.             expression="execution(* cn.*..*.service.EmpService.*(..))" />  
  83.         <aop:advisor advice-ref="myAdvice" pointcut-ref="myPointCut" />  
  84.     </aop:config>  
  85.       
  86.     <!-- 创建jdbcTemplate对象 -->  
  87.     <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">  
  88.         <property name="dataSource" ref="dataSource"></property>  
  89.     </bean>  
  90. </beans>  

dao层中的自动装配

[java]  view plain  copy
  1. //自动装配  
  2. @Autowired  
  3. private JdbcTemplate jdbcTemplate;  


myBatis事务的设置

spring.xml

[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"  
  4.     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"  
  5.     xmlns:mvc="http://www.springframework.org/schema/mvc"  
  6.   
  7.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd  
  8.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd  
  9.         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd  
  10.         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd  
  11.         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd  
  12.         ">  
  13.       
  14.       
  15.     <!-- springmvc的配置只能扫描控制层  spring配置文件不能扫描控制层 -->  
  16.     <context:component-scan base-package="cn.et">  
  17.         <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>  
  18.     </context:component-scan>  
  19.       
  20.     <!-- 读取jdbc.properties文件 -->  
  21.     <context:property-placeholder   
  22.     location="classpath:/cn/et/mybatis/lesson06/utils/jdbc.properties" />  
  23.       
  24.     <!-- 数据库连接池 -->  
  25.     <bean id="dataSource"  
  26.         class="org.apache.commons.dbcp.BasicDataSource">  
  27.         <property name="url" value="${url}"></property>  
  28.         <property name="driverClassName" value="${driverClass}"></property>  
  29.         <property name="username" value="${account}"></property>  
  30.         <property name="password" value="${password}"></property>  
  31.         <!--   
  32.             initialSize   10  
  33.             默认生成10个连接,那么要用到连接的时候就直接拿一条出来用就可以了,  
  34.             就不用等到用的时候再去产生连接  
  35.          -->  
  36.         <property name="initialSize" value="10"></property>  
  37.         <!-- 发起一条测试的sql语句去连接一下数据库,看是否可以正常连接数据库 -->  
  38.     </bean>  
  39.       
  40.     <!-- 事务管理器 spring帮助我们控制事务 -->  
  41.     <bean id="transManager"  
  42.         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  43.         <property name="dataSource" ref="dataSource"></property>  
  44.     </bean>  
  45.   
  46.     <!-- 需要导入架包aspectjweaver 事务架包 -->  
  47.     <!-- 当切点拦截到某个操作的方法  发送通知给tx定义的通知管理  调用事务管理器 提交和回滚 -->  
  48.     <tx:advice id="myAdvice" transaction-manager="transManager">  
  49.         <tx:attributes>  
  50.             <!-- 默认的配置 -->  
  51.             <tx:method name="add*" propagation="REQUIRED" />  
  52.             <tx:method name="query*" propagation="REQUIRED" />  
  53.             <tx:method name="update*" propagation="REQUIRED" />  
  54.             <tx:method name="delete*" propagation="REQUIRED" />  
  55.             <!-- *代表了除了上面配置的方法都不使用事务 -->  
  56.             <tx:method name="*" read-only="true" />  
  57.         </tx:attributes>  
  58.     </tx:advice>  
  59.   
  60.     <!-- aop切面 -->  
  61.     <aop:config>  
  62.         <aop:pointcut id="myPointCut" expression="execution(* cn.*..*.service.EmpService.*(..))" />  
  63.         <aop:advisor advice-ref="myAdvice" pointcut-ref="myPointCut" />  
  64.     </aop:config>  
  65.       
  66.     <!-- 创建一个jdbc模板SqlSessionFactoryBean -->  
  67.     <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
  68.         <property name="dataSource" ref="dataSource"></property>  
  69.     </bean>  
  70.       
  71.       
  72.     <!-- 这里一定要用sqlSessionFactoryBeanName,不然加载不了driverClass -->  
  73.     <!-- 扫描接口映射和注解和xml文件 -->  
  74.     <bean id="scanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
  75.         <property name="sqlSessionFactoryBeanName" value="sessionFactory"></property>  
  76.         <!--   
  77.             如果映射的是Mapper接口直接放到mapper包里面扫描就发好了  
  78.             如果映射的是xml配置文件需要把配置文件名改成对应的接口一样的名称,并都要放到mapper包下  
  79.          -->  
  80.         <property name="basePackage" value="cn.*..*.mapper"></property>  
  81.     </bean>  
  82.       
  83. </beans>  

dao层或service层的自动装载

[java]  view plain  copy
  1. @Autowired  
  2. private EmpMapper mapper;  

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

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值