zoukankan      html  css  js  c++  java
  • Mybatis 的 TypeHandler 加解密

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

    @Data
    @AllArgsConstructor
    public class Encrypt {
        private String value;
    }
    

      

    2、编写一个加解密的TypeHandler

    /**
     * 加解密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语句中写法

    <?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的包路径

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

      

    5、编写后台代码

    @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

  • 相关阅读:
    PAT (Advanced Level) Practice 1055 The World's Richest (25 分) (结构体排序)
    PAT (Advanced Level) Practice 1036 Boys vs Girls (25 分)
    PAT (Advanced Level) Practice 1028 List Sorting (25 分) (自定义排序)
    PAT (Advanced Level) Practice 1035 Password (20 分)
    PAT (Advanced Level) Practice 1019 General Palindromic Number (20 分) (进制转换,回文数)
    PAT (Advanced Level) Practice 1120 Friend Numbers (20 分) (set)
    从零开始吧
    Python GUI编程(TKinter)(简易计算器)
    PAT 基础编程题目集 6-7 统计某类完全平方数 (20 分)
    PAT (Advanced Level) Practice 1152 Google Recruitment (20 分)
  • 原文地址:https://www.cnblogs.com/ooo0/p/15745802.html
Copyright © 2011-2022 走看看