|
|
@@ -0,0 +1,200 @@
|
|
|
+package com.zd.common.core.mybatisplus;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.annotation.FieldFill;
|
|
|
+import com.baomidou.mybatisplus.annotation.IdType;
|
|
|
+import com.baomidou.mybatisplus.generator.FastAutoGenerator;
|
|
|
+import com.baomidou.mybatisplus.generator.IFill;
|
|
|
+import com.baomidou.mybatisplus.generator.config.ConstVal;
|
|
|
+import com.baomidou.mybatisplus.generator.config.OutputFile;
|
|
|
+import com.baomidou.mybatisplus.generator.config.TemplateType;
|
|
|
+import com.baomidou.mybatisplus.generator.config.rules.DateType;
|
|
|
+import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
|
|
|
+import com.baomidou.mybatisplus.generator.fill.Column;
|
|
|
+import com.zd.common.core.utils.StringUtils;
|
|
|
+
|
|
|
+import java.net.URL;
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
+ * <p>代码生成</p>
|
|
|
+ *
|
|
|
+ * @author linft
|
|
|
+ * @version 1.0
|
|
|
+ * @date 11/25/2022
|
|
|
+ */
|
|
|
+public class MybatisPlusGenerator {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 代码生成
|
|
|
+ * @param tables
|
|
|
+ * @param tablePrefix
|
|
|
+ * @param addr
|
|
|
+ */
|
|
|
+ public static void generator(String[] tables, String tablePrefix, URL addr) {
|
|
|
+ ResourceBundle bundle = ResourceBundle.getBundle("code-generator");
|
|
|
+ //数据信息
|
|
|
+ String url = bundle.getString("mysql-url");
|
|
|
+ String username = bundle.getString("mysql-user");
|
|
|
+ String password = bundle.getString("mysql-pwd");
|
|
|
+
|
|
|
+ //输出目录
|
|
|
+ List<String> list = getCurrentModuleDir(addr);
|
|
|
+
|
|
|
+ /*策略配置*/
|
|
|
+ /*Controller 策略*/
|
|
|
+ String controllerSuperClass = "";
|
|
|
+
|
|
|
+ //过滤字段前缀
|
|
|
+ String fieldPrefix = "";
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 主键生成模式;
|
|
|
+ * IdType.AUTO -- 自增
|
|
|
+ * IdType.ASSIGN_ID -- 雪花算法
|
|
|
+ * IdType.ASSIGN_UUID -- uuid
|
|
|
+ */
|
|
|
+ IdType idType = IdType.ASSIGN_UUID;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 添加忽略字段
|
|
|
+ */
|
|
|
+ String[] ignoreColumns = {};
|
|
|
+ System.out.println("====================>>>start generator code......");
|
|
|
+ // 生成器
|
|
|
+ FastAutoGenerator.create(url, username, password)
|
|
|
+ // 全局配置
|
|
|
+ .globalConfig(builder -> builder
|
|
|
+ .disableOpenDir()// 禁止打开输出目录
|
|
|
+ .outputDir(list.get(0))
|
|
|
+ .author(bundle.getString("author"))
|
|
|
+ .dateType(DateType.TIME_PACK)
|
|
|
+ //.commentDate("yyyy-MM-dd HH:mm:ss")
|
|
|
+ .enableSwagger()// 开启 swagger 模式
|
|
|
+ .build())
|
|
|
+ // 包配置
|
|
|
+ .packageConfig(builder -> builder
|
|
|
+ .parent(list.get(2))
|
|
|
+ //.controller(controllerPackageName)
|
|
|
+ .service("service")
|
|
|
+ .serviceImpl("service.impl")
|
|
|
+ .mapper("mapper")
|
|
|
+ .xml("mapper")
|
|
|
+ .entity("entity")
|
|
|
+ // 设置mapperXml生成路
|
|
|
+ .pathInfo(pathInfo(list))
|
|
|
+ .build())
|
|
|
+ // 模板配置
|
|
|
+ .templateConfig(builder -> builder
|
|
|
+ .disable(disableTemplateType)
|
|
|
+ .build()
|
|
|
+ )
|
|
|
+
|
|
|
+ // 策略配置
|
|
|
+ .strategyConfig(builder -> builder
|
|
|
+ .addInclude(tables)
|
|
|
+ .addTablePrefix(tablePrefix)
|
|
|
+ .addFieldPrefix(fieldPrefix)
|
|
|
+
|
|
|
+ // Controller 策略
|
|
|
+ .controllerBuilder()
|
|
|
+ .fileOverride()
|
|
|
+ .superClass(controllerSuperClass)
|
|
|
+ .enableRestStyle()
|
|
|
+ .formatFileName("%sController")
|
|
|
+
|
|
|
+ // Service 策略
|
|
|
+ .serviceBuilder()
|
|
|
+ // 覆盖已生成文件
|
|
|
+ .fileOverride()
|
|
|
+ // 设置 service 接口父类
|
|
|
+ .superServiceClass(ConstVal.SUPER_SERVICE_CLASS)
|
|
|
+ // 设置 service 实现类父类
|
|
|
+ .superServiceImplClass(ConstVal.SUPER_SERVICE_IMPL_CLASS)
|
|
|
+ .formatServiceFileName("%sService")
|
|
|
+ .formatServiceImplFileName("%sServiceImpl")
|
|
|
+
|
|
|
+ // mapper策略
|
|
|
+ .mapperBuilder()
|
|
|
+ .fileOverride()
|
|
|
+ // mapper接口的父类
|
|
|
+ .superClass(ConstVal.SUPER_MAPPER_CLASS)
|
|
|
+ .enableMapperAnnotation()
|
|
|
+ .enableBaseResultMap()
|
|
|
+ .enableBaseColumnList()
|
|
|
+ .formatMapperFileName("%sMapper")
|
|
|
+ .formatXmlFileName("%sMapper")
|
|
|
+
|
|
|
+ // entity 策略
|
|
|
+ .entityBuilder()
|
|
|
+ .fileOverride()
|
|
|
+ .enableLombok()
|
|
|
+ //.enableTableFieldAnnotation()
|
|
|
+ .enableActiveRecord()
|
|
|
+ //.logicDeleteColumnName(logicDeleteColumnName)
|
|
|
+ //.logicDeletePropertyName(logicDeletePropertyName)
|
|
|
+ .addIgnoreColumns(ignoreColumns)
|
|
|
+ .addTableFills(fillColumns)
|
|
|
+ .formatFileName("%s")
|
|
|
+ .idType(idType)
|
|
|
+ .build())
|
|
|
+ .templateEngine(new FreemarkerTemplateEngine())
|
|
|
+ .execute();
|
|
|
+ System.out.println("代码生成已完成!");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取当前模块的路劲
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static List<String> getCurrentModuleDir(URL url) {
|
|
|
+ if (url == null || StringUtils.isEmpty(url.getPath())) {
|
|
|
+ throw new RuntimeException("The empty url!");
|
|
|
+ }
|
|
|
+ List<String> list = new ArrayList<>();
|
|
|
+ String path = url.getPath();
|
|
|
+ if (!StringUtils.isEmpty(path)) {
|
|
|
+ String[] arr = path.substring(1,path.length()-1).split("/target/classes/");
|
|
|
+ list.add(arr[0]+"/src/main/java");
|
|
|
+ list.add(arr[0]+"/src/main/resources");
|
|
|
+ //packages
|
|
|
+ String packages = arr[1].replace("/",".");
|
|
|
+ list.add(packages);
|
|
|
+ }
|
|
|
+ return list;
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 各文件的输出路径
|
|
|
+ *
|
|
|
+ * @return pathInfo
|
|
|
+ */
|
|
|
+ public static Map<OutputFile, String> pathInfo(List<String> list) {
|
|
|
+ //存放各文件的包路径
|
|
|
+ Map<OutputFile, String> pathInfo = new HashMap<>(6);
|
|
|
+ pathInfo.put(OutputFile.xml, list.get(1) + "/mapper");
|
|
|
+ return pathInfo;
|
|
|
+ }
|
|
|
+
|
|
|
+ /*模板配置*/
|
|
|
+ /**
|
|
|
+ * 禁用模板 本项目配置不生成 Service ServiceImpl Controller
|
|
|
+ */
|
|
|
+ static TemplateType[] disableTemplateType = {
|
|
|
+ //TemplateType.ENTITY,
|
|
|
+ //TemplateType.MAPPER,
|
|
|
+ //TemplateType.XML,
|
|
|
+ //TemplateType.SERVICE,
|
|
|
+ //TemplateType.SERVICEIMPL,
|
|
|
+ TemplateType.CONTROLLER
|
|
|
+ };
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 自动填充的字段和时机
|
|
|
+ */
|
|
|
+ static List<IFill> fillColumns = new ArrayList<IFill>() {{
|
|
|
+ add(new Column("create_time", FieldFill.INSERT));
|
|
|
+ add(new Column("create_user", FieldFill.INSERT));
|
|
|
+ add(new Column("modify_time", FieldFill.UPDATE));
|
|
|
+ add(new Column("modify_user", FieldFill.UPDATE));
|
|
|
+ }};
|
|
|
+}
|