• spring boot + mybaties-plus 数据库字段加解密


    前言

    数据库突然要求对某些字段进行加密,因此我们在进行查询时候需要对字段进行加解密,此时需要对字段进行处理,但是如果字段用的地方比较多的话,修改还是挺复杂的,这里只是给大家一个方法,不一定是最好的,但是比较方便。就是使用typeHandler


    提示:以下是本篇文章正文内容,下面案例可供参考

    一、typeHandler是什么?

    TypeHandler被称作类型处理器,MyBatis在设置预处理语句(PreparedStatement)中的参数或从结果集中取出一个值时,都会用类型处理器将Java对象转化为数据库支持的类型或者将获取到数据库值以合适的方式转换成 Java类型。
    mybatis提供了31个默认的类型处理器,它们都位于org.apache.ibatis.type包下,这些默认的处理器能够满足我们大部分场景的需求。
    本文接下来首先介绍TypeHandler接口的定义,之后介绍其在mybatis中如何使用,最后介绍一些mybatis如何注册TypeHandler。

     

    二、使用步骤

    1.引入库

    代码如下(示例):

    1. implementation 'org.springframework.boot:spring-boot-starter-data-redis'
    2. implementation 'org.springframework.boot:spring-boot-starter-web'
    3. implementation 'org.springframework.boot:spring-boot-starter-validation'
    4. implementation 'org.springframework.boot:spring-boot-starter-aop'
    5. implementation 'com.squareup.okhttp3:okhttp:4.10.0'
    6. implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.2.2'
    7. implementation 'com.alibaba:druid-spring-boot-starter:1.2.11'
    8. implementation 'com.alibaba:druid:1.1.10'
    9. runtimeOnly 'mysql:mysql-connector-java'
    10. annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
    11. testImplementation 'org.springframework.boot:spring-boot-starter-test'
    12. annotationProcessor 'org.projectlombok:lombok:1.18.10'
    13. compileOnly 'org.projectlombok:lombok:1.18.10'
    14. testAnnotationProcessor 'org.projectlombok:lombok:1.18.10'
    15. testCompileOnly 'org.projectlombok:lombok:1.18.10'
    16. //Mybatis-plus(简称MP)依赖
    17. implementation group: 'com.baomidou', name: 'mybatis-plus-boot-starter', version: '3.5.1'
    18. implementation group: 'com.github.yulichang', name: 'mybatis-plus-join', version: '1.2.4'

    2.读入数据

    代码如下(示例):

    加入如下配置

    1. # mybatis-plus
    2. mybatis-plus:
    3. mapper-locations: classpath:mapper/*.xml # 如果您在Mapper中有自定义方法(XML中有自定义实现),需要进行该配置
    4. type-aliases-package: classpath:com.xx.xx.xx.entity
    5. type-handlers-package: com.xx.xx.xx.config

    别名类在mybaties 的xml文件中使用比较方便 

    1. package com.xx.xx.xx.entity;
    2. import org.apache.ibatis.type.Alias;
    3. @Alias("SecretField")
    4. public class SecretField {
    5. }

    typeHandler 处理类

    1. package com.xx.xx.xx.config;
    2. import com.xx.xx.xx.entity.SecretField;
    3. import com.xx.xx.xx.utils.Sm4Utils;
    4. import java.sql.CallableStatement;
    5. import java.sql.PreparedStatement;
    6. import java.sql.ResultSet;
    7. import java.sql.SQLException;
    8. import lombok.extern.slf4j.Slf4j;
    9. import org.apache.ibatis.type.BaseTypeHandler;
    10. import org.apache.ibatis.type.JdbcType;
    11. import org.apache.ibatis.type.MappedTypes;
    12. import org.springframework.util.StringUtils;
    13. @MappedTypes(SecretField.class)
    14. @Slf4j
    15. public class SecretFieldTypeHandler extends BaseTypeHandler {
    16. @Override
    17. public void setNonNullParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType) throws SQLException {
    18. try {
    19. if (StringUtils.hasText(parameter)) {
    20. }
    21. } catch (Exception e) {
    22. ps.setString(i, parameter);
    23. }
    24. }
    25. @Override
    26. public String getNullableResult(ResultSet rs, String columnName) throws SQLException {
    27. String columnValue = rs.getString(columnName);
    28. try {
    29. if (StringUtils.hasText(columnValue)) {
    30. columnValue = Sm4Utils.decrypt( "d79cb1dd2b91434791262fed48a8e863", columnValue);
    31. }
    32. } catch (Exception e) {
    33. }
    34. return columnValue;
    35. }
    36. @Override
    37. public String getNullableResult(ResultSet rs, int columnIndex) throws SQLException{
    38. return null;
    39. }
    40. @Override
    41. public String getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
    42. return null;
    43. }
    44. }

     下面是使用方式

    1. @TableName(value = "FZLDDB.SURVEY_CASE", autoResultMap = true)
    2. @Data
    3. public class SurveyCase implements Serializable {
    4. private static final long serialVersionUID = 1L;
    5. @ApiModelProperty("用户姓名")
    6. @TableField(typeHandler = SecretFieldTypeHandler.class)
    7. private String userName;
    8. @ApiModelProperty("证件号")
    9. @TableField(typeHandler = SecretFieldTypeHandler.class)
    10. private String idCard;
    11. }

    这样就可以在需要解密的字段上进行操作了。

    如果是xml文件写的sql,可以使用以下方法

    1. <resultMap id="resultMap " type="SurveyCase">
    2. <result column="user_name" property="userName" javaType="SecretField"/>
    3. </resultMap>
    4. <select id="xxx" resultMap ="resultMap">
    5. select *
    6. from 表名
    7. where user_name= #{userName,jdbcType=VARCHAR,javaType=SecretField}
    8. </select>

    或者

    1. @Results(value = {
    2. @Result(property = "userName", column = "userName", javaType = String.class, typeHandler = SecretFieldTypeHandler.class)
    3. })
    4. SurveyCase detail(@Param("id") String id);


  • 相关阅读:
    从零开始,打造自己的专属游戏世界!
    MyBatis学习:mapper.xml文件中传参时,标签使用javaType和jdbcType属性
    SSM vue办公自动化计划管理系统
    golang --- module-aware 模式下 包引入
    <Java>JDK内置的常用接口和深浅拷贝
    .NET开源的简单、快速、强大的前后端分离后台权限管理系统
    Java开发岗位职责【杭州多测师】【杭州多测师_王sir】
    MySQL学习笔记27
    最新ChatGPT程序源码+AI系统+详细图文部署教程/支持GPT4.0/支持Midjourney绘画/Prompt知识库
    uniapp 工具类方法封装 v1.0.0
  • 原文地址:https://blog.csdn.net/weixin_42710740/article/details/127790103