一、基本配置
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<!-- 連接配接池配置 -->
<bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig">
<property name="poolName" value="springHikariCP" />
<property name="connectionTestQuery" value="SELECT 1" />
<property name="dataSourceClassName" value="${jdbc.driverClassName}" />
<property name="dataSourceProperties">
<props>
<prop key="url">${jdbc.url}</prop>
<prop key="user">${jdbc.username}</prop>
<prop key="password">${jdbc.password}</prop>
<prop key="cachePrepStmts">true</prop>
<prop key="prepStmtCacheSize">250</prop>
<prop key="prepStmtCacheSqlLimit">2048</prop>
</props>
</property>
<!-- 控制自動送出行為 default:true -->
<property name="autoCommit" value="true"/>
<!--連接配接池擷取的連接配接是否隻讀 default:false-->
<property name="readOnly" value="false"/>
<!--最大連接配接逾時時間 default:30秒-->
<property name="connectionTimeout" value="30000"/>
<!--最大空閑逾時時間 default:10分鐘 -->
<property name="idleTimeout" value="600000"/>
<!--連接配接池中一個連接配接的最大生命周期 default:30分鐘-->
<property name="maxLifetime" value="1800000 "/>
</bean>
<bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
<constructor-arg ref="hikariConfig" />
</bean>
<!-- define the SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:META-INF/mybatis/mybatis-config.xml"/>
<property name="mapperLocations" value="classpath:META-INF/mybatis/mapper/*.xml"/>
</bean>
<!-- scan for mappers and let them be autowired -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="annotationClass" value="org.springframework.stereotype.Repository"/>
<property name="basePackage" value="com.geenk.market.dal"/>
</bean>
<!-- 聲明編碼方式事務 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
<property name="transactionManager" ref="transactionManager"/>
<!--ISOLATION_DEFAULT 表示由使用的資料庫決定 -->
<property name="isolationLevelName" value="ISOLATION_DEFAULT"/>
<property name="propagationBehaviorName" value="PROPAGATION_REQUIRED"/>
</bean>
<!-- 啟動使用注解實作聲明式事務管理的支援-->
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
該配置方案是最基本的事物配置方案,MyBatis自動參與到spring事務管理中,無需額外配置,隻要org.mybatis.spring.SqlSessionFactoryBean引用的資料源與DataSourceTransactionManager引用的資料源一緻即可,否則事務管理會不起作用。但是缺點是事物的傳播行為都是一緻的,不能精确的為特定的方法配置事物的傳播行為。
二、增強配置
<tx:advice id="transactionAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="modify*" propagation="REQUIRED" />
<tx:method name="edit*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="remove*" propagation="REQUIRED" />
<tx:method name="get*" propagation="SUPPORTS" />
<tx:method name="find*" propagation="SUPPORTS" />
<tx:method name="load*" propagation="SUPPORTS" />
<tx:method name="search*" propagation="SUPPORTS" />
<tx:method name="datagrid*" propagation="SUPPORTS" />
<tx:method name="*" propagation="SUPPORTS" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="transactionPointcut" expression="execution(* com.geenk.market.dal..*Impl.*(..))" />
<aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" />
</aop:config>
此種配置方案可以精确事物到特定方法,對資料庫有改動的操作如新增、修改、和删除。以事物方式來進行,對資料庫無改動的操作則可以以非事物的方式來進行。
三、基于注解配置
<!-- 配置事務管理器-->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<tx:annotation-driven transaction-manager="txManager"/>
然後隻要在要使用的類或方法上加上注解@Transactional便可。
作者:no-npe 出處:https://www.cnblogs.com/geekdc 本文版權歸作者和部落格園共有,歡迎轉載,但未經作者同意必須保留此聲明,且在文章頁面明顯位置給出原文連結,否則保留追究法律責任的權利。 由于作者個人水準有限,如果文中有什麼錯誤,歡迎指出。以免更多的人被誤導。 |