`
宏基小键盘
  • 浏览: 8116 次
文章分类
社区版块
存档分类
最新评论

spring ibatis clob

阅读更多

iBATIS作为一个易于易用的orm(sql mapping)工具,已经广泛应用在国内的大量的项目中,成熟的iBATIS2已经为社区服务了三年之久,在iBATIS2.3.0中已经废弃了其自带的DAO的框架,转而推荐Spring 提供的ibatis support dao框架,从而得到所有依赖注入,bean管理和template以及声明式事务带来的好处。

本文就在使用过程中Spring DAO + iBATIS + Oracle Clob(Blob)存取时的一些问题的经验总结。

一、开发、测试环境描述

SUN JDK1.4.2
eclipse3.2.1
tomcat 5.0.28
spring-2.0.5.jar
ibatis-2.3.0.677.jar
ojdbc14-10.2.0.3.jar(thin)
commons-dbcp链接池

二、布署环境描述

IBM WebSphere 6.0.2.17

三、数据库描述

Oracle 10.2.0.2.0
采用thin模式连接

四、使用iBATIS自带的clobTypeHanle


读取:将clob字段对应的domain属性配置为String,不需做任何配置
insert,update,时不时出现问题,后采用变态方法,将clob字段移到insert语句的最后,将clob字段移到update的最前位置,问题解决,采用的内联的parametermap

<insert id="insertNews" parameterClass="newsForm">  
<selectKey resultClass="long" keyProperty="id">select mmt_seq.nextval as id from dualselectKey>  
 
    insert into mmt_news_main(  
    id,   
    title,   
    info_source,   
    mod_index,   
    craft_index,   
    p_index,   
    p_name,   
    lang_type,   
    title_short,   
    title_deck,   
    brief,   
    add_date,  
    content  
    )values(  
    #id#,   
    #title#,   
    #info_source#,   
    #mod_index#,   
    #craft_index#,   
    #p_index#,   
    #p_name#,   
    #lang_type#,   
    #title_short#,   
    #title_deck#,   
    #brief#,   
    sysdate,  
    #content,javaType=java.lang.String,jdbcType=CLOB#  
    )  
 ]]>  
</insert>  


<update id="updateNews" parameterClass="newsForm">  
     
        update mmt_news_main set  
        content=#content,javaType=java.lang.String,jdbcType=CLOB#,  
        mod_index=#mod_index#,   
        craft_index=#craft_index#,   
        p_index=#p_index#,   
        p_name=#p_name#,   
        lang_type=#lang_type#,  
        title=#title#,   
        info_source=#info_source#,    
        title_short=#title_short#,   
        title_deck=#title_deck#,   
        brief=#brief#             
        where id=#id#  
    ]]>  
<update>  

但没有测试过多个clob字段的情况,不存在4000字符的问题。
注意,在插入语句中clob字段的java类型为java.io.StringReader

五、使用spring自带的OracleLobHandler


看了一下spring的源码中的注释
位置:org.springframework.jdbc.support.lob.OracleLobHandler
/**
 * LobHandler implementation for Oracle databases. Uses proprietary API to
 * create oracle.sql.BLOB and oracle.sql.CLOB
 * instances, as necessary when working with Oracle's JDBC driver.
 * Note that this LobHandler requires Oracle JDBC driver 9i or higher!
 *
 *

While most databases are able to work with DefaultLobHandler, Oracle just
 * accepts Blob/Clob instances created via its own proprietary BLOB/CLOB API,
 * and additionally doesn't accept large streams for PreparedStatement's
 * corresponding setter methods. Therefore, you need to use a strategy like
 * this LobHandler implementation.
 *
 *

Needs to work on a native JDBC Connection, to be able to cast it to
 * oracle.jdbc.OracleConnection. If you pass in Connections from
 * a connection pool (the usual case in a J2EE environment), you need to set
 * an appropriate NativeJdbcExtractor to allow for automatical retrieval of
 * the underlying native JDBC Connection. LobHandler and NativeJdbcExtractor
 * are separate concerns, therefore they are represented by separate strategy
 * interfaces.
 *
 *

Coded via reflection to avoid dependencies on Oracle classes.
 * Even reads in Oracle constants via reflection because of different Oracle
 * drivers (classes12, ojdbc14) having different constant values! As this
 * LobHandler initializes Oracle classes on instantiation, do not define this
 * as eager-initializing singleton if you do not want to depend on the Oracle
 * JAR being in the class path: use "lazy-init=true" to avoid this issue.
 *
 * @author Juergen Hoeller
 * @since 04.12.2003
 * @see #setNativeJdbcExtractor
 * @see oracle.sql.BLOB
 * @see oracle.sql.CLOB
 */

大体的意思是:oracle使用私有的API产生oracle.sql.BLOB和oracle.sql.CLOB实例,oracle只接受其自己API产生的实例,并且在PreparedStatement中不支持大的数据流。

此实现类需要一个原生的可以被转换成oracle.jdbc.OracleConnection的链接,所以在使用连接池的时候,就需要一个原生JDBC的转换器。

原来如此,马上配置, 快速搞定,在测试,生产环境中都没有问题,sqlMap也不需要注意clob字段的位置,以下是配置:

1、sqlMapConfig

xml 代码
  1. <!---->xml version="1.0" encoding="UTF-8"?>  
  2. <!---->
  3.         PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"    
  4.         "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">  
  5.   
  6. <sqlMapConfig>  
  7.     <settings cacheModelsEnabled="true" enhancementEnabled="true" lazyLoadingEnabled="true"  
  8.         useStatementNamespaces="false" statementCachingEnabled="true" classInfoCacheEnabled="true" />  
  9.   
  10.     <typeHandler jdbcType="BLOB" javaType="[B"  
  11.         callback="org.springframework.orm.ibatis.support.BlobByteArrayTypeHandler" />  
  12.     <typeHandler jdbcType="CLOB" javaType="java.lang.String"  
  13.         callback="org.springframework.orm.ibatis.support.ClobStringTypeHandler" />  
  14.   
  15.     <sqlMap resource="com/xx/ssi/dao/ibatis/maps/SysModule.xml" />  
  16.   
  17. sqlMapConfig>  

2、spring配置(部分)

xml 代码
  1. <jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/dsmmt" />  
  2.   
  3. <!---->  
  4. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  5.     <property name="dataSource" ref="dataSource" />  
  6. bean>  
  7.   
  8. <!---->  
  9. <!---->  
  10.   
  11. <bean id="nativeJdbcExtractor" class="org.springframework.jdbc.support.nativejdbc.CommonsDbcpNativeJdbcExtractor"  
  12.     lazy-init="true" />  
  13. <!---->  
  14.   
  15. <!---->  
  16. <!---->  
  17. <bean id="oracleLobHandler" class="org.springframework.jdbc.support.lob.OracleLobHandler" lazy-init="true">  
  18.     <property name="nativeJdbcExtractor">  
  19.         <ref local="nativeJdbcExtractor" />  
  20.     property>  
  21. bean>  
  22.   
  23. <!---->  
  24. <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">  
  25.     <property name="configLocation" value="classpath:com/ahtec/ssi/config/ibatis/sql-map-config.xml" />  
  26.     <property name="dataSource" ref="dataSource" />  
  27.     <property name="lobHandler">  
  28.         <ref local="oracleLobHandler" />  
  29.     property>  
  30. bean>  


在发布环境中,要将nativeJdbcExtractor  换成 org.springframework.jdbc.support.nativejdbc.WebSphereNativeJdbcExtractor
留意log4j日志,此时的clob字段的java类型为oracle.sql.Clob
Spring内置提供的NativeJdbcExtractor转换器有:
C3P0NativeJdbcExtractor
CommonsDbcpNativeJdbcExtractor
JBossNativeJdbcExtractor
NativeJdbcExtractor
NativeJdbcExtractorAdapter
SimpleNativeJdbcExtractor
WebLogicNativeJdbcExtractor
WebSphereNativeJdbcExtractor
XAPoolNativeJdbcExtractor
基本上够用了。
位于org\springframework\jdbc\support\nativejdbc下

六、最后也总结一下:

ibatis 2.0.9 + (最新的是iBATIS-2.3.0.667)
oracle 10g driver + (最新的是ojdbc14-10.2.0.3.jar)
使用spring 提供的dao框架,按如上配置,不同的链接池采用不同的NativeJdbcExtractor
不需要写显式的parameterMap(我测试了也没有成功),简洁好用的inline-parameterMap应该是首选,配合abator等自动化工具,ibatis照样能达到快速开发。


以上文字为原创,并在所述环境中测试成功,若有不妥或未尽之处,请批正。

分享到:
评论
5 楼 viway 2007-07-06  
支持一下,非常好!
4 楼 xufei0110 2007-05-23  
太棒了 解决了我的问题
3 楼 ddandyy 2007-05-17  
可能是认为应该新手吧

格式有点乱
2 楼 宏基小键盘 2007-05-17  
精华帖 (0) :: 良好帖 (8) :: 新手帖 (0) :: 隐藏帖 (5)

居然5个隐藏。
1 楼 zllwang 2007-05-10  
顶您个肺啊
谢谢大师

相关推荐

    springmvc-ibatis环境搭建

    springmvc ibatis 整合, 解决BLOB,CLOB等大字段的问题,内置查询缓存 和解决SpringMVC 返回JSON下载,乱码等问题 内部并没有实际的项目,只是一个搭建好的环境,方便较少大家时间, 并提供了一个DEMO ,紧为不了解...

    spring oracle blob

    &lt;typeHandler jdbcType="CLOB" javaType="java.lang.String" callback="org.springframework.orm.ibatis.support.ClobStringTypeHandler"/&gt; ``` 这里的配置指定了BLOB类型对应的Java类型为`byte[]`,并通过`...

    Spring中文帮助文档

    处理BLOB 和 CLOB对象 11.7.3. 在IN语句中传入一组参数值 11.7.4. 处理复杂类型的存储过程调用 12. 使用ORM工具进行数据访问 12.1. 简介 12.2. Hibernate 12.2.1. 资源管理 12.2.2. 在Spring容器中创建 ...

    Spring API

    处理BLOB 和 CLOB对象 11.7.3. 在IN语句中传入一组参数值 11.7.4. 处理复杂类型的存储过程调用 12. 使用ORM工具进行数据访问 12.1. 简介 12.2. Hibernate 12.2.1. 资源管理 12.2.2. 在Spring容器中创建 ...

    使用SSH构建Web应用系统

    1. **基本框架结构**:SSH框架通常结合Filter、Struts、Spring以及Hibernate/iBatis,以实现灵活的业务逻辑处理和数据持久化。 2. **展现层**:Struts作为MVC架构中的视图层,负责接收请求和展示结果。 3. **服务...

    oracle基础练习.docx

    SpringMVC、Spring和iBatis组合的SSI框架也是常见的Web开发架构。 配置Oracle监听器是连接数据库的关键步骤,监听器配置文件(listener.ora)定义了主机名、端口和数据库服务名(SID)。如果主机名与监听器配置中的...

    DBKING使用指南

     这类工具已经很多了,比如大家耳熟能详的Hibernate、iBatis,包括Apache的DBUtils、Spring的JdbcTemplate。为什么我们还要推出db-unifier这样一个功能类似的东西呢?  这些工具的主要目的都是对JDBC进行包装...

    Oracle数据库的基础使用

    开发框架如SSH(Struts+Spring+Hibernate)和SSI(SpringMVC+Spring+IBatis)经常与Oracle配合使用。 配置监听器是Oracle数据库管理的重要环节,监听器文件(如Oracle.Listener和OracleService)负责处理客户端连接...

Global site tag (gtag.js) - Google Analytics