开启mybatis+spring注解式事物管理
在mybatis的配置文件中mybatis.xml
需要加入的配置
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.2.xsd">
<!-- BoneCP配置 <bean id="jndisource1" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" > <value>hxsmsdb</value> </property> </bean> -->
<!-- BoneCP配置 -->
<bean id="jndisource" class="com.jolbox.bonecp.BoneCPDataSource"
destroy-method="close">
<property name="driverClass" value="${oracle.jdbc.driverClassName}" />
<property name="jdbcUrl" value="${oracle.jdbc.url}" />
<property name="username" value="${oracle.jdbc.username}" />
<property name="password" value="${oracle.jdbc.password}" />
<!-- 检查数据库连接池中空闲连接的间隔时间,单位是分,默认值:240,如果要取消则设置为0 -->
<property name="idleConnectionTestPeriodInMinutes" value="6" />
<!-- 连接池中未使用的链接最大存活时间,单位是分,默认值:60,如果要永远存活设置为0 -->
<property name="idleMaxAgeInMinutes" value="2" />
<!-- 每个分区最大的连接数 -->
<property name="maxConnectionsPerPartition" value="3" />
<!-- 每个分区最小的连接数 -->
<property name="minConnectionsPerPartition" value="1" />
<!-- 分区数 ,默认值2,最小1,推荐3-4,视应用而定 -->
<property name="partitionCount" value="3" />
<!-- 每次去拿数据库连接的时候一次性要拿几个,默认值:2 -->
<property name="acquireIncrement" value="5" />
<!-- 缓存prepared statements的大小,默认值:0 -->
<property name="statementsCacheSize" value="100" />
<!-- 每个分区释放链接助理进程的数量,默认值:3,除非你的一个数据库连接的时间内做了很多工作,不然过多的助理进程会影响你的性能 -->
<property name="releaseHelperThreads" value="3" />
</bean>
<!---->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg>
<ref bean="jndisource" />
</constructor-arg>
</bean>
<!-- define the SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="jndisource" />
<property name="configLocation" value="classpath:/config/ibatis/mybatis-config.xml" />
<property name="typeAliasesPackage" value="com.yld" />
<property name="mapperLocations" value="classpath:/config/sqlmap/*Mapper.xml" />
</bean>
<!-- mybatis接口, 可以使用分号或逗号 作为分隔符设置多于一个的包路径 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.yld.publicservice.springweb.dao" />
</bean>
<!-- 数据连接事务 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="jndisource" />
</bean>
<!-- 连接事务的注解配置 -->
<tx:annotation-driven transaction-manager="transactionManager"
proxy-target-class="true" />
</beans>
红色的部分是需要加入的
*1、需要注意的有sqlSessionFactory和transactionManager的数据源一定要相同,
*2、而且注解在方法上面默认捕捉的是runtimeexception,同时这个注解只有在外部调用的第一个方法上面才有效,
例如:类A中的testa调用类B中的testb,testb调用本类中的testsunb,该注解@Transactional只有放在testb上面才有效,如果放在testsunb方法上面是没有效果的,同时@Transactional这个值针对public方法有效,其他也是没有效果的。
这只是我自粗浅的理解,菜鸟mark一下。