mybatis_resource.xml
<!--
初始化连接dataSource.initialSize=10
最小空闲连接
dataSource.minIdle=5
最大空闲连接
dataSource.maxIdle=20
最大连接数量
dataSource.maxActive=50
超时等待时间以毫秒为单位 6000毫秒/1000等于6秒
maxWait=6000
是否在自动回收超时连接的时候打印连接的超时错误
dataSource.logAbandoned=true
是否自动回收超时连接
dataSource.removeAbandoned=true
超时时间(以秒数为单位)
dataSource.removeAbandonedTimeout=180
-->
<!-- 数据库连接池 -->
<bean id="dataSourceMyBatis"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close"
p:driverClassName="${jdbc.driverClassName}"
p:url="${jdbc.url}"
p:username="${jdbc.username}"
p:password="${jdbc.password}"
p:initialSize="${c3p0.initialPoolSize}"
p:minIdle="${c3p0.minPoolSize}"
p:maxIdle="50"
p:maxActive="${c3p0.maxPoolSize}"
p:maxWait="6000"
p:logAbandoned="true"
p:removeAbandoned="true"
p:removeAbandonedTimeout="180" />
<!-- Mybatis's sqlSessionFactory config -->
<bean id="sqlSessionFactoryMyBatis" class="org.mybatis.spring.SqlSessionFactoryBean"
p:dataSource-ref="dataSourceMyBatis"
p:configLocation="classpath:mybatis/mybatis-config.xml"
p:mapperLocations="classpath:mybatis/mapper/**/*.xml"/>
<bean name="transactionManagerMyBatis"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSourceMyBatis"></property>
</bean>
<!-- 启用注解式事务 -->
<tx:annotation-driven proxy-target-class="true" transaction-manager="transactionManagerMyBatis" />
<!-- 启用声明式事务 -->
<tx:advice id="txAdviceMyBatis" transaction-manager="transactionManagerMyBatis">
<tx:attributes>
<!-- 新增一类的关键字 -->
<tx:method name="create*" propagation="REQUIRED" rollback-for="Throwable" />
<tx:method name="add*" propagation="REQUIRED" rollback-for="Throwable" />
<tx:method name="save*" propagation="REQUIRED" rollback-for="Throwable" />
<!-- 更新一类的关键字 -->
<tx:method name="modify*" propagation="REQUIRED" rollback-for="Throwable" />
<tx:method name="update*" propagation="REQUIRED" rollback-for="Throwable" />
<!-- 删除一类的关键字 -->
<tx:method name="delete*" propagation="REQUIRED" rollback-for="Throwable" />
<tx:method name="remove*" propagation="REQUIRED" rollback-for="Throwable" />
<!-- 其他方法只读 -->
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>
<!-- 给所有的service类添加事务拦截 -->
<aop:config>
<aop:pointcut id="serviceOperation"
expression="execution(* a.b.c..service.*.*(..))" />
<aop:advisor advice-ref="txAdviceMyBatis" pointcut-ref="serviceOperation" />
</aop:config>