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

  • 相关阅读:
    1062 Talent and Virtue (25 分)
    1083 List Grades (25 分)
    1149 Dangerous Goods Packaging (25 分)
    1121 Damn Single (25 分)
    1120 Friend Numbers (20 分)
    1084 Broken Keyboard (20 分)
    1092 To Buy or Not to Buy (20 分)
    数组与链表
    二叉树
    时间复杂度与空间复杂度
  • 原文地址:https://www.cnblogs.com/ooo0/p/15745802.html
Copyright © 2011-2022 走看看