介绍
使用SpringBoot集成MybatisPlus框架。
第一步:添加MybatisPlus依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.4</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.0.31</version>
<scope>runtime</scope>
</dependency>
第二步:创建MybatisPlus配置类
package com.example.config;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@MapperScan("com.example.system.mapper")
public class MybatisPlusConfig {
/**
* 添加分页插件
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));//如果配置多个插件,切记分页最后添加
interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());//配置乐观锁插件
return interceptor;
}
}
第二步:创建添加时间和修改时间自动填充类
package com.example.config;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;
import java.util.Date;
/**
* MybatisPlus自动填充添加时间和修改时间
*/
@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
@Override
public void insertFill(MetaObject metaObject) {
this.strictInsertFill(metaObject, "addTime", Date.class, new Date()); // 起始版本 3.3.0(推荐使用)
}
@Override
public void updateFill(MetaObject metaObject) {
this.strictUpdateFill(metaObject, "updateTime", Date.class, new Date()); // 起始版本 3.3.0(推荐)
}
}
需要再字段上添加注解配置
/**
* 添加时间
*/
@TableField(fill = FieldFill.INSERT)
private Date addTime;
/**
* 修改时间
*/
@TableField(fill = FieldFill.UPDATE)
private Date updateTime;
第三步:application.yml添加数据库连接
spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/enroll_sys
username: root
password: 666666
第四步:配置代码生成器
添加依赖
<!--代码生成器-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.5.4</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.32</version>
<scope>runtime</scope>
</dependency>
编写生成代码
注意:生成代码类一定要创建在test下面,不然FastAutoGenerator类无法导入
package com.example;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import java.sql.Types;
import java.util.Collections;
public class FastAutoGeneratorTests {
public static void main(String[] args) {
String outputDir = "/Users/mac/gitee/enroll-sys/src/main/java/";
String mapperOutputDir = "/Users/mac/gitee/enroll-sys/src/main/resources/mapper/";
// String outputDir = "E:\\gitee\\enroll-sys\\src\\main\\java\\";
// String mapperOutputDir = "E:\\gitee\\enroll-sys\\src\\main\\resources\\mapper\\";
FastAutoGenerator.create("jdbc:mysql://127.0.0.1:3306/enroll_sys", "root", "666666")
.globalConfig(builder -> {
builder.author("guochao") // 设置作者
// .enableSwagger() // 开启 swagger 模式
.disableOpenDir()
// .fileOverride() // 覆盖已生成文件
.outputDir(outputDir); // 指定输出目录
})
.dataSourceConfig(builder -> builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
int typeCode = metaInfo.getJdbcType().TYPE_CODE;
if (typeCode == Types.TINYINT) {
// 自定义类型转换
return DbColumnType.INTEGER;
}
return typeRegistry.getColumnType(metaInfo);
}))
.packageConfig(builder -> {
builder.parent("com.example") // 设置父包名
.moduleName("system") // 设置父包模块名
.pathInfo(Collections.singletonMap(OutputFile.xml, mapperOutputDir)); // 设置mapperXml生成路径
})
.strategyConfig(builder -> {
builder.addInclude("enroll_input_field") // 设置需要生成的表名
.addTablePrefix("sys_", "c_"); // 设置过滤表前缀
})
.templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板,默认的是Velocity引擎模板
.execute();
}
}