设为首页 收藏本站
查看: 1137|回复: 0

[经验分享] Ibatis 中的 oracle LOB 字段处理

[复制链接]

尚未签到

发表于 2016-7-22 09:06:44 | 显示全部楼层 |阅读模式
方法一
  来源: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.

http://opensource2.atlassian.com/confluence/oss/images/icons/emoticons/check.gif
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"/>


http://opensource2.atlassian.com/confluence/oss/images/icons/emoticons/check.gif
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://blog.csdn.net/fckeditor/editor/images/smiley/msn/lightbulb.gif
方法二

  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://blog.csdn.net/fckeditor/editor/images/smiley/msn/lightbulb.gif
方法三

  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://blog.csdn.net/fckeditor/editor/images/smiley/msn/lightbulb.gif
方法四

  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。
没有大堆代码是没有办法描述这个东西的,大致记录一下,希望可以对人有帮助。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-247623-1-1.html 上篇帖子: JAVA查询Oracle数据库集群连接字符串(转载) 下篇帖子: 数据批量导入Oracle数据库
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表