package cn.shlanbao.qms.handle; import java.sql.CallableStatement; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import org.apache.ibatis.type.JdbcType; import org.apache.ibatis.type.MappedJdbcTypes; import org.apache.ibatis.type.MappedTypes; import org.apache.ibatis.type.TypeHandler; @MappedJdbcTypes(JdbcType.VARCHAR) @MappedTypes(String.class) public class CustomEncodingTypeHandler implements TypeHandler { @Override public void setParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType) throws SQLException { // 一般用于插入或更新操作,这里可能不需要特殊编码设置 ps.setString(i, parameter); } @Override public String getResult(ResultSet rs, String columnName) throws SQLException { String value = rs.getString(columnName); try { // 假设数据是GB2312编码,将其转换为UTF - 8 byte[] gb2312Bytes = value.getBytes("ISO-8859-1"); return new String(gb2312Bytes, "GB2312"); } catch (Exception e) { return value; } } @Override public String getResult(ResultSet rs, int columnIndex) throws SQLException { String value = rs.getString(columnIndex); try { // 假设数据是GB2312编码,将其转换为UTF - 8 byte[] gb2312Bytes = value.getBytes("ISO-8859-1"); return new String(gb2312Bytes, "GB2312"); } catch (Exception e) { return value; } } @Override public String getResult(CallableStatement cs, int columnIndex) throws SQLException { String value = cs.getString(columnIndex); try { // 假设数据是GB2312编码,将其转换为UTF - 8 byte[] gb2312Bytes = value.getBytes("ISO-8859-1"); return new String(gb2312Bytes, "GB2312"); } catch (Exception e) { return value; } } }