<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 :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理
     

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

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

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

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

        <!-- 配置事務(wù)特性 -->

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

            <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>

       

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

        <aop:config>

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

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

        </aop:config>

    這些事務(wù)都是聲明在業(yè)務(wù)邏輯層的對象上的。

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

        <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之后,數(shù)據(jù)源的配置移植到了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>

    這就是所有需要注意的問題了,此后就可以在業(yè)務(wù)邏輯層調(diào)用DAO對象了!


    評論

    # re: ibatis學(xué)習(xí)(三)---ibatis與spring的整合  回復(fù)  更多評論   

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

    # re: ibatis學(xué)習(xí)(三)---ibatis與spring的整合  回復(fù)  更多評論   

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

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

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

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

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

    # re: ibatis學(xué)習(xí)(三)---ibatis與spring的整合  回復(fù)  更多評論   

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

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

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

    # re: ibatis學(xué)習(xí)(三)---ibatis與spring的整合  回復(fù)  更多評論   

    2009-08-13 16:32 by jadmin
    很好,學(xué)習(xí)了

    # re: ibatis學(xué)習(xí)(三)---ibatis與spring的整合  回復(fù)  更多評論   

    2009-08-15 23:15 by 匹馬單槍
    好帖, 學(xué)習(xí)了!

    # re: ibatis學(xué)習(xí)(三)---ibatis與spring的整合  回復(fù)  更多評論   

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

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

    2010-04-28 11:10 by test
    autocommit 如果不設(shè)置為false, 事務(wù)有用么

    # re: ibatis學(xué)習(xí)(三)---ibatis與spring的整合  回復(fù)  更多評論   

    2010-11-01 13:42 by rr
    sqlMapClient里應(yīng)該注入dataSource

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

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

    # re: ibatis學(xué)習(xí)(三)---ibatis與spring的整合  回復(fù)  更多評論   

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

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

    }


    出錯了

    # re: ibatis學(xué)習(xí)(三)---ibatis與spring的整合  回復(fù)  更多評論   

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

    # re: ibatis學(xué)習(xí)(三)---ibatis與spring的整合  回復(fù)  更多評論   

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

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

    2012-10-09 17:22 by 1
    1

    # re: ibatis學(xué)習(xí)(三)---ibatis與spring的整合呃呃呃  回復(fù)  更多評論   

    2013-04-11 15:11 by 恩恩
    地對地導(dǎo)彈

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

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

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

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

    # re: ibatis學(xué)習(xí)(三)---ibatis與spring的整合  回復(fù)  更多評論   

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

    # re: ibatis學(xué)習(xí)(三)---ibatis與spring的整合  回復(fù)  更多評論   

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

    近期項目用到 iBatis,所以需要學(xué)習(xí)iBatis,下面是總結(jié)幾個不錯學(xué)習(xí)網(wǎng)站給大家學(xué)習(xí)參考:

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

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

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

    # re: ibatis學(xué)習(xí)(三)---ibatis與spring的整合  回復(fù)  更多評論   

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

    # re: ibatis學(xué)習(xí)(三)---ibatis與spring的整合  回復(fù)  更多評論   

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

    # re: ibatis學(xué)習(xí)(三)---ibatis與spring的整合  回復(fù)  更多評論   

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

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

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

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


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 亚洲日韩精品无码专区加勒比☆| 超清首页国产亚洲丝袜| 亚洲成av人片天堂网| 一级毛片a免费播放王色电影| 三年片在线观看免费大全| 久久亚洲中文字幕精品有坂深雪 | 亚洲成在人线aⅴ免费毛片| 国产成人yy免费视频| 亚洲人xxx日本人18| 成人免费的性色视频| 亚洲网红精品大秀在线观看| 亚洲国产美女在线观看| 青青青免费国产在线视频小草| 亚洲国产精品婷婷久久| 中文字幕一区二区免费| 亚洲成人高清在线| 豆国产96在线|亚洲| 永久免费无码网站在线观看| 中文字幕无码精品亚洲资源网久久 | 国产成人精品日本亚洲专一区| 四虎影视在线影院在线观看免费视频| 国产AV无码专区亚洲AV漫画| 青青操免费在线视频| 亚洲乱码国产一区三区| 华人在线精品免费观看| 亚洲国产精品一区二区久久hs | 在线免费观看h片| 亚洲国产美女精品久久久久∴| www成人免费观看网站| 亚洲中文字幕无码不卡电影| 黄视频在线观看免费| 亚洲国产精品久久久天堂| 久久爰www免费人成| 亚洲大片免费观看| 久久精品视频免费看| 中文字幕亚洲精品资源网| 国产h肉在线视频免费观看| 成人亚洲国产va天堂| 女人被男人桶得好爽免费视频| 含羞草国产亚洲精品岁国产精品| 四虎国产精品免费视|