• spring boot整合mybatis,mybatis generator ,自定义typhandler


    spring boot整合mybatis,mybatis generator ,自定义typhandler

    spring boot 版本 2.7.3

    1. 依赖

     		<dependency>
                <groupId>org.mybatis.spring.bootgroupId>
                <artifactId>mybatis-spring-boot-starterartifactId>
                <version>2.2.2version>
            dependency>
            <dependency>
                <groupId>mysqlgroupId>
                <artifactId>mysql-connector-javaartifactId>
            dependency>
    
            <dependency>
                <groupId>org.mybatis.generatorgroupId>
                <artifactId>mybatis-generator-coreartifactId>
                <version>1.3.7version>
            dependency>
            
            <dependency>
                <groupId>cn.hutoolgroupId>
                <artifactId>hutool-allartifactId>
                <version>5.7.22version>
            dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    2. application.yaml 配置

    server:
      servlet:
        context-path: tcoding
    spring:
      datasource:
        driver-class-name: com.mysql.cj.jdbc.Driver
        type: com.zaxxer.hikari.HikariDataSource
        url: jdbc:mysql://127.0.0.1:3306/hello_spring_boot?useUnicode=true&characterEncoding=UTF-8&useSSL=false
        username: root
        password: 123456
        hikari:
          minimum-idle: 5
          maximum-pool-size: 15
          connection-test-query: "select 1"
          max-lifetime: 1800000
          connection-timeout: 3000
          pool-name: "DatebookHikariCP"
    mybatis:
    #classpath和classpath*区别: 
    #classpath:只会到你的class路径中查找找文件。
    #classpath*:不仅包含class路径,还包括jar文件中(class路径)进行查找。
    #注意: 用classpath*:需要遍历所有的classpath,所以加载速度是很慢的;因此,在规划的时候,应该尽可能规划好资源文件所在的路径,尽量避免使用classpath*。
      mapper-locations: classpath:mapper/**/*.xml
      #type-handlers-package: com.tcoding.demo.mybatis.mbg.handler
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    3. mybatis generator配置

    就不贴代码了,需要的可以到末尾,有源码链接

    3.1 Generator
    3.2 CommentGenerator
    3.3 generator配置文件
    3.4 生成代码

    4. 自定义typhandler

    public class EncryptTypeHandler extends BaseTypeHandler<String> {
    
        private static final String KEY = "9a4601004c1111ec9444b7a8a6f9c533";
        private static final AES AES =
        // 使用的hutool提供的工具类,可以根据情况选择其他方式加解密
         SecureUtil.aes(SecureUtil.generateKey(SymmetricAlgorithm.AES.getValue(), KEY.getBytes()).getEncoded());
    
        @Override
        public void setNonNullParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType) throws SQLException {
            if (parameter == null) {
                ps.setString(i, null);
                return;
            }
    
            ps.setString(i, AES.encryptHex(parameter));
        }
    
        @Override
        public String getNullableResult(ResultSet rs, String columnName) throws SQLException {
            return AES.decryptStr(rs.getString(columnName));
        }
    
        @Override
        public String getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
            return AES.decryptStr(rs.getString(columnIndex));
        }
    
        @Override
        public String getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
            return AES.decryptStr(cs.getString(columnIndex));
        }
    }
    
    • 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

    是自定义typehandler生效,需要一下步骤

    1. mybatis配置typhandler
    mybatis:
      type-handlers-package: com.tcoding.demo.mybatis.mbg.handler
    
    • 1
    • 2
    1. xml 配置
      2.1 修改或更新的时候,指定需要加密字段
      <insert id="insert" parameterType="com.tcoding.demo.mybatis.mbg.model.SysUser">
           
           <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Long">
               SELECT LAST_INSERT_ID()
           selectKey>
           insert into sys_user (user_name, group_id, password,
           phone_number, c_time, u_time
           )
           values (#{userName,jdbcType=VARCHAR}, #{groupId,jdbcType=BIGINT},
           #指定加密字段
           #{password,jdbcType=VARCHAR,typeHandler=com.tcoding.demo.mybatis.mbg.handler.EncryptTypeHandler},
           #指定加密字段
           #{phoneNumber,jdbcType=VARCHAR,typeHandler=com.tcoding.demo.mybatis.mbg.handler.EncryptTypeHandler},
           #{cTime,jdbcType=TIMESTAMP},
           #{uTime,jdbcType=TIMESTAMP}
           )
       insert>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    2.2 BaseResultMap是指定需要加密字段

    <resultMap id="BaseResultMap" type="com.tcoding.demo.mybatis.mbg.model.SysUser">
          
          <id column="id" jdbcType="BIGINT" property="id"/>
          <result column="user_name" jdbcType="VARCHAR" property="userName"/>
          <result column="group_id" jdbcType="BIGINT" property="groupId"/>
           #指定加密字段
          <result column="password" jdbcType="VARCHAR" property="password"
                  typeHandler="com.tcoding.demo.mybatis.mbg.handler.EncryptTypeHandler"/>
          #指定加密字段
          <result column="phone_number" jdbcType="VARCHAR" property="phoneNumber"
                  typeHandler="com.tcoding.demo.mybatis.mbg.handler.EncryptTypeHandler"/>
          <result column="c_time" jdbcType="TIMESTAMP" property="cTime"/>
          <result column="u_time" jdbcType="TIMESTAMP" property="uTime"/>
      resultMap>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    源码地址 链接: https://github.com/googalAmbition/hello-spring-boot/tree/main/03-mybatis

  • 相关阅读:
    Elixir-Tuples
    【教学类-09】20221022《动物棋》(数字续写和骰子游戏)(大班主题《动物花花衣》)
    数据库的一级、二级、三级封锁协议
    Java - SpringBoot整合Shiro(附源码地址)
    java计算机毕业设计小微企业人事管理系统源代码+数据库+系统+lw文档
    web前端JS基础------制作一个获取验证码
    setContentView详解
    Python开发者必读:Pip使用全攻略与最佳实践
    好用的天气预报API推荐
    第一百五十回 自定义组件综合实例:游戏摇杆一
  • 原文地址:https://blog.csdn.net/qq_23934475/article/details/126568277