<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文件在哪加載的,怎么沒講明白,沒加載等于沒用啊!

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


    網站導航:
     
    主站蜘蛛池模板: 免费欧洲美女牲交视频| 日本无吗免费一二区| 亚洲av永久无码精品三区在线4 | 免费在线观看亚洲| 久久久久国产成人精品亚洲午夜| 老司机69精品成免费视频| 亚洲乱码在线播放| 亚洲国产精品成人久久蜜臀 | 8888四色奇米在线观看免费看| 亚洲男人的天堂久久精品| 亚洲Aⅴ无码一区二区二三区软件| 免费精品久久天干天干| 天堂亚洲国产中文在线| 国产亚洲自拍一区| 18禁网站免费无遮挡无码中文| 黄色毛片免费在线观看| 亚洲自偷自拍另类12p| 免费无码AV电影在线观看| ww在线观视频免费观看w| 亚洲精品免费在线| 亚洲成A∨人片天堂网无码| 99re视频精品全部免费| 男女超爽视频免费播放| 亚洲黄色免费观看| 亚洲性在线看高清h片| 韩国免费一级成人毛片| 国产一级高青免费| 亚洲av无码专区国产不乱码| 亚洲高清在线视频| 四虎免费影院4hu永久免费| 亚洲综合免费视频| 久久高潮一级毛片免费| 亚洲欧美成aⅴ人在线观看| 亚洲AV永久精品爱情岛论坛| 免费国产在线观看老王影院| 免费观看无遮挡www的小视频| a高清免费毛片久久| 亚洲精品久久无码| 亚洲AV无码一区二区三区在线| 亚洲精品乱码久久久久66| 国产免费私拍一区二区三区|