ibatis與spring的整合

          Spring通過DAO模式,提供了對iBATIS的良好支持。SqlMapClient對象是iBATIS中的主要對象,我們可以通過配置讓spring來管理SqlMapClient對象的創建。

          hibernate類似,Spring 提供了SqlMapClientDaoSupport對象,我們的DAO可以繼承這個類,通過它所提供的SqlMapClientTemplate對象來操縱數據庫。看起來這些概念都與hibernate類似。

          通過SqlMapClientTemplate來操縱數據庫的CRUD是沒有問題的,這里面關鍵的問題是事務處理。Spring提供了強大的聲明式事務處理的功能,我們已經清楚hibernate中如何配置聲明式的事務,那么在iBATIS中如何獲得聲明式事務的能力呢?

          第一,我們需要了解的是spring通過AOP來攔截方法的調用,從而在這些方法上面添加聲明式事務處理的能力。典型配置如下:applicationContext-common.xml

              <!-- 配置事務特性 -->

              <tx:advice id="txAdvice" transaction-manager="事務管理器名稱">

                  <tx:attributes>

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

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

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

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

                 </tx:attributes>

              </tx:advice>

             

              <!-- 配置哪些類的方法需要進行事務管理 -->

              <aop:config>

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

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

              </aop:config>

          這些事務都是聲明在業務邏輯層的對象上的。

          第二,我們需要一個事務管理器,對事務進行管理。

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

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

              </bean>

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

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

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

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

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

              </bean>

          此后,我們需要讓spring來管理SqlMapClient對象:

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

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

              </bean>

          我們的sqlMapConfig.xml就可以簡寫為:

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

          <!DOCTYPE sqlMapConfig     

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

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

          <sqlMapConfig>

              <settings

                 lazyLoadingEnabled="true"

                  useStatementNamespaces="true" />

              <!-- 使用spring之后,數據源的配置移植到了spring上,所以iBATIS本身的配置可以取消 -->

            <sqlMap resource="com/ibatis/dao/impl/ibatis/User.xml"/>

          </sqlMapConfig>

          User.xml:如下

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

          <!DOCTYPE sqlMap     

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

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

          <sqlMap namespace="User">

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

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

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

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

              select * from t_user

           </select>

           

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

            select * from t_user where id=#id#

           </select>

           

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

            insert into t_user values (

                 null,#username#,#password#

            )

           </insert>

           

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

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

            where id=#id#

            </update>

           

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

            delete from t_user where id=#id#

           </delete>

          </sqlMap>

          我們的DAO的編寫:

          package com.iabtis.dao.impl.ibatis;

          import java.util.List;

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

          import com.ibatis.dao.UserDAO;

          import com.ibatis.crm.model.User;

          public class UserDAOImpl extends SqlMapClientDaoSupport implements UserDAO {

              public void select(User user) {

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

                 }

             public List findAll() {

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

                 }

                 public void delete(User user) {

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

                 }

                 public void save(User user) {

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

                 }

                 public void update(User user) {

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

                 }

          }

          繼承SqlMapClientDaoSupport,要求我們注入SqlMapClient對象,因此,需要有如下的DAO配置:

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

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

          </bean>

          這就是所有需要注意的問題了,此后就可以在業務邏輯層調用DAO對象了!

          posted on 2009-11-12 23:45 飛熊 閱讀(245) 評論(0)  編輯  收藏 所屬分類: Ibatis

          <2009年11月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          導航

          統計

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          收藏夾

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 宁安市| 小金县| 桐城市| 疏勒县| 慈利县| 南通市| 双流县| 和顺县| 靖西县| 宁夏| 社旗县| 兴国县| 安丘市| 平乐县| 驻马店市| 峨眉山市| 始兴县| 仙游县| 扎兰屯市| 台东县| 绥德县| 玉林市| 福鼎市| 佛坪县| 阜宁县| 千阳县| 成武县| 大城县| 湘阴县| 呼伦贝尔市| 成安县| 景德镇市| 蚌埠市| 白朗县| 印江| 介休市| 绍兴县| 唐河县| 阿坝县| 沙坪坝区| 丰原市|