Terry.Li-彬

          虛其心,可解天下之問;專其心,可治天下之學;靜其心,可悟天下之理;恒其心,可成天下之業(yè)。

            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            143 隨筆 :: 344 文章 :: 130 評論 :: 0 Trackbacks
          10.3 Spring對IBatis的支持 
          http://book.csdn.net/ 2006-8-15 16:27:00
          圖書導讀
          IT 游戲 開發(fā)人才專業(yè)招聘網(wǎng)站
          每日上萬個IT簡歷更新,快捷有效的招聘求職
          www.jobg.cn
          非常4+1大學生實訓計劃,首付6800
          4個月的全套Java技術體系的實訓, 1個月的企業(yè)大型真實項目的實戰(zhàn)開發(fā)。
          www.kettas.com.cn/
          近距離接觸開源ERP
          開源帶來了哪些方面的好處? 開源后的運營方向又是怎樣?
          live.csdn.net

          SpringIBatis提供了完善的內(nèi)建支持。使用Spring提供的IBatis輔助類,可以大大簡化原有的IBatis訪問代碼。這些輔助類位于org.springframework.orm.ibatis包下,目前Spring可同時支持IBatis1.3.x2.0 此外,針對IBatisSpring也提供了和JdbcTemplate一致的異常處理方式

          10.3.1         標準JavaBean實體和映射

          Spring寵物店非常典型的展現(xiàn)了SpringIBatis的整合,下文將圍繞寵物店展開介紹。

          首先來看寵物店中的一個領域?qū)ο螅ㄋ且粋€標準的JavaBean)和它的映射文件,如代碼10.13~10.14所示。

          代碼10.13  Product.java

          public class Product implements Serializable {

            private String productId;

            private String categoryId;

            private String name;

            private String description;

           省略getter/setter...

          }

          代碼10.14  Product.xml

          <sqlMap namespace="Product">

            ...

            <resultMap id="result"

           class="org.springframework.samples.jpetstore.domain.Product">

              <result property="productId" column="productid" columnIndex="1"/>

              ...

            </resultMap>

            <select id="getProduct" resultMap="result">

              select productid, name, descn, category from product where productid = #value#

            </select>

            <select id="getProductListByCategory" resultMap="result">

              select productid, name, descn, category from product where category = #value#

            </select>

              ...

          </sqlMap>

          10.3.2 銜接IBatis配置和DAO實現(xiàn)

          接著給出IBatis的基本配置文件,如代碼10.15所示。

          代碼10.15  sql-map-config.xml

          <sqlMapConfig>

            ...

            <sqlMap resource="org/springframework/samples/jpetstore/dao/ibatis/maps/Product.xml"/>

            ...

          </sqlMapConfig>

          在寵物店中,該文件僅包含了所有領域?qū)ο蟮挠成湮募沧吡岁P于IBatis的事務和數(shù)據(jù)源配置(即IBatis配置文件中的transactionManager元素和它的子元素dataSource)。

          注意:在稍后將要給出的Spring配置文件中接手了這些配置,這是一個整合點。

          在寵物店中,持久和數(shù)據(jù)訪問都是通過DAO來實現(xiàn)的。對于Product,存在一個與其對應的SqlMapProductDao,如代碼10.16所示。

          代碼10.16  SqlMapProductDao.java

          package org.springframework.samples.jpetstore.dao.ibatis;

          import java.util.ArrayList;

          import java.util.List;

          import java.util.StringTokenizer;

          import org.springframework.dao.DataAccessException;

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

          import org.springframework.samples.jpetstore.dao.ProductDao;

          import org.springframework.samples.jpetstore.domain.Product;

          public class SqlMapProductDao

            extends SqlMapClientDaoSupport

            implements ProductDao {

            public List getProductListByCategory(String categoryId)

              throws DataAccessException {

              return getSqlMapClientTemplate().queryForList("getProductListByCategory",

           categoryId);

            }

            public Product getProduct(String productId)

              throws DataAccessException {

              return (Product) getSqlMapClientTemplate().queryForObject("getProduct", productId);

            }

            ...

          }

          上述代碼中出現(xiàn)了Spring提供的IBatis DAO支持類和獲取SqlMapClientTemplate的父類模板方法,這和JdbcDaoSupportJdbcTemplate的使用具有一致的概念。并且,這些操作都統(tǒng)一的拋出Spring的通用數(shù)據(jù)訪問異常DataAccessException

          注意:在早期的IBatis1.3.x版本中Dao支持類和模板類分別被命名為SqlMapDaoSupportSqlMapTemplate,在使用時不要混淆。

          10.3.3 關鍵整合點:Spring配置文件

          有了以上的DAO組件后,來看一下Spring的配置,這是一個關鍵的整合點,如代碼10.17所示。

          代碼10.17  dataAccessContext-local.xml

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

          <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"

          "http://www.springframework.org/dtd/spring-beans.dtd">

          <beans>

            <!-- 相關數(shù)據(jù)源和事務管理的定義 -->

            <bean id="dataSource"

          class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">

              <property name="driverClassName" value="${jdbc.driverClassName}"/>

              <property name="url" value="${jdbc.url}"/>

              <property name="username" value="${jdbc.username}"/>

              <property name="password" value="${jdbc.password}"/>

            </bean>

            <!-- Transaction manager for a single JDBC DataSource -->

            <bean id="transactionManager"

            class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

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

            </bean>

            <!-- Spring提供的iBatisSqlMap配置-->

            <bean id="sqlMapClient"

          class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">

              <property name="configLocation" value="WEB-INF/sql-map-config.xml"/>

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

            </bean>

            <!-- DAO定義-->

            ...

            <bean id="productDao"

          class="org.springframework.samples.jpetstore.dao.ibatis.SqlMapProductDao">

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

            </bean>

            ...

          </beans>

          可以發(fā)現(xiàn),Spring在上述文件中分別配置了數(shù)據(jù)源和事務管理的策略,其中挪去了原先在IBatis文件中的配置。

          說明:這樣做的好處是可以通過Spring IoC容器統(tǒng)一的管理資源,在稍后還可以看到,Spring提供的聲明性事務管理就是借助于統(tǒng)一的數(shù)據(jù)源和事務管理配置。

          SqlMapClientFactoryBean又是一個工廠bean,它暴露了兩個關鍵屬性用于注射IBatis配置文件和相關的數(shù)據(jù)源。在工廠內(nèi)部,通過讀取IBatis配置文件,Spring會創(chuàng)建出IBatis的核心組件SqlMapClient,并向相關的DAO進行注射。

          SqlMapProductDao繼承了SqlMapClientDaoSupport,后者暴露出一個sqlMapClient屬性,用于接受Spring的注射。SqlMapClientDaoSupport會對其中封裝的SqlMapClientTemplate做相應的設置,所以DAO子類便可在取用SqlMapClientTemplate時正常地工作了。

          10.3.4 添加聲明式事務管理

          以上的IBatis DAO可以很自方便地被注射到相應的業(yè)務對象,并參與到Spring提供的聲明性事務中,配置如代碼10.18所示。

          代碼10.18   applicationContext.xml

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

          <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"

          "http://www.springframework.org/dtd/spring-beans.dtd">

          <beans>

            <!-- 通用屬性文件定義 -->

            <bean id="propertyConfigurer"

            class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

              <property name="locations">

                <list>

                   ...

                  <value>WEB-INF/jdbc.properties</value>

                </list>

              </property>

            </bean>

            <!-- 業(yè)務對象定義 -->

            ...

            <bean id="baseTransactionProxy"

          class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"

          abstract="true">

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

              <property name="transactionAttributes">

                <props>

                  <prop key="insert*">PROPAGATION_REQUIRED</prop>

                  <prop key="update*">PROPAGATION_REQUIRED</prop>

                  <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>

                </props>

              </property>

            </bean>

            <bean id="petStore" parent="baseTransactionProxy">

              <property name="target">

                <bean class="org.springframework.samples.jpetstore.domain.logic.PetStoreImpl">

                 ...

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

                  ...

                </bean>

            </bean>

          </beans>

          至此就基本完成了Spring IoCIBatis的整合了,當然也可以通過編程的方式來使用Spring所提供的模板和支持類。

          posted on 2007-12-05 15:39 禮物 閱讀(592) 評論(0)  編輯  收藏 所屬分類: ibatis + spring
          主站蜘蛛池模板: 关岭| 上栗县| 青海省| 微博| 福鼎市| 铜川市| 海伦市| 贺州市| 曲靖市| 秭归县| 马关县| 静宁县| 清河县| 尤溪县| 青州市| 沅江市| 济阳县| 龙井市| 龙海市| 深州市| 洮南市| 台北县| 都兰县| 南木林县| 灌南县| 南投市| 阳东县| 武安市| 安康市| 如皋市| 榆中县| 永和县| 株洲市| 东台市| 武穴市| 黄冈市| 眉山市| 金川县| 合作市| 日喀则市| 大城县|