zoukankan      html  css  js  c++  java
  • Spring对IBatis的整合

    10.3 Spring对IBatis的支持  
         Spring对IBatis提供了完善的内建支持。使用Spring提供的IBatis辅助类,可以大大简化原有的IBatis访问代码。这些辅助类位于org.springframework.orm.ibatis包下,目前Spring可同时支持IBatis1.3.x和2.0。 此外,针对IBatis,Spring也提供了和JdbcTemplate一致的异常处理方式

    10.3.1 标准JavaBean实体和映射
         Spring宠物店非常典型的展现了Spring和IBatis的整合,下文将围绕宠物店展开介绍。
    首先来看宠物店中的一个领域对象(它是一个标准的JavaBean)和它的映射文件,如代码10.13~10.14所示。

    代码10.13  Product.java(JavaBean)
    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的父类模板方法,这和JdbcDaoSupport及JdbcTemplate的使用具有一致的概念。并且,这些操作都统一的抛出Spring的通用数据访问异常DataAccessException。
    注意:在早期的IBatis1.3.x版本中Dao支持类和模板类分别被命名为SqlMapDaoSupport和SqlMapTemplate,在使用时不要混淆。

    10.3.3  关键整合点:Spring配置文件
    有了以上的DAO组件后,来看一下Spring的配置,这是一个关键的整合点,如代码10.17所示。
    代码10.17  dataAccessContext-local.xml(Spring配置文件)
    <?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提供的iBatis的SqlMap配置-->
      <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 IoC和IBatis的整合了,当然也可以通过编程的方式来使用Spring所提供的模板和支持类。

         转自:http://book.csdn.net/bookfiles/92/100922653.shtml

  • 相关阅读:
    webpack实现开发、测试、生产等环境的打包切换
    Python报错
    WGAN将数值限制在一定范围内 Python代码 tf.clip_by_value(p, -0.01, 0.01))
    cmd 进入指定文件夹
    Wasserstein 距离
    MSE(均方误差)、RMSE (均方根误差)、MAE (平均绝对误差)
    inf
    plt画log图
    KL散度与JS散度
    安装指定版本的第三方包
  • 原文地址:https://www.cnblogs.com/kelin1314/p/1369864.html
Copyright © 2011-2022 走看看