下面是applicationContext.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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-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/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<context:component-scan base-package="com.wlyd.wms.service.*" />
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="classpath*:jdbc.properties" />
</bean>
<!-- 数据源配置, 使用BoneCP数据库连接池 -->
<bean id="dataSource" class="com.jolbox.bonecp.BoneCPDataSource"
destroy-method="close">
<!-- 数据库驱动 -->
<property name="driverClass" value="${jdbc.driverClass}" />
<!-- 相应驱动的jdbcUrl,您懂的 -->
<property name="jdbcUrl" value="${jdbc.jdbcUrl}" />
<!-- 数据库的用户名 -->
<property name="username" value="${jdbc.user}" />
<!-- 数据库的密码 -->
<property name="password" value="${jdbc.password}" />
<!-- 检查数据库连接池中空闲连接的间隔时间,单位是分,默认值:240,如果要取消则设置为0 -->
<property name="idleConnectionTestPeriod" value="240" />
<!-- 连接池中未使用的链接最大存活时间,单位是分,默认值:60,如果要永远存活设置为0 -->
<property name="idleMaxAge" value="60" />
<!-- 每个分区最大的连接数 -->
<property name="maxConnectionsPerPartition" value="30" />
<!-- 每个分区最小的连接数 -->
<property name="minConnectionsPerPartition" value="3" />
<!-- 分区数 ,默认值2,最小1,推荐3-4,视应用而定-->
<property name="partitionCount" value="2" />
<!-- 每次去拿数据库连接的时候一次性要拿几个,默认值:2 -->
<property name="acquireIncrement" value="2" />
<!-- 缓存prepared statements的大小,默认值:0 -->
<property name="statementsCacheSize" value="0" />
<!-- 每个分区释放链接助理进程的数量,默认值:3,除非您的一个数据库连接的时间内做了很多工作,不然过多的助理进程会影响您的性能 -->
<property name="releaseHelperThreads" value="3" />
<property name="acquireRetryAttempts" value="5" />
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis-config.xml" />
</bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory"></constructor-arg>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.wlyd.wms.persistence.mapper" />
</bean>
<bean name="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="del*" propagation="REQUIRED" read-only="false"
rollback-for="java.lang.Exception" />
<tx:method name="insert*" propagation="REQUIRED" read-only="false"
rollback-for="java.lang.Exception" />
<tx:method name="update*" propagation="REQUIRED" read-only="false"
rollback-for="java.lang.Exception" />
<tx:method name="*" propagation="REQUIRED" read-only="true" />
</tx:attributes>
</tx:advice>
<!-- 事务拦截 -->
<aop:config>
<aop:pointcut id="pc"
expression="execution(* com.wlyd.wms.service..*(..))" />
<aop:advisor pointcut-ref="pc" advice-ref="txAdvice" />
</aop:config>
<!-- 初始化基础数据 -->
<bean id="InitSysProperties" class="com.wlyd.wms.util.sysinit.InitSysProperties" />
</beans>
注意:事务是管理一系列动作的,其中任何一步操作失败都要回滚才合理。service平行调用事务不生效,建议: 不要在controller写逻辑处理。正确的次序为:controller>service>mapper
示例代码(只是引导方法取名):
@Autowired
DBMapper mapper;
public void insertDoSomething(){
try{
mappper.add("sssssssssssssssssssss");// 有序操作一
mapper.deleteAll();// 有序操作二
}catch(Exception e){
}
}