1. maven 导入 mybatis-plus
<!-- 导入mybatis-plus依赖 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-spring-boot3-starter</artifactId>
<version>3.5.7</version>
</dependency>
在application.yml文件中添加数据库相关
# DataSource Config
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/db_edu?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
username: edu
password: 123456
#mybatis-plus配置
mybatis-plus:
global-config:
banner: false
db-config:
logic-delete-field: isDeleted #软删除,只改变状态不做删除
id-type: auto
field-strategy: 1
db-column-underline: true
至此可通过mapper直接操作数据库
2. maven 导入 代码生成器
引入代码生成器时同时引入模板
<!-- 使用mybatis_plus的代码生成器 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.5.7</version>
</dependency>
<!-- 生成器 模板引擎 依赖 -->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.30</version>
</dependency>
编写 CodeGenerator
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.TemplateConfig;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import static com.baomidou.mybatisplus.generator.config.rules.DateType.ONLY_DATE;
public class CodeGenerator {
public static String parentFile = "com.example.eduserver";
public static String url = "jdbc:mysql://127.0.0.1:3306/db_edu?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai";
public static String username = "edu";
public static String password = "123456";
public static void main(String[] args) {
TemplateConfig build = new TemplateConfig.Builder()
.controller("templates/controller.java")
.build();
FastAutoGenerator.create(url, username, password)
.globalConfig(builder -> builder
.author("汪强")
.outputDir(Paths.get(System.getProperty("user.dir")) + "/src/main/java")
.commentDate("yyyy-MM-dd")
.disableOpenDir()
.dateType(ONLY_DATE)
)
.packageConfig(builder -> builder
.parent(parentFile)
.entity("entity")
.mapper("mapper")
.service("service")
.serviceImpl("service.impl")
.xml("mapper.xml")
)
.strategyConfig((scanner, builder) -> builder.addInclude(getTables(scanner.apply("请输入表名,多个英文逗号分隔?所有输入 all")))
.entityBuilder()
.enableLombok()
.controllerBuilder()
.template("templates/controller.java")
.enableFileOverride()
.entityBuilder()
.javaTemplate("templates/entity.java")
.enableFileOverride()
.mapperBuilder()
.mapperTemplate("templates/mapper.java")
.enableFileOverride()
.mapperXmlTemplate("templates/mapper.xml")
.enableFileOverride()
.serviceBuilder()
.serviceTemplate("templates/service.java")
.enableFileOverride()
.serviceImplTemplate("templates/serviceImpl.java")
.enableFileOverride()
)
.templateEngine(new FreemarkerTemplateEngine())
.execute();
}
protected static List<String> getTables(String tables) {
return "all".equals(tables) ? Collections.emptyList() : Arrays.asList(tables.split(","));
}
}
代码生成器不设置模板会自动使用freemarker默认模板 我这里自定义了几个模板 位置在resources下的templates目录下
freemarker默认模板位置在
至此,执行代码生成器 输入表名 会自动生成 controller、entity、mapper、mapper.xml、service、serviceImpl文件
有需求的可自行修改模板,快速构建自己的业务代码