日期:2014-05-18 浏览次数:20645 次
<!-- 扫描注释组件 -->
<context:component-scan base-package="com.tl"/>
<!-- 隐式地向 Spring 容器注册-->
<context:annotation-config/>
<!-- 配置数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/bbs"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
<property name="maxActive" value="100"/>
<property name="maxIdle" value="30"/>
<property name="maxWait" value="500"/>
<!-- 如果数据源的defaultAutoCommit设置为True了,那么方法中如果自己捕获了异常,事务是不会回滚的,如果没有自己捕获异常则事务会回滚 -->
<property name="defaultAutoCommit" value="false"/>
</bean>
<!-- 配置mybatis sqlSession工厂 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath*:com/tl/dao/*.xml"/>
</bean>
<!-- 事务管理 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<tx:advice id="txAdivce" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" rollback-for="SQLException"/>
<tx:method name="udpate*" propagation="REQUIRED" rollback-for="SQLException"/>
<tx:method name="insert*" propagation="REQUIRED" rollback-for="SQLException"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="pointcut" expression="execution(* com.tl.serviceImpl.*.*(..))" />
<aop:advisor advice-ref="txAdivce" pointcut-ref="pointcut"/>
</aop:config>
<bean
id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
p:dataSource-ref="dataSource"
p:defaultTimeout="40">
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
1.类注解
@Service
@Transactional(readOnly=false) &