日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

spring整合mybatis是如何配置事务的?

發(fā)布時(shí)間:2024/9/20 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring整合mybatis是如何配置事务的? 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

作者:郭無(wú)心
鏈接:https://www.zhihu.com/question/30206875/answer/84675373
來(lái)源:知乎
著作權(quán)歸作者所有。商業(yè)轉(zhuǎn)載請(qǐng)聯(lián)系作者獲得授權(quán),非商業(yè)轉(zhuǎn)載請(qǐng)注明出處。

http://czj4451.iteye.com/blog/2037759 mybatis的事務(wù)管理:
一、單獨(dú)使用mybatis組件,使用SqlSession來(lái)處理事務(wù): public class MyBatisTxTest {private static SqlSessionFactory sqlSessionFactory;private static Reader reader;@BeforeClasspublic static void setUpBeforeClass() throws Exception {try {reader = Resources.getResourceAsReader("Configuration.xml");sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);} finally {if (reader != null) {reader.close();}}}@Testpublic void updateUserTxTest() {SqlSession session = sqlSessionFactory.openSession(false); // 打開會(huì)話,事務(wù)開始try {IUserMapper mapper = session.getMapper(IUserMapper.class);User user = new User(9, "Test transaction");int affectedCount = mapper.updateUser(user); // 因后面的異常而未執(zhí)行commit語(yǔ)句User user = new User(10, "Test transaction continuously");int affectedCount2 = mapper.updateUser(user2); // 因后面的異常而未執(zhí)行commit語(yǔ)句int i = 2 / 0; // 觸發(fā)運(yùn)行時(shí)異常session.commit(); // 提交會(huì)話,即事務(wù)提交} finally {session.close(); // 關(guān)閉會(huì)話,釋放資源}} }

測(cè)試會(huì)發(fā)現(xiàn)數(shù)據(jù)沒有被修改

和Spring集成后,使用Spring的事務(wù)管理: <!-- 數(shù)據(jù)源 --> <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><!-- 事務(wù)管理器 --> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 事務(wù)注解驅(qū)動(dòng),標(biāo)注@Transactional的類和方法將具有事務(wù)性 --> <tx:annotation-driven transaction-manager="txManager" /> <bean id="userService" class="com.john.hbatis.service.UserService" /> @Service("userService") public class UserService {@AutowiredIUserMapper mapper;public int batchUpdateUsersWhenException() { // 非事務(wù)性User user = new User(9, "Before exception");int affectedCount = mapper.updateUser(user); // 執(zhí)行成功User user2 = new User(10, "After exception");int i = 1 / 0; // 拋出運(yùn)行時(shí)異常int affectedCount2 = mapper.updateUser(user2); // 未執(zhí)行if (affectedCount == 1 && affectedCount2 == 1) {return 1;}return 0;}@Transactionalpublic int txUpdateUsersWhenException() { // 事務(wù)性User user = new User(9, "Before exception");int affectedCount = mapper.updateUser(user); // 因后面的異常而回滾User user2 = new User(10, "After exception");int i = 1 / 0; // 拋出運(yùn)行時(shí)異常,事務(wù)回滾int affectedCount2 = mapper.updateUser(user2); // 未執(zhí)行if (affectedCount == 1 && affectedCount2 == 1) {return 1;}return 0;} } @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "classpath:beans-da-tx.xml" }) public class SpringIntegrateTxTest {@ResourceUserService userService;@Testpublic void updateUsersExceptionTest() {userService.batchUpdateUsersWhenException();}@Testpublic void txUpdateUsersExceptionTest() {userService.txUpdateUsersWhenException();} } ================================================================================

<bean id="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="yourDataSource" /> </bean> @Transactional public void blabla(){ }

總結(jié)

以上是生活随笔為你收集整理的spring整合mybatis是如何配置事务的?的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。