spring boot 版本 2.7.3
<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>
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.1 Generator
3.2 CommentGenerator
3.3 generator配置文件
3.4 生成代码
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生效,需要一下步骤
mybatis:
type-handlers-package: com.tcoding.demo.mybatis.mbg.handler
<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>
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>
源码地址 链接: https://github.com/googalAmbition/hello-spring-boot/tree/main/03-mybatis