Spring 2.5整合iBATIS 2.3并使用Spring的声明式事务管理
程序员文章站
2022-06-13 08:59:33
...
Spring 2.5整合iBATIS 2.3的核心配置文件beans.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:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!-- 配置分散配置的属性文件 --> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- 配置c3p0数据源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driverclass}"/> <property name="jdbcUrl" value="${jdbc.url}"/> <property name="user" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> <property name="maxPoolSize" value="${c3p0.pool.size.max}"/> <property name="minPoolSize" value="${c3p0.pool.size.min}"/> <property name="initialPoolSize" value="${c3p0.pool.size.init}"/> <property name="acquireIncrement" value="${c3p0.pool.size.increment}"/> </bean> <!-- SqlMapClientFactoryBean spring整合ibatis的入口 --> <bean id="sqlMapClient"class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"> <property name="dataSource" ref="dataSource"/> <!-- 引入SqlMapConfig.xml --> <property name="configLocation"> <value>classpath:SqlMapConfig.xml</value> </property> </bean> <!--创建SqlMapClientTemplate类 --> <bean id="sqlMapClientTemplate" class="org.springframework.orm.ibatis.SqlMapClientTemplate"> <property name="sqlMapClient" ref="sqlMapClient"/> </bean> <!-- 事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!-- 事务通知 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="insert*" propagation="REQUIRED" isolation="DEFAULT"/> <tx:method name="update*" propagation="REQUIRED" isolation="DEFAULT"/> <tx:method name="delete*" propagation="REQUIRED" isolation="DEFAULT"/> <tx:method name="select*" propagation="REQUIRED" isolation="DEFAULT" read-only="true"/> <tx:method name="*" propagation="REQUIRED" isolation="DEFAULT"/> </tx:attributes> </tx:advice> <!-- aop配置 --> <aop:config> <!-- 第一个*匹配方法的返回类型 第二*..匹配任何包以及其子包 第三个*匹配以相应表达式结尾的类或接口 第四个.*(..)匹配相应类中带任何参数的方法 --> <aop:pointcut id="txPointcut" expression="execution(* *..*Service.*(..))"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/> </aop:config> <!--将SqlMapClientTemplate模版类注入到相应的DAO对象中--> <bean id="accountDao" class="com.test.dao.impl.AccountDaoImpl"> <property name="sqlMapClientTemplate" ref="sqlMapClientTemplate"/> </bean> <!-- 将相应的DAO对象注入到相应的Service中 --> <bean id="accountService" class="com.test.service.impl.AccountServiceImpl"> <property name="accountDao" ref="accountDao"/> </bean> </beans>