提交 | 用户 | age
|
826b66
|
1 |
package com.hx.mybatis.aes.handler; |
C |
2 |
|
|
3 |
import com.hx.mybatis.aes.springbean.VariableAesKey; |
|
4 |
import com.hx.util.mysql.aes.MysqlHexAesTool; |
|
5 |
import org.apache.ibatis.type.BaseTypeHandler; |
|
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); |
|
35 |
if(data != null && data.length()%32==0 && MysqlHexAesTool.isHexStrValid(data)){ |
|
36 |
try{ |
|
37 |
data = MysqlHexAesTool.decryptData(data, VariableAesKey.getAesKey(columnName),null); |
|
38 |
}catch (Exception e){ |
|
39 |
//e.printStackTrace(); |
|
40 |
} |
|
41 |
} |
|
42 |
return data; |
|
43 |
} |
|
44 |
|
|
45 |
@Override |
|
46 |
public String getNullableResult(ResultSet rs, int columnIndex) throws SQLException { |
|
47 |
String data = rs.getString(columnIndex); |
|
48 |
if(data != null && data.length()%32==0 && MysqlHexAesTool.isHexStrValid(data)){ |
|
49 |
try{ |
|
50 |
data = MysqlHexAesTool.decryptData(data, VariableAesKey.getAesKey(null),null); |
|
51 |
}catch (Exception e){ |
|
52 |
//e.printStackTrace(); |
|
53 |
} |
|
54 |
} |
|
55 |
return data; |
|
56 |
} |
|
57 |
|
|
58 |
@Override |
|
59 |
public String getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { |
|
60 |
String data = cs.getString(columnIndex); |
|
61 |
if(data != null && data.length() < 129 && data.length()%32==0 && MysqlHexAesTool.isHexStrValid(data)){ |
|
62 |
try{ |
|
63 |
data = MysqlHexAesTool.decryptData(data, VariableAesKey.getAesKey(null),null); |
|
64 |
}catch (Exception e){ |
|
65 |
//e.printStackTrace(); |
|
66 |
} |
|
67 |
} |
|
68 |
return data; |
|
69 |
} |
|
70 |
|
|
71 |
} |
|
72 |
|