<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)航:
     
    主站蜘蛛池模板: 免费va人成视频网站全| 亚洲另类图片另类电影| 热99re久久免费视精品频软件| 国产精品视频全国免费观看| 亚洲日韩一区精品射精| 久久久亚洲欧洲日产国码是AV| 久久亚洲高清综合| 国产免费午夜a无码v视频| 免费在线观看毛片| 毛片大全免费观看| 四虎在线成人免费网站| 最近国语视频在线观看免费播放| 极品色天使在线婷婷天堂亚洲| 亚洲午夜在线播放| 亚洲欧洲日本国产| 亚洲综合精品香蕉久久网97| 好男人www免费高清视频在线| 9420免费高清在线视频| 在线看片免费人成视频播| 亚洲国产夜色在线观看| 精品亚洲A∨无码一区二区三区| 亚洲香蕉成人AV网站在线观看| 3d成人免费动漫在线观看| 亚洲中文无码亚洲人成影院| 亚洲神级电影国语版| 亚洲人成电影在在线观看网色| 亚洲不卡av不卡一区二区| 在线永久免费观看黄网站| 99视频精品全部免费观看| 91免费国产视频| 三上悠亚电影全集免费| 日韩av无码免费播放| 老司机精品免费视频| 91成人免费观看在线观看| 免费毛片在线看不用播放器| 国内少妇偷人精品视频免费| 黄网站免费在线观看| 亚洲午夜免费视频| 亚洲黄色免费在线观看| 114一级毛片免费| 午夜视频免费观看|