`
sbpya
  • 浏览: 610043 次
  • 性别: Icon_minigender_1
  • 来自: 杭州,长沙
社区版块
存档分类
最新评论

Ibatis 中的 oracle LOB 字段处理

阅读更多

方法一

来源:http://opensource2.atlassian.com/confluence/oss/display/IBATIS/How+do+I+use+a+BLOB+or+CLOB

Here is an example of how to use the Custom Type Handler (CTH) feature of iBatis with large objects (LOB) such as BLOB's (Binary) and CLOB's (Character). As of release 2.0.9 the iBatis framework has the default CLOB and BLOB type handlers included. The example below was done for Oracle but should work for any database with a well written JDBC driver. Make sure that you do not use the thin driver supplied from Oracle. You need to use the latest ojbc14.jar.

The example below was not the intended way to use CTH's but it works great for me!

First lets take a look at the table.

Report.sql
REPORT {
        id              varchar2(5),
        name            varchar2(25),
        description     varchar2(1000),
        data            BLOB
}

Next we continue by creating a plain old java object (POJO) to represent this table.

Report.java
/*
 * Report.java
 *
 * Created on March 23, 2005, 11:00 AM
 */
package reporting.viewer.domain;

/**
 *
 * @author Nathan Maves
 */
public class Report {
    
    /**
     * Holds value of property id.
     */
    private String id;
    /**
     * Holds value of property name.
     */
    private String name;
    /**
     * Holds value of property description.
     */
    private String id;
    /**
     * Holds value of property data.
     */
    private byte[] data;


    //Standard accessors and mutators

   public byte[] getData() {
       return this.data;
   }

   public void setData(byte[] data) {
       this.data = data;
   }
}

Now that the easy stuff is completed let connect both the database and the POJO together using iBatis.

Report.xml
<typeAlias alias="Report" type="reporting.viewer.domain.Report"/>

<resultMap class="Report" id="ReportResult">
        <result column="id" property="id" />
        <result column="name" property="name" />
        <result column="description" property="description" />
        <result column="data" property="data" jdbcType="BLOB"/>
</resultMap>

<select id="getReportById" parameterClass="string" resultMap="ReportResult">
        SELECT 
            *
        FROM 
            REPORT
        WHERE 
            id = #value#
</select>

<insert id="insertReport" parameterClass="Report">
        INSERT INTO 
            REPORT (
                id, 
                name, 
                description,
                data
                )
            values (
                #id#, 
                #name#, 
                #description#,
                #data#
            )
</insert>

<update id="updateReport" parameterClass="Report">
        UPDATE REPORT set
                name = #name#,
                description = #description#,
                data = #data#
        WHERE
                id = #id#
</update>

As you can see there is nothing special that you need to do.

When working with a CLOB the only that the you need to change is the property in your bean. Just change byte[] to java.lang.String.

Data size bigger than max size for this type: ????

Some of the older jdbc drivers for Oracle have trouble with Strings that are larger then 4k. The first step to correct this issue it to get a jdbc driver from Oracle that is newer then 10g Release 2. This driver will work with both 9i and 10g databases. If you are stuck with an older driver you can try to set a driver property. The property is SetBigStringTryClob=true. If you are using the SimpleDataSource with iBatis use the follow line in the config file.

<property name="Driver.SetBigStringTryClob" value="true"/>

Data size always 86 bytes?

If you find that the length of your byte[] is always 86, check that you have the jdbcType="BLOB" or jdbcType="CLOB" in your result map.

方法二

http://spaces.msn.com/tsaijun/

Spring+Ibatis中插入LOB字段
sql-map-config.xml中
 
<typeHandler jdbcType="BLOB" javaType="[B" callback="org.springframework.orm.ibatis.support.BlobByteArrayTypeHandler"/>
  <typeHandler jdbcType="CLOB" javaType="java.lang.String" callback="org.springframework.orm.ibatis.support.ClobStringTypeHandler"/>
 
Spring中配置
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
  <property name="configLocation"><value>/sql-map-config.xml</value></property>
  <property name="dataSource"><ref local="dataSource"/></property>
  <property name="lobHandler"><ref local="oracleLobHandler"/></property>
 </bean>

 <bean id="nativeJdbcExtractor" class="org.springframework.jdbc.support.nativejdbc.CommonsDbcpNativeJdbcExtractor"
   lazy-init="true"/>
 <!-- LobHandler for Oracle JDBC drivers -->
 <!-- (refers to the NativeJdbcExtractor above to get access to native OracleConnections) -->
 <bean id="oracleLobHandler" class="org.springframework.jdbc.support.lob.OracleLobHandler"
   lazy-init="true">
  <property name="nativeJdbcExtractor"><ref local="nativeJdbcExtractor"/></property>
 </bean>
 
这样Spring会使用LobHandler对Lob字段进行操作。

 

方法三

http://java.mblogger.cn/wuyu/archive/09292004.aspx

ibatis 2.0.5以后提供的TypeHandlerCallback,应该可以解决ibatis操作oracle clob/blob这类字段的问题

自己用了ibatis以后,逛ibatis的forum似乎就成了每日的功课了。今天在论坛上翻到一个贴

http://sourceforge.net/forum/forum.php?thread_id=1120723&forum_id=206694

因为是在家,勿勿做了一个的CLOB读测试,等明天上班再全面测试好了。

自己定义了一个spring.ORM.ibatis.OracleClobTypeHandlerCallback,因为只测试读,就只实现了getResult方法

public class OracleClobTypeHandlerCallback implements TypeHandlerCallback {

  public Object getResult(ResultGetter getter) throws SQLException {
    if (logger.isDebugEnabled()) logger.debug(getClass().getName());
    CLOB clob = (CLOB) getter.getClob();
    if (clob == null || clob.length() == 0) {
      if (logger.isDebugEnabled()) logger.debug("CLOB对象为空");
      return "";
    }
    else
      return clob.getSubString((long) 1, (int) clob.length());
  }

做了个load的sqlmap文件

<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN"
    "
http://www.ibatis.com/dtd/sql-map-2.dtd">
<sqlMap namespace="Clob">
    <resultMap class="spring.ORM.Clob" id="result">
        <result property="id" column="CONTENT_ID" javaType="long"
            jdbcType="BIGINT"/>
        <result property="value" column="S_CONTENT"
            typeHandler="spring.ORM.ibatis.OracleClobTypeHandlerCallback"/>
    </resultMap>
    <select id="load" parameterClass="long" resultMap="result"> <![CDATA[
        SELECT
            t.CONTENT_ID,
            t.S_CONTENT
        FROM T_INFOCONTENT  t
        WHERE t.CONTENT_ID=#value#
        ]]> </select>
</sqlMap>

随便写了段测试代码:

    ClobDAOIface dao = (ClobDAOIface) ctx.getBean("ClobDAOProxy");
    Clob clob=dao.loadClob(113499191529712L);
    System.out.println(clob.getValue());

哈哈!看来有望解决CLOB/BLOB的问题了,可以不再调jdbc来单独操作clob/blob字段,ibatis提供的cache、延迟加载等等东西也能用了,爽!

方法四

http://www.matrix.org.cn/blog/zhenggc/

spring+ibatis+oracle clob完全搞定

算是伤经动骨了,几乎翻了一遍ibatis和spring相关的代码,先说搞定的情况,
spring115+ibatis216(215有问题,216已经修复,原来用215,现在已经替换,是ibatis在buglist上的191号问题)oracle9i2。oracle的驱动用了10g的,感谢上次在qq上那个(谁?忘记名字了,不好意思)传给我的10g的驱动。
基本思路是用ibatis提供的com.ibatis.sqlmap.engine.type.ClobTypeHandlerCallback
在ibatis的sqlmap中配置resultMap和parameterMap,
Oracle9i的驱动只能插入很少的字符,不清楚为什么,读取基本没有问题。
Oracle8i的数据库和驱动根本就没有成功过。
spring的那个org.springframework.jdbc.support.lob.LobHandler
和org.springframework.jdbc.support.lob.OracleLobHandler,
和org.springframework.orm.ibatis.support.ClobStringTypeHandler只能读取,写的时候居然判断Connection的类型是不是Oracle.jdbc.OracleConnection,不适合dbcp之类的连接池,不清楚后期版本有没有改动。spring的org.springframework.jdbc.support.lob.OracleLobHandler就是等于getString和setString,一点用处都没有,呵呵。
ibatis215的那个com.ibatis.sqlmap.engine.type.ClobTypeHandlerCallback居然在set reader以后,又set了一次string,真够害人的。216已经修改bug。
没有大堆代码是没有办法描述这个东西的,大致记录一下,希望可以对人有帮助。

分享到:
评论

相关推荐

    ibatis 读取oracle clob类型

    ibatis 读取oracle clob类型

    ibatis连接oracle所需的jar包

    下面我们将详细探讨这些关键的jar包以及它们在Ibatis与Oracle连接中的作用。 1. **ibatis-2.3.3.jar**: 这是Ibatis框架的核心库,包含了Ibatis的主要组件和API。它提供了SQL映射文件解析、事务管理、数据源配置等...

    ibatis+oracle实例

    2. **事务管理**:Ibatis提供了对数据库事务的控制,可以配合Oracle的ACID特性处理复杂的事务场景。 3. **存储过程调用**:Ibatis可以通过`&lt;select&gt;`、`&lt;procedure&gt;`元素调用Oracle的存储过程,增强业务逻辑的封装...

    ibatis调用oracle存储过程分页

    ibatis调用oracle存储过程分页

    ibatis调用oracle存储过程

    在IT领域,特别是数据库操作与Java开发中,利用ibatis框架调用Oracle存储过程是一个常见的需求,这不仅能够提升代码的执行效率,还能增强应用程序的安全性。本文将深入解析ibatis如何与Oracle数据库中的存储过程进行...

    Ibatis+Oracle(含对应数据库sql) 源码

    5. **事务管理**:学习如何在Ibatis中处理事务,包括手动提交和回滚。 6. **错误调试**:通过实际操作,学习如何解决可能出现的数据库连接、SQL语法、映射文件配置等问题。 这个资源为初学者提供了一个从理论到...

    spring+ibatis+oracle分页缓存源码

    在Oracle中,我们可以利用ROWNUM伪列进行分页查询。例如,通过WHERE ROWNUM BETWEEN start AND end限制返回的结果集范围。同时,可以结合索引来进一步优化查询性能,尤其是在处理大数据量时。此外,Oracle的...

    Spring MVC+ibatis+oracle框架整合

    本项目整合了Spring MVC、iBatis和Oracle数据库,这三个组件都是企业级应用开发中的重要工具。接下来,我们将深入探讨这三个技术以及它们如何协同工作。 **Spring MVC框架** Spring MVC是Spring框架的一部分,是一...

    Ibatis调用Oracle存储过程返回自定义类型

    本文将深入探讨如何使用Ibatis框架来调用Oracle数据库中的存储过程,并实现返回自定义类型的处理方法。 #### Oracle自定义类型简介 Oracle支持用户自定义数据类型,这为复杂数据结构的应用提供了极大的便利。...

    iBATIS操作Oracle CLOB数据

    在Oracle中,CLOB通常用于存储大段的文本,如文章、报告或者XML文档。 iBATIS是一个Java库,它允许开发者将SQL语句与Java代码分离,提供了一种比JDBC更简单的数据访问方法。iBATIS的配置文件和映射文件中,我们可以...

    spring mvc+ibatis+oracle单表增删改(有包)

    在本项目中,我们主要探讨的是如何利用Spring MVC、iBATIS和Oracle数据库来实现一个基本的单表操作,包括增、删、改等常见功能。Spring MVC是Spring框架的一个模块,专门处理Web应用程序的模型-视图-控制器(MVC)...

    ibatis + oracle 增删改查例子

    本示例“ibatis + oracle 增删改查例子”将向我们展示如何利用这两个工具进行基本的数据操作。 首先,`iBatis`的核心是SQL Map配置文件,它包含了SQL语句和映射规则。在`MyEclipse`中导入这个项目后,你需要找到并...

    struts2+spring+ibatis+oracle整合的登陆系统

    总结来说,"struts2+spring+ibatis+oracle整合的登陆系统"是一个综合性的Web应用示例,它展示了如何利用Struts2处理用户交互,Spring管理依赖和事务,iBatis执行数据库操作,以及Oracle存储数据。这样的整合方案为...

    SSI整合,有ibatis连接oracle的分页,ajax等技术

    而Ibatis是一个轻量级的Java持久层框架,它允许开发者将SQL语句直接写在配置文件中,与ORM(对象关系映射)框架相比,提供了更大的灵活性。 在这个项目中,"SSI整合"指的是开发者将SSI技术应用到项目中,可能是在...

    jar框架包2 ibatis spring oracle mysql

    本压缩包“jar框架包2”聚焦于四大关键技术:Ibatis、Spring、Oracle和MySQL,这四者在企业级Java应用开发中扮演着至关重要的角色。 Ibatis,全名MyBatis,是一个优秀的持久层框架,它支持定制化SQL、存储过程以及...

    struts2+spring+ibatis+oracle+分页搜索+上传附件实例

    Struts2、Spring、iBatis以及Oracle是Java Web开发中的四大核心组件,它们共同构建了一个强大且灵活的后端架构。在这个实例中,我们将会深入探讨这些技术如何协同工作,实现分页搜索功能和上传附件操作。 1. **...

    ibatis下oracle树查询

    在本话题中,我们将探讨如何在使用iBatis框架与Oracle数据库时实现树形查询。 首先,iBatis是一个优秀的持久层框架,它允许开发者将SQL语句直接写在XML配置文件或者注解中,提供了比传统JDBC更高级的抽象层,使得...

    Ibatis.net+ oracle 简单事例

    添加对Oracle 的引用,如Oracle.DataAccess.Client,然后将Ibatis.net 的相关配置文件和Mapper XML文件加入到项目中。使用VS2008的调试工具,可以快速测试SQL语句和业务逻辑。 【示例应用】 "IbatisTet" 可能是一个...

    ibatis调用oracle的函数,存储过程的方法

    本篇文章将详细介绍如何在Ibatis中调用Oracle的函数和存储过程。 首先,理解基本概念: 1. **Oracle函数**:函数是一段可重复使用的PL/SQL代码,它接收输入参数(IN参数),可选地返回一个结果值(RETURN参数)。...

    ibatis oracle clob

    标题 "ibatis oracle clob" 涉及到的是在Java开发中,使用iBATIS框架与Oracle数据库交互时处理CLOB(Character Large Object)类型数据的问题。CLOB是Oracle数据库用于存储大文本数据(如XML文档、长篇文章等)的...

Global site tag (gtag.js) - Google Analytics