<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    瘋狂

    STANDING ON THE SHOULDERS OF GIANTS
    posts - 481, comments - 486, trackbacks - 0, articles - 1
      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

    ibatis學習(三)---ibatis與spring的整合

    Posted on 2007-12-07 18:26 瘋狂 閱讀(70600) 評論(25)  編輯  收藏
     

    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對象了!


    評論

    # re: ibatis學習(三)---ibatis與spring的整合  回復  更多評論   

    2007-12-08 09:42 by laocat
    豁然開朗 !!

    # re: ibatis學習(三)---ibatis與spring的整合  回復  更多評論   

    2008-03-24 16:08 by 屹礫
    整合的問題現在終于清楚了,
    對于advice和aop還有點不清楚。

    # re: ibatis學習(三)---ibatis與spring的整合[未登錄]  回復  更多評論   

    2008-06-13 17:26 by 冷漠大神
    真是不錯的文章啊 如果在在可以把源碼提供下載 那就更完美了 :-) 呵呵 是不是有點貪心

    # re: ibatis學習(三)---ibatis與spring的整合[未登錄]  回復  更多評論   

    2008-06-13 17:34 by 冷漠大神
    有沒有代碼下載啊?

    # re: ibatis學習(三)---ibatis與spring的整合  回復  更多評論   

    2008-08-25 11:35 by 379548695qq
    UserDAO類里面的內容是什么?

    # re: ibatis學習(三)---ibatis與spring的整合[未登錄]  回復  更多評論   

    2009-03-19 10:17 by 蟲子
    正在煩惱中,google到了你的方案!呵呵

    # re: ibatis學習(三)---ibatis與spring的整合  回復  更多評論   

    2009-08-13 16:32 by jadmin
    很好,學習了

    # re: ibatis學習(三)---ibatis與spring的整合  回復  更多評論   

    2009-08-15 23:15 by 匹馬單槍
    好帖, 學習了!

    # re: ibatis學習(三)---ibatis與spring的整合  回復  更多評論   

    2009-09-01 13:07 by 2
    為什么我的出錯了

    # re: ibatis學習(三)---ibatis與spring的整合[未登錄]  回復  更多評論   

    2010-04-28 11:10 by test
    autocommit 如果不設置為false, 事務有用么

    # re: ibatis學習(三)---ibatis與spring的整合  回復  更多評論   

    2010-11-01 13:42 by rr
    sqlMapClient里應該注入dataSource

    # re: ibatis學習(三)---ibatis與spring的整合[未登錄]  回復  更多評論   

    2011-09-19 16:27 by ddd
    不用封裝實體類嗎?

    # re: ibatis學習(三)---ibatis與spring的整合  回復  更多評論   

    2011-09-27 19:33 by LL
    public void select(User user) {

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

    }


    出錯了

    # re: ibatis學習(三)---ibatis與spring的整合  回復  更多評論   

    2012-04-28 14:42 by 張毅
    文章很好

    # re: ibatis學習(三)---ibatis與spring的整合  回復  更多評論   

    2012-09-04 14:02 by 蘭偉
    能幫幫我嗎 我在sqlserver上建了個user表 id為自增的主鍵 要增加個user 在配置文件中的selectkey 怎么寫啊??

    # re: ibatis學習(三)---ibatis與spring的整合[未登錄]  回復  更多評論   

    2012-10-09 17:22 by 1
    1

    # re: ibatis學習(三)---ibatis與spring的整合呃呃呃  回復  更多評論   

    2013-04-11 15:11 by 恩恩
    地對地導彈

    # re: ibatis學習(三)---ibatis與spring的整合[未登錄]  回復  更多評論   

    2013-04-28 10:39 by tbw
    有沒有實例啊

    # re: ibatis學習(三)---ibatis與spring的整合[未登錄]  回復  更多評論   

    2013-04-28 15:09 by 123
    很垃圾ibatis

    # re: ibatis學習(三)---ibatis與spring的整合  回復  更多評論   

    2013-10-08 14:08 by 可耕地
    在在苛下人手仍有雨

    # re: ibatis學習(三)---ibatis與spring的整合  回復  更多評論   

    2014-01-23 13:17 by 哈林木
    iBatis 是apache 的一個開源項目,一個O/R Mapping 解決方案,iBatis 最大的特點就是小巧,上手很快。
    如果不需要太多復雜的功能,iBatis 是能夠滿足你的要求又足夠靈活的最簡單的解決方案,現在的iBatis 已經改名為Mybatis 了。

    近期項目用到 iBatis,所以需要學習iBatis,下面是總結幾個不錯學習網站給大家學習參考:

    1、官網(英文資料):http://www.mybatis.org/

    2、iBATIS(中文教程):http://www.yiibai.com/ibatis/

    3、iBATIS - iBATIS Apache軟件基金會的官方網站。
    http://ibatis.apache.org/index.html

    # re: ibatis學習(三)---ibatis與spring的整合  回復  更多評論   

    2014-04-15 00:01 by 最代碼
    最代碼的轉載地址:http://www.zuidaima.com/share/1780211932679168.htm

    # re: ibatis學習(三)---ibatis與spring的整合  回復  更多評論   

    2014-05-13 11:33 by rh
    very good

    # re: ibatis學習(三)---ibatis與spring的整合  回復  更多評論   

    2014-06-27 17:24 by 。。。
    和Hibernate很相似。。

    # re: ibatis學習(三)---ibatis與spring的整合[未登錄]  回復  更多評論   

    2014-11-11 14:48 by 小白
    applicationContext-common.xml文件在哪加載的,怎么沒講明白,沒加載等于沒用啊!

    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 波多野结衣免费视频观看| 久久久久久久久久免免费精品 | 亚洲爱情岛论坛永久| 国产青草视频免费观看97| 67pao强力打造高清免费| 青青草国产免费国产是公开| 亚洲综合久久一本伊伊区| 亚洲AV无码国产精品麻豆天美| 亚洲色偷偷综合亚洲AV伊人| 色吊丝最新永久免费观看网站| 中文字幕亚洲免费无线观看日本| 成人A毛片免费观看网站| 在线91精品亚洲网站精品成人| 国产日本亚洲一区二区三区| 日本久久久久亚洲中字幕| 亚洲精品乱码久久久久久按摩 | 亚洲无av在线中文字幕| 国产精品va无码免费麻豆| 一个人免费观看视频www| 67pao强力打造高清免费| 毛片在线播放免费观看| 三级毛片在线免费观看| 国内精品99亚洲免费高清| 亚洲国产精品成人精品无码区| 亚洲伊人久久大香线蕉综合图片| 亚洲成a人在线看天堂无码| 四虎永久在线精品免费影视| 日韩成人在线免费视频| 免费观看男人免费桶女人视频 | 亚洲中文字幕久久精品无码2021| 麻豆亚洲av熟女国产一区二| 水蜜桃亚洲一二三四在线| 久久精品视频亚洲| 亚洲日本va午夜中文字幕一区| 亚洲成色在线影院| 91精品国产亚洲爽啪在线观看| 777亚洲精品乱码久久久久久 | 成人A毛片免费观看网站| 国内永久免费crm系统z在线 | 亚洲av无码潮喷在线观看| 亚洲VA中文字幕无码毛片|