spring+mybaits 事务配置

数据源中配置好事务,包括tx 和 aop的命名空间,<tx:annotation-driven />等
<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:mvc="http://www.springframework.org/schema/mvc" xmlns:task="http://www.springframework.org/schema/task"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-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/task http://www.springframework.org/schema/task/spring-task-3.0.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
    <tx:annotation-driven />
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <property name="driverClass" value="${mysql.jdbc.driverClassName}" />
        <property name="jdbcUrl" value="${mysqlDataBase.url}" />
        <property name="user" value="${mysqlDataBase.user}" />
        <property name="password" value="${mysqlDataBase.passwd}" />
        <property name="maxPoolSize" value="10" />
        <property name="acquireIncrement" value="5" />
        <property name="idleConnectionTestPeriod" value="60" />
        <property name="testConnectionOnCheckin" value="true" />
        <property name="testConnectionOnCheckout" value="true" />
        <property name="preferredTestQuery" value="select 1 from dual" />
    </bean>
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
</beans>

然后可以在方法或类级别使用 @Transactional了

你可能感兴趣的:(spring)