Spring 2.5整合iBATIS 2.3并使用Spring的声明式事务管理

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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值