ibatis-数据类型超最大值

解决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

 

Java代码 复制代码  收藏代码
  1. package com.xxxx.component.ibatis.typehandler;   
  2.   
  3. import java.io.StringReader;   
  4. import java.sql.PreparedStatement;   
  5. import java.sql.SQLException;   
  6.   
  7. import com.ibatis.sqlmap.engine.type.StringTypeHandler;   
  8.   
  9. /**  
  10.  * For using LargeStringTypeHandler, you must add a line in SqlMapConfig.xml as following:<br/>  
  11.  * &lt;typeHandler javaType="java.lang.String" callback="com.sinosoft.component.ibatis.typehandler.LargeStringTypeHandler"/&gt;  
  12.  * @author airlink  
  13.  * @see http://blog.csdn.net/ruanee/archive/2006/03/24/637213.aspx  
  14.  * @see http://www.tomjamescn.cn/?p=63  
  15.  */  
  16. public class LargeStringTypeHandler extends StringTypeHandler {   
  17.   
  18.       
  19.     public void setParameter(PreparedStatement ps, int i, Object parameter, String jdbcType)   
  20.     throws SQLException {   
  21.         String s = (String)parameter;   
  22.         if (s.length() < 667) {   
  23.             //assume that all characters are chinese characters.   
  24.             super.setParameter(ps, i, parameter, jdbcType);   
  25.         }else{   
  26.             //use setCharacterStream can insert more characters.   
  27.             ps.setCharacterStream(i, new StringReader(s), s.length());   
  28.         }   
  29.     }   
  30.       
  31. }  
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"/>

 

问题解决。

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值