文章目录
提示:以下是本篇文章正文内容,下面案例可供参考
本样例仅支持mybatis-plus-generator3.5.1及以上版本
一.导入pom依赖
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.9</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-generator -->
<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>
二.配置代码生成器
package com.vector.music.utils;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import java.util.Collections;
/**
* @description: 代码生成器
* @author: YuanJie
* @create: 2022-1-31 16:22
**/
// 演示例子,执行 main 方法控制台输入模块表名回车自动生成对应项目目录中
public class Generator {
public static void main(String[] args) {
String path = System.getProperty("user.dir");
// 数据库配置(DataSourceConfig)
FastAutoGenerator.create("jdbc:mysql://localhost:3306/music?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true", "root", "123456")
// 全局配置(GlobalConfig)
.globalConfig(builder -> {
builder.author("YuanJie") // 设置作者
.enableSwagger() // 开启 swagger 模式
.fileOverride()// 覆盖已生成文件
.outputDir(path+"/src/main/java"); // 指定输出目录
})
// 包配置(PackageConfig)
.packageConfig(builder -> {
builder.parent("com.vector.music") // 设置父包名
.mapper("mapper")
.entity("pojo")
.controller("controller")
.service("service")
.pathInfo(Collections.singletonMap(OutputFile.mapperXml, path+"/src/main/resources/mapper")); // 设置mapperXml生成路径;
})
.strategyConfig(builder -> {
builder.addInclude("admin","collect","comment","list_contain_song","rank","sing","song","song_list","user") // 设置需要生成的表名
.addTablePrefix("t_", "c_"); // 设置过滤表前缀
})
.templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板,默认的是Velocity引擎模板
.execute();
}
}
生成代码结构