前言
数据库突然要求对某些字段进行加密,因此我们在进行查询时候需要对字段进行加解密,此时需要对字段进行处理,但是如果字段用的地方比较多的话,修改还是挺复杂的,这里只是给大家一个方法,不一定是最好的,但是比较方便。就是使用typeHandler
提示:以下是本篇文章正文内容,下面案例可供参考
一、typeHandler是什么?
TypeHandler被称作类型处理器,MyBatis在设置预处理语句(PreparedStatement)中的参数或从结果集中取出一个值时,都会用类型处理器将Java对象转化为数据库支持的类型或者将获取到数据库值以合适的方式转换成 Java类型。
mybatis提供了31个默认的类型处理器,它们都位于org.apache.ibatis.type包下,这些默认的处理器能够满足我们大部分场景的需求。
本文接下来首先介绍TypeHandler接口的定义,之后介绍其在mybatis中如何使用,最后介绍一些mybatis如何注册TypeHandler。
二、使用步骤
1.引入库
代码如下(示例):
implementation 'org.springframework.boot:spring-boot-starter-data-redis'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-aop'
implementation 'com.squareup.okhttp3:okhttp:4.10.0'
implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.2.2'
implementation 'com.alibaba:druid-spring-boot-starter:1.2.11'
implementation 'com.alibaba:druid:1.1.10'
runtimeOnly 'mysql:mysql-connector-java'
annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
annotationProcessor 'org.projectlombok:lombok:1.18.10'
compileOnly 'org.projectlombok:lombok:1.18.10'
testAnnotationProcessor 'org.projectlombok:lombok:1.18.10'
testCompileOnly 'org.projectlombok:lombok:1.18.10'
//Mybatis-plus(简称MP)依赖
implementation group: 'com.baomidou', name: 'mybatis-plus-boot-starter', version: '3.5.1'
implementation group: 'com.github.yulichang', name: 'mybatis-plus-join', version: '1.2.4'
2.读入数据
代码如下(示例):
加入如下配置
# mybatis-plus
mybatis-plus:
mapper-locations: classpath:mapper/*.xml # 如果您在Mapper中有自定义方法(XML中有自定义实现),需要进行该配置
type-aliases-package: classpath:com.xx.xx.xx.entity
type-handlers-package: com.xx.xx.xx.config
别名类在mybaties 的xml文件中使用比较方便
package com.xx.xx.xx.entity;
import org.apache.ibatis.type.Alias;
@Alias("SecretField")
public class SecretField {
}
typeHandler 处理类
package com.xx.xx.xx.config;
import com.xx.xx.xx.entity.SecretField;
import com.xx.xx.xx.utils.Sm4Utils;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedTypes;
import org.springframework.util.StringUtils;
@MappedTypes(SecretField.class)
@Slf4j
public class SecretFieldTypeHandler extends BaseTypeHandler<String> {
@Override
public void setNonNullParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType) throws SQLException {
try {
if (StringUtils.hasText(parameter)) {
}
} catch (Exception e) {
ps.setString(i, parameter);
}
}
@Override
public String getNullableResult(ResultSet rs, String columnName) throws SQLException {
String columnValue = rs.getString(columnName);
try {
if (StringUtils.hasText(columnValue)) {
columnValue = Sm4Utils.decrypt( "d79cb1dd2b91434791262fed48a8e863", columnValue);
}
} catch (Exception e) {
}
return columnValue;
}
@Override
public String getNullableResult(ResultSet rs, int columnIndex) throws SQLException{
return null;
}
@Override
public String getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
return null;
}
}
下面是使用方式
@TableName(value = "FZLDDB.SURVEY_CASE", autoResultMap = true)
@Data
public class SurveyCase implements Serializable {
private static final long serialVersionUID = 1L;
@ApiModelProperty("用户姓名")
@TableField(typeHandler = SecretFieldTypeHandler.class)
private String userName;
@ApiModelProperty("证件号")
@TableField(typeHandler = SecretFieldTypeHandler.class)
private String idCard;
}
这样就可以在需要解密的字段上进行操作了。
如果是xml文件写的sql,可以使用以下方法
<resultMap id="resultMap " type="SurveyCase">
<result column="user_name" property="userName" javaType="SecretField"/>
</resultMap>
<select id="xxx" resultMap ="resultMap">
select *
from 表名
where user_name= #{userName,jdbcType=VARCHAR,javaType=SecretField}
</select>
或者
@Results(value = {
@Result(property = "userName", column = "userName", javaType = String.class, typeHandler = SecretFieldTypeHandler.class)
})
SurveyCase detail(@Param("id") String id);