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

10.3.1         標準JavaBean實體和映射

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

首先來看寵物店中的一個領域對象(它是一個標準的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實現

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

代碼10.15  sql-map-config.xml

 

<sqlMapConfig>

  ...

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

  ...

</sqlMapConfig>

在寵物店中,該文件僅包含了所有領域對象的映射文件,而挪走了關于IBatis的事務和數據源配置(即IBatis配置文件中的transactionManager元素和它的子元素dataSource)。

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

在寵物店中,持久和數據訪問都是通過DAO來實現的。對于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);

  }

  ...

}

上述代碼中出現了Spring提供的IBatis DAO支持類和獲取SqlMapClientTemplate的父類模板方法,這和JdbcDaoSupportJdbcTemplate的使用具有一致的概念。并且,這些操作都統一的拋出Spring的通用數據訪問異常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>

 

  <!-- 相關數據源和事務管理的定義 -->

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

可以發現,Spring在上述文件中分別配置了數據源和事務管理的策略,其中挪去了原先在IBatis文件中的配置。

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

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

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

10.3.4  添加聲明式事務管理

以上的IBatis DAO可以很自方便地被注射到相應的業務對象,并參與到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>

  <!-- 業務對象定義 -->

  ...

  <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所提供的模板和支持類。



久久不醉