溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

iBATIS中怎么操作CLOB字段

發(fā)布時(shí)間:2021-07-27 16:14:17 來源:億速云 閱讀:307 作者:Leah 欄目:編程語言

本篇文章為大家展示了iBATIS中怎么操作CLOB字段,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。

環(huán)境:

JDK/JRE:1.5.0;Spring:1.2.7;iBATIS:2.1.7;Oracle:9.2.0.1;JDBC:OJDBC14_10g.jar

目標(biāo):

基于Spring提供的DBCP,使用iBATIS SQL Maps更新數(shù)據(jù)庫的CLOB字段。

現(xiàn)象:

總是有少部分特定的數(shù)據(jù)沒有更新,且并不是因?yàn)閿?shù)據(jù)過長而失敗,有時(shí)很短的數(shù)據(jù)也會(huì)失敗。現(xiàn)象可以重現(xiàn)。原來的SQL的 Maps配置文件如下:

﹤update id="updateByFoodID" parameterClass="Food"﹥    update TB_FOOD      ﹤dynamic prepend="set"﹥        ﹤isNotNull prepend="," property="foodDesc"﹥          FOOD_DESC = #foodDesc#        ﹤/isNotNull﹥        ﹤isNotNull prepend="," property="foodImage"﹥          FOOD_IMAGE = #foodImage#        ﹤/isNotNull﹥      ﹤/dynamic﹥    where FOOD_ID = #foodID#  ﹤/update﹥

查看log4j的DEBUG輸出,只有Preparing Statement、Executing Statement、和Parameters,并沒有異常輸出。

iBATIS操作CLOB字段初步分析原因,有如下6個(gè):

1、首先還是懷疑有地方數(shù)據(jù)過長;

2、操作的長字符串是JSON格式的,并且數(shù)據(jù)內(nèi)容可能包含‘?’、‘/’等特殊字符;

3、由于有多個(gè)程序同時(shí)使用該表,可能是事務(wù)隔離的原因;

4、LOB字段在Dynamic Mapped Statements中運(yùn)行的問題;

5、這個(gè)版本的Spring對iBATIS的支持問題;

6、JDBC的版本問題。

iBATIS操作CLOB字段分別分析:

1、VARCHAR2類型的列最長為4000字節(jié),VARCHAR2類型的PL/SQL變量最長為32767字節(jié),操作的數(shù)據(jù)確實(shí)有可能超過64k,但程序中沒有使用VARCHAR2變量存儲數(shù)據(jù),LOB類型的列更是可以存儲4G的數(shù)據(jù);

2、走查更新失敗的字符串內(nèi)容,沒有發(fā)現(xiàn)特殊之處;

3、新建一張臨時(shí)表,用以進(jìn)行insert和update操作,發(fā)現(xiàn)并沒有解決問題;

4、去掉動(dòng)態(tài)部分,寫最簡單的SQL語句,也沒有解決問題;

5、(暫時(shí)略過);

6、這個(gè)可能性比較小,一直都在用***版本的JDBC。

網(wǎng)上有人提到3種辦法,其中“第2種”,給SqlMapClientFactoryBean增加lobHandler屬性,據(jù)稱是指能工作在native的驅(qū)動(dòng)方式下,經(jīng)查Spring的API文檔,并不準(zhǔn)確,摘錄文檔如下:

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.

因此,修改Spring配置文件:

﹤bean id="jdbcExtractor" class="org.springframework.jdbc.support.nativejdbc.CommonsDbcpNativeJdbcExtractor" /﹥  ﹤bean id="oracleLobHandler" class="org.springframework.jdbc.support.lob.OracleLobHandler" lazy-init="true"﹥      ﹤property name="nativeJdbcExtractor"﹥﹤ref local="jdbcExtractor" /﹥﹤/property﹥  ﹤/bean﹥  ﹤bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"﹥      ﹤property name="configLocation" value="classpath:conf/sqlMapConfig.xml"﹥﹤/property﹥      ﹤property name="dataSource"﹥﹤ref local="aeqdsDataSource" /﹥﹤/property﹥      ﹤property name="lobHandler"﹥﹤ref local="oracleLobHandler" /﹥﹤/property﹥  ﹤/bean﹥

未果。

使用提到的“第3種”方法,這又有2種方式,第1種是全局性的,在SQL Maps配置文件中加入callback方法:

﹤typeHandler jdbcType="BLOB" javaType="[B" callback="org.  ringframework.orm.ibatis.support.BlobByteArrayTypeHandler" /﹥  ﹤typeHandler jdbcType="CLOB" javaType="java.lang.String"   llback="org.springframework.orm.ibatis.support.ClobStringTypeHandler" /﹥

第2種是在單個(gè)的屬性上指定typeHandler,如果是select,就可以用:

﹤resultMap id="foodResult" class="Food"﹥    ﹤result property="foodId" column="FOOD_ID"/﹥    ﹤result property="foodDesc" column="FOOD_DESC" typeHandler="org.springframework.orm.ibatis.support.ClobStringTypeHandler"/﹥    ﹤result property="foodImage" column="FOOD_IMAGE" typeHandler="org.springframework.orm.ibatis.support.BlobByteArrayTypeHandler"/﹥  ﹤/resultMap﹥

如果是insert或update,就可以用:

﹤update id="updateByFoodID" parameterClass="Food"﹥    update TB_FOOD      set FOOD_DESC = #foodDesc,handler=org.springframework.orm.ibatis.support.ClobStringTypeHandler#,          FOOD_IMAGE = #foodImage,handler=org.springframework.orm.ibatis.support.BlobByteArrayTypeHandler#      where FOOD_ID = #foodID#  ﹤/update﹥

加入這個(gè)callback或typeHandler方法后,iBATIS抱怨沒見過這個(gè)標(biāo)記,程序不能啟動(dòng)。把iBATIS升級為2.3.0,順便也把Spring升級為2.0.2,正常啟動(dòng)。但是效果更差,所有數(shù)據(jù)均不能更新,且日志顯示只進(jìn)行了Prepare Statement,卻沒有執(zhí)行。

死馬當(dāng)活馬醫(yī):

﹤parameterMap id="updateByFoodIDPara" class="Food"﹥    ﹤parameter property="foodDesc" jdbcType="CLOB" javaType="java.lang.String" /﹥    ﹤parameter property="foodImage" jdbcType="BLOB" javaType="[B" /﹥    ﹤parameter property="foodID" jdbcType="VARCHAR" javaType="java.lang.String" /﹥  ﹤/parameterMap﹥  ﹤update id="updateByFoodID" parameterMap="updateByFoodIDPara"﹥    update TB_FOOD      set FOOD_DESC = ?, FOOD_IMAGE = ?      where FOOD_ID = ?  ﹤/update﹥

依舊未果。

使出殺手锏,把update封裝在一個(gè)存儲過程中(其中不需要特別的事務(wù)處理),然后將SQL Maps配置文件的對應(yīng)內(nèi)容簡簡單單的修改為:

﹤procedure id="updateByFoodID" parameterClass="Food"﹥    {call pkg_xxx.sp_xxx (#foodID#, #foodDesc#, #foodImage#)}  ﹤/procedure﹥

上述內(nèi)容就是iBATIS中怎么操作CLOB字段,你們學(xué)到知識或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識儲備,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI