时间:2021-07-01 10:21:17 帮助过:9人阅读
这些 typeHandle 类源码:
package com.yule.querydb.typehandler; import org.apache.ibatis.type.BaseTypeHandler; import org.apache.ibatis.type.JdbcType; import org.apache.ibatis.type.MappedJdbcTypes; import org.apache.ibatis.type.MappedTypes; import java.io.StringReader; import java.sql.*; /** * 自定义处理 JdbcType.Blob 和 java Object(String) * 处理转 json 报错,将 oracle.sql.Clob 类型转为 String 类型 * copy org.apache.ibatis.type.ClobTypeHandler 类的处理 * @author yule * @date 2018/10/9 17:57 */ @MappedTypes({Object.class}) @MappedJdbcTypes(value = {JdbcType.CLOB}) public class MyObjectClobTypeHandle extends BaseTypeHandler<Object> { @Override public void setNonNullParameter(PreparedStatement ps, int i, Object parameter, JdbcType jdbcType) throws SQLException { String parameterStr = (String) parameter; StringReader reader = new StringReader(parameterStr); ps.setCharacterStream(i, reader, parameterStr.length()); } @Override public Object getNullableResult(ResultSet rs, String columnName) throws SQLException { String value = ""; Clob clob = rs.getClob(columnName); if(clob != null) { int size = (int)clob.length(); value = clob.getSubString(1L, size); } return value; } @Override public Object getNullableResult(ResultSet rs, int columnIndex) throws SQLException { String value = ""; Clob clob = rs.getClob(columnIndex); if(clob != null) { int size = (int)clob.length(); value = clob.getSubString(1L, size); } return value; } @Override public Object getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { String value = ""; Clob clob = cs.getClob(columnIndex); if(clob != null) { int size = (int)clob.length(); value = clob.getSubString(1L, size); } return value; } }
MyObjectBlobTypeHandle
package com.yule.querydb.typehandler; import org.apache.ibatis.type.BaseTypeHandler; import org.apache.ibatis.type.JdbcType; import org.apache.ibatis.type.MappedJdbcTypes; import org.apache.ibatis.type.MappedTypes; import java.io.StringReader; import java.sql.*; /** * 自定义处理 JdbcType.Blob 和 java Object(String) * 处理转 json 报错,将 oracle.sql.Blob 类型转为 String 类型 * copy org.apache.ibatis.type.BlobTypeHandler 类的处理 * @author yule * @date 2018/10/9 17:57 */ @MappedTypes({Object.class}) @MappedJdbcTypes(value = {JdbcType.BLOB}) public class MyObjectBlobTypeHandle extends BaseTypeHandler<Object> { @Override public void setNonNullParameter(PreparedStatement ps, int i, Object parameter, JdbcType jdbcType) throws SQLException { byte[] parameterByteArray = (byte[]) parameter; String parameterStr = new String(parameterByteArray); StringReader reader = new StringReader(parameterStr); ps.setCharacterStream(i, reader, parameterStr.length()); } @Override public Object getNullableResult(ResultSet rs, String columnName) throws SQLException { Blob blob = rs.getBlob(columnName); byte[] returnValue = null; if(null != blob) { returnValue = blob.getBytes(1L, (int)blob.length()); } return returnValue == null ? "" : new String(returnValue); } @Override public Object getNullableResult(ResultSet rs, int columnIndex) throws SQLException { Blob blob = rs.getBlob(columnIndex); byte[] returnValue = null; if(null != blob) { returnValue = blob.getBytes(1L, (int)blob.length()); } return returnValue == null ? "" : new String(returnValue); } @Override public Object getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { Blob blob = cs.getBlob(columnIndex); byte[] returnValue = null; if(null != blob) { returnValue = blob.getBytes(1L, (int)blob.length()); } return returnValue == null ? "" : new String(returnValue); } }
MyObjectLongTypeHandle
package com.yule.querydb.typehandler; import org.apache.ibatis.type.BaseTypeHandler; import org.apache.ibatis.type.JdbcType; import org.apache.ibatis.type.MappedJdbcTypes; import org.apache.ibatis.type.MappedTypes; import java.io.StringReader; import java.sql.*; /** * 自定义处理 JdbcType.LONGVARCHAR 和 java Object(String) * 处理转 mybatis 执行返回map的sql 报错,将 oracle Long 型转为 String 类型 * @author yule * @date 2018/10/9 19:18 */ @MappedTypes({Object.class, String.class}) @MappedJdbcTypes(value = {JdbcType.LONGVARCHAR}) public class MyObjectLongTypeHandle extends BaseTypeHandler<Object> { @Override public void setNonNullParameter(PreparedStatement ps, int i, Object parameter, JdbcType jdbcType) throws SQLException { String parameterStr = (String) parameter; StringReader reader = new StringReader(parameterStr); ps.setCharacterStream(i, reader, parameterStr.length()); } @Override public Object getNullableResult(ResultSet rs, String columnName) throws SQLException { String str = rs.getString(columnName); return str != null ? str : ""; } @Override public Object getNullableResult(ResultSet rs, int columnIndex) throws SQLException { String str = rs.getString(columnIndex); return str != null ? str : ""; } @Override public Object getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { String str = cs.getString(columnIndex); return str != null ? str : ""; } }
解决:oracle+myBatis ResultMap 类型为 map 时,表字段类型有 Long/Blob/Clob 时报错
标签:alt pac ctypes 也会 而且 src turn pes lex