spring整合ibatis事务管理(各种AOP,比较乱)


Spring通过DAO模式,提供了对iBATIS的良好支持。SqlMapClient对象是iBATIS中的主要对象,我们可以通过配置让spring来管理SqlMapClient对象的创建。

与hibernate类似,Spring提供了SqlMapClientDaoSupport对象,我们的DAO可以继承这个类,通过它所提供的SqlMapClientTemplate对象来操纵数据库。看起来这些概念都与hibernate类似。

通过SqlMapClientTemplate来操纵数据库的CRUD是没有问题的,这里面关键的问题是事务处理。Spring提供了强大的声明式事务处理的功能,我们已经清楚hibernate中如何配置声明式的事务,那么在iBATIS中如何获得声明式事务的能力呢?

第一,我们需要了解的是spring通过AOP来拦截方法的调用,从而在这些方法上面添加声明式事务处理的能力。典型配置如下:applicationContext-common.xml

Xml代码 收藏代码
  1. <!--配置事务特性-->
  2. <tx:adviceid="txAdvice"transaction-manager="事务管理器名称">
  3. <tx:attributes>
  4. <tx:methodname="add*"propagation="REQUIRED"/>
  5. <tx:methodname="del*"propagation="REQUIRED"/>
  6. <tx:methodname="update*"propagation="REQUIRED"/>
  7. <tx:methodname="*"read-only="true"/>
  8. </tx:attributes>
  9. </tx:advice>
  10. <!--配置哪些类的方法需要进行事务管理-->
  11. <aop:config>
  12. <aop:pointcutid="allManagerMethod"expression="execution(*com.ibatis.manager.*.*(..))"/>
  13. <aop:advisoradvice-ref="txAdvice"pointcut-ref="allManagerMethod"/>
  14. </aop:config>

这些事务都是声明在业务逻辑层的对象上的。

第二,我们需要一个事务管理器,对事务进行管理。

Xml代码 收藏代码
  1. <beanid="txManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  2. <propertyname="dataSource"ref="dataSource"/>
  3. </bean>
  4. <beanid="dataSource"class="org.apache.commons.dbcp.BasicDataSource">
  5. <propertyname="driverClassName"value="com.mysql.jdbc.Driver"/>
  6. <propertyname="url"value="jdbc:mysql://127.0.0.1/ibatis"/>
  7. <propertyname="username"value="root"/>
  8. <propertyname="password"value="mysql"/>
  9. </bean>

此后,我们需要让spring来管理SqlMapClient对象:

Xml代码 收藏代码
  1. <beanid="sqlMapClient"class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
  2. <propertyname="configLocation"><value>classpath:sqlMapConfig.xml</value></property>
  3. </bean>

我们的sqlMapConfig.xml就可以简写为:

Xml代码 收藏代码
  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <!DOCTYPEsqlMapConfig
  3. PUBLIC"-//ibatis.apache.org//DTDSQLMapConfig2.0//EN"
  4. "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
  5. <sqlMapConfig>
  6. <settings
  7. lazyLoadingEnabled="true"
  8. useStatementNamespaces="true"/>
  9. <!--使用spring之后,数据源的配置移植到了spring上,所以iBATIS本身的配置可以取消-->
  10. <sqlMapresource="com/ibatis/dao/impl/ibatis/User.xml"/>
  11. </sqlMapConfig>

User.xml:如下

Xml代码 收藏代码
  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <!DOCTYPEsqlMap
  3. PUBLIC"-//ibatis.apache.org//DTDSQLMap2.0//EN"
  4. "http://ibatis.apache.org/dtd/sql-map-2.dtd">
  5. <sqlMapnamespace="User">
  6. <!--Usetypealiasestoavoidtypingthefullclassnameeverytime.-->
  7. <typeAliasalias="User"type="com.ibatis.User"/>
  8. <!--SelectwithnoparametersusingtheresultmapforAccountclass.-->
  9. <selectid="selectAllUsers"resultClass="User">
  10. select*fromt_user
  11. </select>
  12. <selectid="selectUser"resultClass="User"parameterClass="int">
  13. select*fromt_userwhereid=#id#
  14. </select>
  15. <insertid="insertUser"parameterClass="User">
  16. insertintot_uservalues(
  17. null,#username#,#password#
  18. )
  19. </insert>
  20. <updateid="updateUser"parameterClass="User">
  21. updatet_usersetusername=#username#,password=#password#
  22. whereid=#id#
  23. </update>
  24. <deleteid="deleteUser"parameterClass="int">
  25. deletefromt_userwhereid=#id#
  26. </delete>
  27. </sqlMap>

我们的DAO的编写:

Java代码 收藏代码
  1. packagecom.iabtis.dao.impl.ibatis;
  2. importjava.util.List;
  3. importorg.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
  4. importcom.ibatis.dao.UserDAO;
  5. importcom.ibatis.crm.model.User;
  6. publicclassUserDAOImplextendsSqlMapClientDaoSupportimplementsUserDAO{
  7. publicvoidselect(Useruser){
  8. getSqlMapClientTemplate().delete("selectUser",user.getId());
  9. }
  10. publicListfindAll(){
  11. returngetSqlMapClientTemplate().queryForList("selectAllUsers");
  12. }
  13. publicvoiddelete(Useruser){
  14. getSqlMapClientTemplate().delete("deleteUser",user.getId());
  15. }
  16. publicvoidsave(Useruser){
  17. getSqlMapClientTemplate().insert("insertUser",user);
  18. }
  19. publicvoidupdate(Useruser){
  20. getSqlMapClientTemplate().update("updateUser",user);
  21. }
  22. }

继承SqlMapClientDaoSupport,要求我们注入SqlMapClient对象,因此,需要有如下的DAO配置:

Xml代码 收藏代码
  1. <beanid="userDAO"class="com.ibatils.dao.impl.ibatis.UserDAOImpl">
  2. <propertyname=”sqlMapClient”ref=”sqlMapClient”/>
  3. </bean>

这就是所有需要注意的问题了,此后就可以在业务逻辑层调用DAO对象了!


<?xml version="1.0" encoding="UTF-8"?>
<beans<wbr><wbr><br style="margin:0px; padding:0px"> xmlns="http://www.springframework.org/schema/beans"<wbr><wbr><br style="margin:0px; padding:0px"> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"<wbr><wbr><br style="margin:0px; padding:0px"> xmlns:aop="http://www.springframework.org/schema/aop"<wbr><wbr><br style="margin:0px; padding:0px"> xmlns:tx="http://www.springframework.org/schema/tx"<wbr><wbr><br style="margin:0px; padding:0px"> xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd<wbr><wbr><br style="margin:0px; padding:0px"> http://www.springframework.org/schema/aop<br style="margin:0px; padding:0px"> http://www.springframework.org/schema/aop/spring-aop-2.5.xsd<wbr><wbr><br style="margin:0px; padding:0px"> http://www.springframework.org/schema/tx<wbr><br style="margin:0px; padding:0px"> http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"&gt;<wbr><wbr><br style="margin:0px; padding:0px"><wbr><wbr><wbr><wbr><br style="margin:0px; padding:0px"><wbr><wbr><wbr>&lt;!-- 数据源配置 --&gt;<br style="margin:0px; padding:0px"><wbr><wbr><wbr>&lt;bean id="dataSource"<br style="margin:0px; padding:0px"><wbr><wbr><wbr><wbr><wbr><wbr>class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"&gt;<br style="margin:0px; padding:0px"><wbr><wbr><wbr><wbr><wbr><wbr>&lt;property name="driverClassName"<br style="margin:0px; padding:0px"><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>value="net.sourceforge.jtds.jdbc.Driver"&gt;<br style="margin:0px; padding:0px"><wbr><wbr><wbr><wbr><wbr><wbr>&lt;/property&gt;<br style="margin:0px; padding:0px"><wbr><wbr><wbr><wbr><wbr><wbr>&lt;property name="url"<br style="margin:0px; padding:0px"><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>value="jdbc:jtds:sqlserver://localhost:1433;DatabaseName=dbName;SelectMethod=cursor"&gt;<br style="margin:0px; padding:0px"><wbr><wbr><wbr><wbr><wbr><wbr>&lt;/property&gt;<br style="margin:0px; padding:0px"><wbr><wbr><wbr><wbr><wbr><wbr>&lt;property name="username" value="sa"&gt;&lt;/property&gt;<br style="margin:0px; padding:0px"><wbr><wbr><wbr><wbr><wbr><wbr>&lt;property name="password" value="sa"&gt;&lt;/property&gt;<br style="margin:0px; padding:0px"><wbr><wbr><wbr>&lt;/bean&gt;<br style="margin:0px; padding:0px"><wbr><wbr><wbr><wbr><br style="margin:0px; padding:0px"><wbr><wbr><wbr>&lt;!-- Spring iBatis Template --&gt;<br style="margin:0px; padding:0px"><wbr><wbr><wbr>&lt;bean id="sqlMapClient"<br style="margin:0px; padding:0px"><wbr><wbr><wbr><wbr><wbr><wbr>class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"&gt;<br style="margin:0px; padding:0px"><wbr><wbr><wbr><wbr><wbr><wbr>&lt;property name="configLocation" value="classpath:config/SqlMapConfig.xml" /&gt;<br style="margin:0px; padding:0px"><wbr><wbr><wbr><wbr><wbr><wbr>&lt;property name="dataSource" ref="dataSource" /&gt;<br style="margin:0px; padding:0px"><wbr><wbr><wbr>&lt;/bean&gt;<br style="margin:0px; padding:0px"><wbr><wbr><wbr><wbr><br style="margin:0px; padding:0px"><wbr><wbr><wbr>&lt;!-- 事务管理 --&gt;<br style="margin:0px; padding:0px"><wbr><wbr><wbr>&lt;bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionMan<wbr>ager"&gt;<br style="margin:0px; padding:0px"><wbr><wbr><wbr>&lt;property name="dataSource" ref="dataSource"&gt;&lt;/property&gt;<br style="margin:0px; padding:0px"><wbr><wbr><wbr>&lt;/bean&gt;<br style="margin:0px; padding:0px"><wbr><wbr><wbr><wbr><br style="margin:0px; padding:0px"><wbr><wbr><wbr>&lt;!-- 声明式事务管理 --&gt;<br style="margin:0px; padding:0px"><wbr><wbr><wbr>&lt;bean id="baseTransactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryB<wbr>ean" abstract="true"&gt;<br style="margin:0px; padding:0px"><wbr><wbr><wbr><wbr><wbr><wbr>&lt;property name="transactionManager" ref="transactionManager"&gt;&lt;/property&gt;<br style="margin:0px; padding:0px"><wbr><wbr><wbr><wbr><wbr><wbr>&lt;property name="transactionAttributes"&gt;<br style="margin:0px; padding:0px"><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>&lt;props&gt;<br style="margin:0px; padding:0px"><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>&lt;prop key="add*"&gt;PROPAGATION_REQUIRED&lt;/prop&gt;<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><br style="margin:0px; padding:0px"><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>&lt;prop key="edit*"&gt;PROPAGATION_REQUIRED&lt;/prop&gt;<br style="margin:0px; padding:0px"><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>&lt;prop key="*"&gt;PROPAGATION_REQUIRED,readOnly&lt;/prop&gt;<br style="margin:0px; padding:0px"><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>&lt;/props&gt;<br style="margin:0px; padding:0px"><wbr><wbr><wbr><wbr><wbr><wbr>&lt;/property&gt;<br style="margin:0px; padding:0px"><wbr><wbr><wbr>&lt;/bean&gt;<br style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><wbr><span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; color:rgb(255,0,0)"><wbr><wbr>&lt;!-- 注意该DAO 一定要继承SqlMapClientDaoSupport 使用getSqlMapClientTemplate()方法,并且要抛出 throws DataAccessException 异常 spring才能捕获并回滚 --&gt;</wbr></wbr></span><br style="margin:0px; padding:0px"><wbr><wbr><wbr>&lt;bean id="UserDAO" class="com.gong.struts.test.UserDAO"&gt;<br style="margin:0px; padding:0px"><wbr><wbr><wbr><wbr><wbr><wbr>&lt;property name="sqlMapClient" ref="sqlMapClient"&gt;&lt;/property&gt;<br style="margin:0px; padding:0px"><wbr><wbr><wbr style="margin:0px; padding:0px">&lt;/bean&gt;<br></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值