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
<?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" 该方法不使用事务 --> <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> <bean id="transManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <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:config> <aop:pointcut id="myPointCut" expression="execution(* cn.*..*.service.EmpService.*(..))" /> <aop:advisor advice-ref="myAdvice" pointcut-ref="myPointCut" /> </aop:config> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean> </beans>
dao层中的自动装配
[java]
view plain
copy
@Autowired private JdbcTemplate jdbcTemplate;
myBatis事务的设置
spring.xml
[html]
view plain
copy
<?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 "> <context:component-scan base-package="cn.et"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <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> </bean> <bean id="transManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <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:config> <aop:pointcut id="myPointCut" expression="execution(* cn.*..*.service.EmpService.*(..))" /> <aop:advisor advice-ref="myAdvice" pointcut-ref="myPointCut" /> </aop:config> <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> </bean> <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层的自动装载
[java]
view plain
copy
@Autowired private EmpMapper mapper;
这里的装配可以是dao层,也可以是service层,因为mapper层映射了需要实现的所有sql语句,还可以实现动态的sql语句,所以mapper可以完全替代dao层。那么这里就可以直接装配到service层也是一样的。