论坛首页 Java企业应用论坛

扩展ibatis的StringTypeHandler解决JDBC 对字符串超长限制

浏览 3935 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-12-23   最后修改:2009-12-23

解决thin JDBC 对字符串超长限制的问题。

 

原始错误是:
org.springframework.jdbc.UncategorizedSQLException: SqlMapClient operation; uncategorized SQLException for SQL []; SQL state [null]; error code [17070];
--- The error occurred in com/sinosoft/card/cardKind/db/dao/IC_CARDKINDNOTIFY_SqlMap.xml.
--- The error occurred while applying a parameter map.
--- Check the IC_CARDKINDNOTIFY.abatorgenerated_insert-InlineParameterMap.
--- Check the parameter mapping for the 'value' property.
--- Cause: java.sql.SQLException: 数据大小超出此类型的最大值: 2986; nested exception is com.ibatis.common.jdbc.exception.NestedSQLException:
--- The error occurred in com/sinosoft/card/cardKind/db/dao/IC_CARDKINDNOTIFY_SqlMap.xml.
--- The error occurred while applying a parameter map.
--- Check the IC_CARDKINDNOTIFY.abatorgenerated_insert-InlineParameterMap.
--- Check the parameter mapping for the 'value' property.
--- Cause: java.sql.SQLException: 数据大小超出此类型的最大值: 2986
at org.springframework.jdbc.support.SQLStateSQLExceptionTranslator.translate(SQLStateSQLExceptionTranslator.java:124)
at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.translate(SQLErrorCodeSQLExceptionTranslator.java:322)

 

分析结果见

http://blog.csdn.net/ruanee/archive/2006/03/24/637213.aspx

http://www.tomjamescn.cn/?p=63

 

后面的文章直接hack了ibatis的StringTypeHandler,完全没有必要

 

我的解决方法:1、扩展StringTypeHandler为LargeStringTypeHandler

 

package com.xxxx.component.ibatis.typehandler;

import java.io.StringReader;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import com.ibatis.sqlmap.engine.type.StringTypeHandler;

/**
 * For using LargeStringTypeHandler, you must add a line in SqlMapConfig.xml as following:<br/>
 * &lt;typeHandler javaType="java.lang.String" callback="com.sinosoft.component.ibatis.typehandler.LargeStringTypeHandler"/&gt;
 * @author airlink
 * @see http://blog.csdn.net/ruanee/archive/2006/03/24/637213.aspx
 * @see http://www.tomjamescn.cn/?p=63
 */
public class LargeStringTypeHandler extends StringTypeHandler {

   
    public void setParameter(PreparedStatement ps, int i, Object parameter, String jdbcType)
    throws SQLException {
        String s = (String)parameter;
        if (s.length() < 667) {
            //assume that all characters are chinese characters.
            super.setParameter(ps, i, parameter, jdbcType);
        }else{
            //use setCharacterStream can insert more characters.
            ps.setCharacterStream(i, new StringReader(s), s.length());
        }
    }
   
}

 

2、在SqlMapConfig.xml中添加一行

<typeHandler javaType="java.lang.String" callback="com.sinosoft.component.ibatis.typehandler.LargeStringTypeHandler"/>

 

问题解决。

 

 

论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics