提交 | 用户 | age
|
c64e12
|
1 |
package com.hx.mybatis.aes.handler; |
e29546
|
2 |
|
c64e12
|
3 |
import com.hx.mybatis.aes.springbean.VariableAesKey; |
893a03
|
4 |
import com.hx.util.mysql.aes.MysqlHexAesTool; |
e29546
|
5 |
import org.apache.ibatis.type.BaseTypeHandler; |
C |
6 |
import org.apache.ibatis.type.JdbcType; |
|
7 |
import org.apache.ibatis.type.MappedJdbcTypes; |
|
8 |
import org.apache.ibatis.type.MappedTypes; |
|
9 |
|
|
10 |
import java.sql.CallableStatement; |
|
11 |
import java.sql.PreparedStatement; |
|
12 |
import java.sql.ResultSet; |
|
13 |
import java.sql.SQLException; |
|
14 |
|
|
15 |
/** |
|
16 |
* @author CJH |
|
17 |
* @Date 2021-01-02 |
|
18 |
* // @MappedTypes注解中的类代表此转换器可以自动转换为的java对象,@MappedJdbcTypes注解中设置的是对应的jdbctype,mysql的json对象对应的jdbctype为VARCHAR。 |
|
19 |
*/ |
|
20 |
@MappedTypes(value = {String.class}) |
|
21 |
@MappedJdbcTypes(value = {JdbcType.VARCHAR}, includeNullJdbcType = true) |
|
22 |
public class GenericStringHandler extends BaseTypeHandler<String> { |
|
23 |
|
|
24 |
public GenericStringHandler() { |
|
25 |
} |
|
26 |
|
|
27 |
@Override |
|
28 |
public void setNonNullParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType) throws SQLException { |
|
29 |
ps.setString(i, parameter); |
|
30 |
} |
|
31 |
|
|
32 |
@Override |
|
33 |
public String getNullableResult(ResultSet rs, String columnName) throws SQLException { |
|
34 |
String data = rs.getString(columnName); |
893a03
|
35 |
if(data != null && data.length()%32==0 && MysqlHexAesTool.isHexStrValid(data)){ |
e29546
|
36 |
try{ |
893a03
|
37 |
data = MysqlHexAesTool.decryptData(data, VariableAesKey.getAesKey(columnName),null); |
e29546
|
38 |
}catch (Exception e){ |
C |
39 |
//e.printStackTrace(); |
|
40 |
} |
|
41 |
} |
|
42 |
return data; |
|
43 |
} |
|
44 |
|
|
45 |
@Override |
|
46 |
public String getNullableResult(ResultSet rs, int columnIndex) throws SQLException { |
c64e12
|
47 |
String data = rs.getString(columnIndex); |
893a03
|
48 |
if(data != null && data.length()%32==0 && MysqlHexAesTool.isHexStrValid(data)){ |
c64e12
|
49 |
try{ |
893a03
|
50 |
data = MysqlHexAesTool.decryptData(data, VariableAesKey.getAesKey(null),null); |
c64e12
|
51 |
}catch (Exception e){ |
C |
52 |
//e.printStackTrace(); |
|
53 |
} |
|
54 |
} |
|
55 |
return data; |
e29546
|
56 |
} |
C |
57 |
|
|
58 |
@Override |
|
59 |
public String getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { |
c64e12
|
60 |
String data = cs.getString(columnIndex); |
893a03
|
61 |
if(data != null && data.length() < 129 && data.length()%32==0 && MysqlHexAesTool.isHexStrValid(data)){ |
c64e12
|
62 |
try{ |
893a03
|
63 |
data = MysqlHexAesTool.decryptData(data, VariableAesKey.getAesKey(null),null); |
c64e12
|
64 |
}catch (Exception e){ |
C |
65 |
//e.printStackTrace(); |
|
66 |
} |
|
67 |
} |
|
68 |
return data; |
e29546
|
69 |
} |
C |
70 |
|
|
71 |
} |
|
72 |
|