文章目录
- maven依赖
- application.yaml配置数据源
- 代码生成器
maven依赖
pom.xml
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.28</version></dependency><!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-boot-starter --><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.5.2</version></dependency><!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-generate --><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-generator</artifactId><version>3.5.1</version></dependency><dependency><groupId>org.freemarker</groupId><artifactId>freemarker</artifactId><version>2.3.31</version></dependency>
application.yaml配置数据源
# 项目名称 context: application
server:servlet:context-path: /app1port: 8080spring:datasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/empdb?useSSL=true&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghaiusername: rootpassword: root
代码生成器
package com.example;import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;import java.util.Collections;public class TestMybatisGenerate {public static void main(String[] args) {FastAutoGenerator.create("jdbc:mysql://localhost:3306/dicts?useSSL=true&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai", "root", "123456").globalConfig(builder -> {builder.author("dd") // 设置作者//.enableSwagger() // 开启 swagger 模式.outputDir("D://generate"); // 指定输出目录}).packageConfig(builder -> {builder.parent("com.example") // 设置父包名.moduleName("") // 设置父包模块名.pathInfo(Collections.singletonMap(OutputFile.mapperXml, "D://generate//com/example/mapper")); // 设置mapperXml生成路径}).strategyConfig(builder -> {builder.addInclude("receive_address") // 设置需要生成的表名.addTablePrefix("tb_", "c_"); // 设置过滤表前缀}).templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板,默认的是Velocity引擎模板.execute();}}