mybatis開啟spring事務(wù)代碼解析
1、事務(wù)
Spring事務(wù)的本質(zhì)其實(shí)就是數(shù)據(jù)庫對事務(wù)的支持,沒有數(shù)據(jù)庫的事務(wù)支持,spring是無法提供事務(wù)功能的。最終都是調(diào)用數(shù)據(jù)庫連接來完成事務(wù)的開啟、提交和回滾。
2、模塊
那么在對于spring事務(wù)而言,幾個(gè)不可或缺的模塊就是數(shù)據(jù)源、事務(wù)管理器以及事務(wù)編程
3、xml配置
<!--事務(wù)管理器-->
<bean id="springTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!--數(shù)據(jù)源-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8" />
<property name="username" value="root" />
<property name="password" value="123456" />
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 指定sqlMapConfig總配置文件,訂制的environment在spring容器中不在生效-->
<!--指定實(shí)體類映射文件,可以指定同時(shí)指定某一包以及子包下面的所有配置文件,mapperLocations和configLocation有一個(gè)即可,當(dāng)需要為實(shí)體類指定別名時(shí),可指定configLocation屬性,再在mybatis總配置文件中采用mapper引入實(shí)體類映射文件 -->
<!--<property name="configLocation" value="classpath:fwportal/beans/dbconfig/mybatis.xml" />-->
<property name="mapperLocations" value="classpath:mapper/*.xml" />
</bean>
<!--將DAO接口注冊為BEAN-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="TRANSACTION.DAO" />
</bean>
4、事務(wù)編程
@Test
public void testDelete() throws Exception {
ApplicationContext context = new ClassPathXmlApplicationContext("mysqltransaction.xml");
DataSourceTransactionManager springTransactionManager = (DataSourceTransactionManager) context.getBean("springTransactionManager");
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
//開啟事務(wù)
TransactionStatus status = springTransactionManager.getTransaction(def);
final StudentDAO dao = (StudentDAO)context.getBean("studentDAO");
try {
dao.delete(2L);
}
catch (Exception ex) {
springTransactionManager.rollback(status);
//事務(wù)回滾
throw ex;
}
springTransactionManager.commit(status);
//事務(wù)提交
}
5、總結(jié)
以上就是利用mybatis和spring完成了對事務(wù)操作的簡要案例??梢詫?shù)據(jù)庫事務(wù)隔離級別進(jìn)行配置,mysql的數(shù)據(jù)庫隔離級別是connection維度的。
還可以設(shè)置事務(wù)的超時(shí)時(shí)間,即超時(shí)事務(wù)自動回滾。
以上就是本文關(guān)于mybatis開啟spring事務(wù)代碼解析的全部內(nèi)容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
- SpringMVC+MyBatis 事務(wù)管理(實(shí)例)
- MyBatis在Spring環(huán)境下的事務(wù)管理
- springboot使用mybatis開啟事務(wù)回滾
- Spring事務(wù)&Spring整合MyBatis的兩種方式
- springboot配置mybatis和事務(wù)管理方式
- springboot mybatis手動事務(wù)的實(shí)現(xiàn)
- Mybatis事務(wù)如何跟Spring結(jié)合(數(shù)據(jù)庫事務(wù)特性和Spring事務(wù)管理源碼)
- MyBatisPlus+Spring實(shí)現(xiàn)聲明式事務(wù)的方法實(shí)現(xiàn)
相關(guān)文章
Java中動態(tài)設(shè)置JVM參數(shù)的方法總結(jié)
通過動態(tài)設(shè)置JVM參數(shù),開發(fā)者可以更有效地管理資源使用和優(yōu)化性能,本文將詳細(xì)闡述如何在Java中動態(tài)設(shè)置JVM參數(shù),感興趣的小伙伴可以了解下2024-12-12
SpringBoot使用spring retry重試機(jī)制的操作詳解
實(shí)際工作中由于網(wǎng)絡(luò)波動等原因?qū)е麓a執(zhí)行失敗需要重新執(zhí)行,保證最終能夠完成業(yè)務(wù)功能,通常來說,會用try/catch,while循環(huán)或者定時(shí)任務(wù)重處理,但是這樣的做法缺乏統(tǒng)一性,要多寫很多代碼,spring-retry組件可以通過注解優(yōu)雅的實(shí)現(xiàn)重處理功能2024-12-12
SpringCloud使用Feign實(shí)現(xiàn)遠(yuǎn)程調(diào)用流程詳細(xì)介紹
OpenFeign源于Netflix的Feign,是http通信的客戶端。屏蔽了網(wǎng)絡(luò)通信的細(xì)節(jié),直接面向接口的方式開發(fā),讓開發(fā)者感知不到網(wǎng)絡(luò)通信細(xì)節(jié)。所有遠(yuǎn)程調(diào)用,都像調(diào)用本地方法一樣完成2023-02-02
Java util.List如何實(shí)現(xiàn)列表分段處理
這篇文章主要介紹了Java util.List如何實(shí)現(xiàn)列表分段處理,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09
SpringBoot借助spring.factories文件跨模塊實(shí)例化Bean
這篇文章主要介紹了SpringBoot借助spring.factories文件跨模塊實(shí)例化Bean,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,需要的小伙伴可以參考一下2022-04-04
Spring?Security中如何獲取AuthenticationManager對象
有時(shí)需要使用AuthenticationManager(以下簡稱Manager)對象,可是這個(gè)對象不是Bean,沒有直接保存在Spring的Bean庫中,那么如何獲取Spring Security中的這個(gè)對象呢,需要的朋友可以參考下2022-11-11

