问题:Spring如何支持IBatis

本文介绍Spring框架如何整合轻量级持久层框架IBatis,包括领域对象映射、IBatis配置与DAO实现,以及Spring事务管理配置。
问题:Spring如何支持IBatis
除了对JDBC的支持,Spring还对很多持久层框架提供了优雅的支持。本节中设计师L就将展示Spring是如何支持一款轻量级的开源持久框架IBatis,目前Spring支持IBatis1.3.x和IBatis.2.0,这里假设读者已经比较熟悉IBatis这个框架。
6.5.1  领域对象映射
Spring宠物店示例就是一个最好的关于Spring整合IBatis的示例,接下来,L将围绕宠物店示例展开介绍。首先给出一个宠物店的领域对象和它的相关IBatis映射文件,见例6.41~例6.42。
例6.41:Product.java
package org.springframework.samples.jpetstore.domain;
import java.io.Serializable;
public class Product implements Serializable {
  private String productId;
  private String categoryId;
  private String name;
  private String description;
  public String getProductId() { return productId; }
  public void setProductId(String productId) { this.productId = productId. trim(); }
  public String getCategoryId() { return categoryId; }
  public void setCategoryId(String categoryId) { this.categoryId = categoryId; }
  public String getName() { return name; }
  public void setName(String name) { this.name = name; }
  public String getDescription() { return description; }
  public void setDescription(String description) { this.description = description; }
  public String toString() {
    return getName();
  }
}
例6.42: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>
6.5.2  IBatis配置和DAO
有了领域对象和其配置后,L接着给出了IBatis的基本配置以及相应的DAO代码,见例6.43和例6.45。
例6.43:sql-map-config.xml
<sqlMapConfig>
  ...
  <sqlMap resource="org/springframework/samples/jpetstore/dao/ibatis/maps/ Product.xml"/>
  ...
</sqlMapConfig>
例6.44:ProductDao.java
package org.springframework.samples.jpetstore.dao;
import java.util.List;
import org.springframework.dao.DataAccessException;
import org.springframework.samples.jpetstore.domain.Product;
public interface ProductDao {
  List getProductListByCategory(String categoryId) throws DataAccessException;
  List searchProductList(String keywords) throws DataAccessException;
     Product getProduct(String productId) throws DataAccessException;
}
例6.45: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 DataAccess Exception {
    return getSqlMapClientTemplate().queryForList("getProductListByCategory", categoryId);
  }
  public Product getProduct(String productId) throws DataAccessException {
    return (Product) getSqlMapClientTemplate().queryForObject("getProduct", productId);
  }
  public List searchProductList(String keywords) throws DataAccessException {
    Object parameterObject = new ProductSearch(keywords);
    returngetSqlMapClientTemplate().queryForList("searchProductList", parameterObject);
  }
  public static class ProductSearch {
    private List keywordList = new ArrayList();
    public ProductSearch(String keywords) {
      StringTokenizer splitter = new StringTokenizer(keywords, " ", false);
      while (splitter.hasMoreTokens()) {
        this.keywordList.add("%" + splitter.nextToken() + "%");
      }
    }
    public List getKeywordList() {
      return keywordList;
    }
  }
}
需要说明的是,上述代码中出现了Spring提供的IBatis DAO支持类SqlMapClientDaoSupport。getSqlMapClientTemplate()方法返回类型为SqlMapClientTemplate,SqlMapClientTemplate也提供了一些列便利的模板方法,这点和JdbcDaoSupport及JdbcTemplate非常相似,也显示了Spring框架一致的处理方式。
6.5.3  Spring和事务声明配置
最后,L展示了宠物店中相关的Spring配置,见例6.46。
例6.46: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>
  <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>
同上节中的JDBC示例一样,再给出一份事务声明配置,IBatis的DAO方便地被注入到相关的业务对象,业务对象的相关方法便又可以具有事务特性,见例6.47。
例6.47: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和IBatis的所有整合工作。
 
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值