spring整合mybatis是如何配置事务的?
2017-02-28 12:00
344 查看
作者:郭无心
链接:https://www.zhihu.com/question/30206875/answer/84675373
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
mybatis的事务管理:
一、单独使用mybatis组件,使用SqlSession来处理事务:
测试会发现数据没有被修改
和Spring集成后,使用Spring的事务管理:
链接:https://www.zhihu.com/question/30206875/answer/84675373
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
http://czj4451.iteye.com/blog/2037759
mybatis的事务管理:
一、单独使用mybatis组件,使用SqlSession来处理事务:
public class MyBatisTxTest { private static SqlSessionFactory sqlSessionFactory; private static Reader reader; @BeforeClass public static void setUpBeforeClass() throws Exception { try { reader = Resources.getResourceAsReader("Configuration.xml"); sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); } finally { if (reader != null) { reader.close(); } } } @Test public void updateUserTxTest() { SqlSession session = sqlSessionFactory.openSession(false); // 打开会话,事务开始 try { IUserMapper mapper = session.getMapper(IUserMapper.class); User user = new User(9, "Test transaction"); int affectedCount = mapper.updateUser(user); // 因后面的异常而未执行commit语句 User user = new User(10, "Test transaction continuously"); int affectedCount2 = mapper.updateUser(user2); // 因后面的异常而未执行commit语句 int i = 2 / 0; // 触发运行时异常 session.commit(); // 提交会话,即事务提交 } finally { session.close(); // 关闭会话,释放资源 } } }
测试会发现数据没有被修改
和Spring集成后,使用Spring的事务管理:
<!-- 数据源 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" lazy-init="true"> <property name="driverClassName"> <value>org.logicalcobwebs.proxool.ProxoolDriver</value> </property> <property name="url"> <value>proxool.gcld</value> </property> </bean> <!-- sessionFactory --> <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="configLocation" value="classpath:mybatis-config.xml"> </property> <property name="dataSource" ref="dataSource"/> </bean> <!-- 事务管理器 --> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 事务注解驱动,标注@Transactional的类和方法将具有事务性 --> <tx:annotation-driven transaction-manager="txManager" /> <bean id="userService" class="com.john.hbatis.service.UserService" />
@Service("userService") public class UserService { @Autowired IUserMapper mapper; public int batchUpdateUsersWhenException() { // 非事务性 User user = new User(9, "Before exception"); int affectedCount = mapper.updateUser(user); // 执行成功 User user2 = new User(10, "After exception"); int i = 1 / 0; // 抛出运行时异常 int affectedCount2 = mapper.updateUser(user2); // 未执行 if (affectedCount == 1 && affectedCount2 == 1) { return 1; } return 0; } @Transactional public int txUpdateUsersWhenException() { // 事务性 User user = new User(9, "Before exception"); int affectedCount = mapper.updateUser(user); // 因后面的异常而回滚 User user2 = new User(10, "After exception"); int i = 1 / 0; // 抛出运行时异常,事务回滚 int affectedCount2 = mapper.updateUser(user2); // 未执行 if (affectedCount == 1 && affectedCount2 == 1) { return 1; } return 0; } }
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "classpath:beans-da-tx.xml" }) public class SpringIntegrateTxTest { @Resource UserService userService; @Test public void updateUsersExceptionTest() { userService.batchUpdateUsersWhenException(); } @Test public void txUpdateUsersExceptionTest() { userService.txUpdateUsersWhenException(); } }
================================================================================
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="yourDataSource" /> </bean>
@Transactional public void blabla(){ }
相关文章推荐
- Spring + mvc,Mybatis整合 (含事务配置,分页功能)
- SpringMVC+Spring+Mybatis整合,使用druid连接池,声明式事务,maven配置
- Spring + mvc,Mybatis整合 (含事务配置,分页功能)
- SpringMVC+Spring+Mybatis整合,使用druid连接池,声明式事务,maven配置
- Spring + mvc,Mybatis整合 (含事务配置,分页功能)
- SpringBoot系列七:SpringBoot 整合 MyBatis(配置 druid 数据源、配置 MyBatis、事务控制、druid 监控)
- spring+mybatis 事务之如何在service层配置事务
- SpringMVC+Spring+Mybatis整合,使用druid连接池,声明式事务,maven配置
- spring整合mybatis之事务配置
- spring数据源配置及事务管理--mybatis3
- MyBatis3整合Spring3的Transaction事务处理
- Spring3.3 整合 Hibernate3、MyBatis3.2 配置多数据源/动态切换数据源 方法
- 事务级别Spring的事务如何配置
- Spring+Mybatis整合事务不起作用之解决方案汇总
- Spring3.3 整合 Hibernate3、MyBatis3.2 配置多数据源/动态切换数据源 方法
- Spring、MyBatis的整合数据映射器类(UserMapper->iocContext.xml)配置文件详解
- Spring的事务如何配置
- mybatis与spring整合(基于配置文件)
- mybatis与spring整合(基于配置文件)
- Spring3.0和Mybatis的集成,含事务配置