因为clob类型的字段引发的错误:SQLException: ORA-03146: TTC 字段的缓冲区长度无效.
解决方法:
1、拦截类:
import org.apache.ibatis.type.JdbcType; import org.apache.ibatis.type.TypeHandler; import java.sql.CallableStatement; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import oracle.jdbc.OracleConnection; import oracle.sql.CLOB; import java.sql.*; /** * Oracle CLOB字段处理类. * @Author: gene **/ public class OracleClobTypeHandler implements TypeHandler<String> { public String valueOf(String param) { return null; } @Override public String getResult(ResultSet arg0, String arg1) throws SQLException { CLOB clob = (CLOB) arg0.getClob(arg1); return (clob == null || clob.length() == 0) ? null : clob.getSubString((long) 1, (int) clob.length()); } @Override public String getResult(ResultSet arg0, int arg1) throws SQLException { return null; } @Override public String getResult(CallableStatement arg0, int arg1) throws SQLException { return null; } @Override public void setParameter(PreparedStatement arg0, int arg1, String arg2, JdbcType arg3) throws SQLException { Connection connection = arg0.getConnection(); OracleConnection oracleConnection = connection.unwrap(OracleConnection.class); CLOB clob = CLOB.createTemporary(oracleConnection, true, 1, (short) 1); clob.setString(1, arg2); arg0.setClob(arg1, clob); } }
2、在xml中使用:
在新增或者修改语句中,找到对应的字段,在字段后面加上这一句--【jdbcType=CLOB, typeHandler=com.foundersc.handler.OracleClobTypeHandler】,示例如下:
<if test="holdInfo != null and holdInfo != '' "> hold_info = #{holdInfo, jdbcType=CLOB, typeHandler=com.foundersc.handler.OracleClobTypeHandler}, </if>