import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.TemplateType;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class Generator {
private static final String USERNAME = System.getenv().get("USER");
/**
* 專案資訊
*/
private static final String PROJECT_PATH = System.getProperty("user.dir");
private static final String JAVA_PATH = "/src/main/java";
private static final String RESOURCE_PATH = "/src/main/resources";
private static final String BASE_PACKAGE = "io.github.xxyopen.novel";
/**
* 資料庫資訊
*/
private static final String DATABASE_IP = "127.0.0.1";
private static final String DATABASE_PORT = "3306";
private static final String DATABASE_NAME = "novel";
private static final String DATABASE_USERNAME = "root";
private static final String DATABASE_PASSWORD = "test123456";
public static void main(String[] args) {
// 傳入需要生成的表名,多個用英文逗號分隔,所有用 all 表示
genCode("sys_user");
}
/**
* 程式碼生成
*/
private static void genCode(String tables) {
// 全域性配置
FastAutoGenerator.create(String.format("jdbc:mysql://%s:%s/%s?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai", DATABASE_IP, DATABASE_PORT, DATABASE_NAME), DATABASE_USERNAME, DATABASE_PASSWORD)
.globalConfig(builder -> {
builder.author(USERNAME) // 設定作者
.fileOverride()
// kotlin
//.enableSwagger() // 開啟 swagger 模式
.fileOverride() // 覆蓋已生成檔案
.commentDate("yyyy/MM/dd")
.outputDir(PROJECT_PATH + JAVA_PATH); // 指定輸出目錄
})
// 包配置
.packageConfig(builder -> builder.parent(BASE_PACKAGE) // 設定父包名
.entity("dao.entity")
.service("service")
.serviceImpl("service.impl")
.mapper("dao.mapper")
.controller("controller.front")
.pathInfo(Collections.singletonMap(OutputFile.mapperXml, PROJECT_PATH + RESOURCE_PATH + "/mapper")))
// 模版配置
.templateConfig(builder -> builder.disable(TemplateType.SERVICE)
.disable(TemplateType.SERVICEIMPL)
.disable(TemplateType.CONTROLLER))
// 策略配置
.strategyConfig(builder -> builder.addInclude(getTables(tables)) // 設定需要生成的表名
.controllerBuilder()
.enableRestStyle()
.serviceBuilder()
.formatServiceFileName("%sService")
) // 開啟生成@RestController 控制器
//.templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板,預設的是Velocity引擎模板
.execute();
}
/**
* 處理 all 和多表情況
*/
protected static List<String> getTables(String tables) {
return "all".equals(tables) ? Collections.emptyList() : Arrays.asList(tables.split(","));
}
}