Mybatis 的 TypeHandler 加解密

 

1、编写一个实体类,凡是此实体类的数据都表示需要加解密的

1
2
3
4
5
@Data
@AllArgsConstructor
public class Encrypt {
    private String value;
}

  

2、编写一个加解密的TypeHandler

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/**
 * 加解密TypeHandler
 */
@MappedJdbcTypes(JdbcType.VARCHAR)
@MappedTypes(Encrypt.class)
public class EncryptTypeHandler extends BaseTypeHandler<Encrypt> {
 
    private static final byte[] KEYS = "12345678abcdefgh".getBytes(StandardCharsets.UTF_8);
 
    /**
     * 设置参数
     */
    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, Encrypt parameter, JdbcType jdbcType) throws SQLException {
        if (parameter == null || parameter.getValue() == null) {
            ps.setString(i, null);
            return;
        }
        AES aes = SecureUtil.aes(KEYS);
        String encrypt = aes.encryptHex(parameter.getValue());
        ps.setString(i, encrypt);
    }
 
    /**
     * 获取值
     */
    @Override
    public Encrypt getNullableResult(ResultSet rs, String columnName) throws SQLException {
        return decrypt(rs.getString(columnName));
    }
 
    /**
     * 获取值
     */
    @Override
    public Encrypt getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        return decrypt(rs.getString(columnIndex));
    }
 
    /**
     * 获取值
     */
    @Override
    public Encrypt getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        return decrypt(cs.getString(columnIndex));
    }
 
    public Encrypt decrypt(String value) {
        if (null == value) {
            return null;
        }
        return new Encrypt(SecureUtil.aes(KEYS).decryptStr(value));
    }
}
  • @MappedTypes:表示该处理器处理的java类型是什么。

  • @MappedJdbcTypes:表示处理器处理的Jdbc类型。

 

3、sql语句中写法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xc.xcspringboot.mapper.customer.CustomerMapper">
 
    <resultMap id="BaseResultMapper" type="com.xc.xcspringboot.model.customer.Customer">
        <id column="id" property="id"/>
        <result column="phone" property="phone"/>
        <result column="address" property="address"/>
    </resultMap>
 
    <insert id="addCustomer">
        insert into customer(phone, address)
        values (#{phone}, #{address})
    </insert>
 
    <select id="findCustomer" resultMap="BaseResultMapper">
        select *
        from customer
        where phone = #{phone}
    </select>
 
</mapper>

SQL中没有什么特殊的写法。

 

4、配置文件中指定Typehandler的包路径

1
2
mybatis:
  type-handlers-package: com.xc.xcspringboot.typehandler

  

5、编写后台代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@RestController
public class CustomerController {
 
    @Autowired
    private CustomerMapper customerMapper;
 
    @ApiOperation(value = "addCustomer", notes = "")
    @GetMapping("addCustomer")
    public String addCustomer(@RequestParam("phone") String phone, @RequestParam("address") String address) {
        int result = customerMapper.addCustomer(new Encrypt(phone), address);
        return "添加结果: " + result;
    }
 
    @ApiOperation(value = "findCustomer", notes = "")
    @GetMapping("findCustomer")
    public Customer findCustomer(@RequestParam("phone") String phone) {
        return customerMapper.findCustomer(new Encrypt(phone));
    }
}

  

 

文章来源:https://juejin.cn/post/6963811586184052767

 

posted @   草木物语  阅读(419)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
历史上的今天:
2020-12-29 MySQL 存储过程
点击右上角即可分享
微信分享提示