baoshiwei
2025-03-12 3c2c87364b89de46d12e95abd5bdf8cbd2c6dbf6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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<String> {
    @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;
        }
    }
}