package cn.yuanqiao.archive.common.typeHandler;import com.alibaba.druid.proxy.jdbc.NClobProxyImpl;import org.apache.ibatis.type.*;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Component;import java.io.BufferedReader;import java.io.IOException;import java.io.Reader;import java.io.StringReader;import java.sql.*;@MappedTypes(Clob.class)@MappedJdbcTypes(JdbcType.CLOB)@Componentpublic class NClobTypeHandler extends BaseTypeHandler<String> { @Value("${YQ_DATABASE}") private Integer databaseId; @Override public void setNonNullParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType) throws SQLException { StringReader reader=new StringReader(parameter); ps.setCharacterStream(i,reader,parameter.length()); } @Override public String getNullableResult(ResultSet rs, String columnName) throws SQLException { String clobToString=""; try { switch (databaseId) { case 4://达梦数据库 NClobProxyImpl aa=(NClobProxyImpl) rs.getObject(columnName); Clob rawClob = aa.getRawClob(); clobToString = ClobToString(rawClob); break; default: clobToString="长文本类型,无法显示"; break; } } catch (SQLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return clobToString; } @Override public String getNullableResult(ResultSet rs, int i) throws SQLException { String clobToString=""; try { switch (databaseId) { case 4: NClobProxyImpl aa=(NClobProxyImpl) rs.getObject(i); Clob rawClob = aa.getRawClob(); clobToString = ClobToString(rawClob); break; default: clobToString="长文本类型,无法显示"; break; } } catch (SQLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return clobToString; } @Override public String getNullableResult(CallableStatement callableStatement, int i) throws SQLException { String clobToString=""; try { switch (databaseId) { case 4: NClobProxyImpl aa=(NClobProxyImpl) callableStatement.getObject(i); Clob rawClob = aa.getRawClob(); clobToString = ClobToString(rawClob); break; default: clobToString="长文本类型,无法显示"; break; } } catch (SQLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return clobToString; } private String ClobToString(Clob clob) throws SQLException, IOException { String reString = ""; Reader is = clob.getCharacterStream();// 得到流 BufferedReader br = new BufferedReader(is); String s = br.readLine(); StringBuffer sb = new StringBuffer(); while (s != null) {// 执行循环将字符串全部取出付值给StringBuffer由StringBuffer转成STRING sb.append(s); s = br.readLine(); } reString = sb.toString(); return reString; }}
来源地址:https://blog.csdn.net/weixin_43484846/article/details/133776124