配置方式:
@Slf4j
public class SensitiveHandler extends BaseTypeHandler<String> {
@Override
public void setNonNullParameter(PreparedStatement preparedStatement, int i, String s, JdbcType jdbcType)
throws SQLException {
// 加密
preparedStatement.setString(i, encrypt(s));
}
@Override
public String getNullableResult(ResultSet resultSet, String s) throws SQLException {
return tryDecrypt(resultSet.getString(s));
}
@Override
public String getNullableResult(ResultSet resultSet, int i) throws SQLException {
return tryDecrypt(resultSet.getString(i));
}
@Override
public String getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
return tryDecrypt(callableStatement.getString(i));
}
//加密
private String encrypt(String data) {
}
//解密:解密失败后,返回data
private String tryDecrypt(String data) {
}
}
使用方式:
@Data
@EqualsAndHashCode(callSuper = false)
@Builder
@TableName("t_test", autoResultMap = true)
public class Test extends Model<Test> {
/**
* 自增id
*/
@TableId(value = "id", type = IdType.AUTO)
private Long id;
/**
* 地址
*/
@TableField(typeHandler = KeyCenterHandler.class)
private String address;
}
注意事项:mybatisPlus如果使用wrapper来更新字段时,那么不会生效。更新保存与查询必须以对象为维度。
网友评论