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

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

spring boot 版本 2.7.3

1. 依赖

 		<dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.2.2</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-core</artifactId>
            <version>1.3.7</version>
        </dependency>
        <!-- 主要做自定义typhandler加解密,可以不引入-->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.7.22</version>
        </dependency>

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

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));
    }
}

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

  1. mybatis配置typhandler
mybatis:
  type-handlers-package: com.tcoding.demo.mybatis.mbg.handler
  1. xml 配置
    2.1 修改或更新的时候,指定需要加密字段
  <insert id="insert" parameterType="com.tcoding.demo.mybatis.mbg.model.SysUser">
       <!--
         WARNING - @mbg.generated
         This element is automatically generated by MyBatis Generator, do not modify.
       -->
       <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>

2.2 BaseResultMap是指定需要加密字段

<resultMap id="BaseResultMap" type="com.tcoding.demo.mybatis.mbg.model.SysUser">
      <!--
        WARNING - @mbg.generated
        This element is automatically generated by MyBatis Generator, do not modify.
      -->
      <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>

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

tcoding

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值