Spring整个Ibatis之SqlMapClientDaoSupport

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

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

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

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


  1. <!-- 配置事务特性 -->


  2.    <tx:advice id="txAdvice" transaction-manager="事务管理器名称">


  3.        <tx:attributes>


  4.           <tx:method name="add*" propagation="REQUIRED"/>


  5.           <tx:method name="del*" propagation="REQUIRED"/>


  6.           <tx:method name="update*" propagation="REQUIRED"/>


  7.           <tx:method name="*" read-only="true"/>


  8.       </tx:attributes>


  9.    </tx:advice>




  10.    <!-- 配置哪些类的方法需要进行事务管理 -->


  11.    <aop:config>


  12.       <aop:pointcut id="allManagerMethod" expression="execution(* com.ibatis.manager.*.*(..))"/>


  13.       <aop:advisor advice-ref="txAdvice" pointcut-ref="allManagerMethod"/>


  14.    </aop:config>

这些事务都是声明在业务逻辑层的对象上的。 第二,我们需要一个事务管理器,对事务进行管理,实现整合iBatis和Spring的第二步。

  1. <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">


  2.   <property name="dataSource" ref="dataSource"/>


  3.   </bean>


  4.   <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">


  5.       <property name="driverClassName" value="com.mysql.jdbc.Driver"/>


  6.       <property name="url" value="jdbc:mysql://127.0.0.1/ibatis"/>


  7.       <property name="username" value="root"/>


  8.       <property name="password" value="mysql"/>


  9.   </bean>

此后,我们需要让spring来管理SqlMapClient对象,实现整合iBatis和Spring的第三步

  1. <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">


  2.      <property name="configLocation"><value>classpath:sqlMapConfig.xml</value></property>


  3.   </bean>

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

  1. <?xml version="1.0" encoding="UTF-8" ?>


  2. <!DOCTYPE sqlMapConfig        


  3.    PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.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.  <sqlMap resource="com/ibatis/dao/impl/ibatis/User.xml"/>


  11. </sqlMapConfig>


  12. User.xml:如下  


  13. <?xml version="1.0" encoding="UTF-8" ?>


  14. <!DOCTYPE sqlMap        


  15.    PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"        


  16.    "http://ibatis.apache.org/dtd/sql-map-2.dtd">


  17. <sqlMap namespace="User">


  18. <!-- Use type aliases to avoid typing the full classname every time. -->


  19. <typeAlias alias="User" type="com.ibatis.User"/>


  20. <!-- Select with no parameters using the result map for Account class. -->


  21. <select id="selectAllUsers" resultClass="User">


  22.    select * from t_user  


  23. </select>




  24. <select id="selectUser" resultClass="User" parameterClass="int">


  25.  select * from t_user where id=#id#  


  26. </select>




  27. <insert id="insertUser" parameterClass="User">


  28.  insert into t_user values (  


  29.       null,#username#,#password#  


  30.  )  


  31. </insert>




  32. <update id="updateUser" parameterClass="User">


  33.  update t_user set username = #username#,password=#password#  


  34.  where id=#id#  


  35.  </update>




  36. <delete id="deleteUser" parameterClass="int">


  37.  delete from t_user where id=#id#  


  38. </delete>


  39. </sqlMap>


我们的DAO的编写:

  1. package com.iabtis.dao.impl.ibatis;  


  2. import java.util.List;  


  3. import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;  


  4. import com.ibatis.dao.UserDAO;  


  5. import com.ibatis.crm.model.User;  


  6. public class UserDAOImpl extends SqlMapClientDaoSupport implements UserDAO {  


  7.    public void select(User user) {  


  8.              getSqlMapClientTemplate().delete("selectUser ",user.getId());  


  9.       }  


  10.   public List findAll() {  


  11.              return getSqlMapClientTemplate().queryForList("selectAllUsers ");  


  12.       }  


  13.       public void delete(User user) {  


  14.              getSqlMapClientTemplate().delete("deleteUser ",user.getId());  


  15.       }  


  16.       public void save(User user) {  


  17.              getSqlMapClientTemplate().insert("insertUser ",user);  


  18.       }  


  19.       public void update(User user) {  


  20.              getSqlMapClientTemplate().update("updateUser ",user);  


  21.       }  


  22. }  

继承SqlMapClientDaoSupport,要求我们注入SqlMapClient对象,因此,需要有如下的DAO配置,这是整合iBatis和Spring的最后一步了

  1. <bean id="userDAO" class="com.ibatils.dao.impl.ibatis.UserDAOImpl">

  2.     <property name=”sqlMapClient” ref=”sqlMapClient”/>

  3. </bean>

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


你可能感兴趣的:(如何)