package com.hx.mybatis.aes.handler;
|
|
import com.hx.mybatis.aes.springbean.VariableAesKey;
|
import com.hx.util.mysql.aes.MysqlHexAes;
|
import org.apache.ibatis.type.BaseTypeHandler;
|
import org.apache.ibatis.type.JdbcType;
|
import org.apache.ibatis.type.MappedJdbcTypes;
|
import org.apache.ibatis.type.MappedTypes;
|
|
import java.sql.CallableStatement;
|
import java.sql.PreparedStatement;
|
import java.sql.ResultSet;
|
import java.sql.SQLException;
|
|
/**
|
* @author CJH
|
* @Date 2021-01-02
|
* // @MappedTypes注解中的类代表此转换器可以自动转换为的java对象,@MappedJdbcTypes注解中设置的是对应的jdbctype,mysql的json对象对应的jdbctype为VARCHAR。
|
*/
|
@MappedTypes(value = {String.class})
|
@MappedJdbcTypes(value = {JdbcType.VARCHAR}, includeNullJdbcType = true)
|
public class GenericStringHandler extends BaseTypeHandler<String> {
|
|
public GenericStringHandler() {
|
}
|
|
@Override
|
public void setNonNullParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType) throws SQLException {
|
ps.setString(i, parameter);
|
}
|
|
@Override
|
public String getNullableResult(ResultSet rs, String columnName) throws SQLException {
|
String data = rs.getString(columnName);
|
if(data != null && data.length()%32==0 && MysqlHexAes.isHexStrValid(data)){
|
try{
|
data = MysqlHexAes.decryptData(data, VariableAesKey.getAesKey(columnName),null);
|
}catch (Exception e){
|
//e.printStackTrace();
|
}
|
}
|
return data;
|
}
|
|
@Override
|
public String getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
|
String data = rs.getString(columnIndex);
|
if(data != null && data.length()%32==0 && MysqlHexAes.isHexStrValid(data)){
|
try{
|
data = MysqlHexAes.decryptData(data, VariableAesKey.getAesKey(null),null);
|
}catch (Exception e){
|
//e.printStackTrace();
|
}
|
}
|
return data;
|
}
|
|
@Override
|
public String getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
|
String data = cs.getString(columnIndex);
|
if(data != null && data.length() < 129 && data.length()%32==0 && MysqlHexAes.isHexStrValid(data)){
|
try{
|
data = MysqlHexAes.decryptData(data, VariableAesKey.getAesKey(null),null);
|
}catch (Exception e){
|
//e.printStackTrace();
|
}
|
}
|
return data;
|
}
|
|
}
|