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"/>

 

問題解決。

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章