|
@@ -0,0 +1,158 @@
|
|
|
+package com.rongwei.bscommon.sys.utils;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.annotation.DbType;
|
|
|
+import com.baomidou.mybatisplus.annotation.FieldFill;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.StringPool;
|
|
|
+import com.baomidou.mybatisplus.generator.AutoGenerator;
|
|
|
+import com.baomidou.mybatisplus.generator.InjectionConfig;
|
|
|
+import com.baomidou.mybatisplus.generator.config.*;
|
|
|
+import com.baomidou.mybatisplus.generator.config.po.TableFill;
|
|
|
+import com.baomidou.mybatisplus.generator.config.po.TableInfo;
|
|
|
+import com.baomidou.mybatisplus.generator.config.rules.DateType;
|
|
|
+import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
|
|
|
+import com.rongwei.rwcommon.base.BaseDo;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author admin
|
|
|
+ */
|
|
|
+public class CodeGeneration {
|
|
|
+
|
|
|
+ //main函数
|
|
|
+ public static void main(String[] args) {
|
|
|
+
|
|
|
+ AutoGenerator autoGenerator = new AutoGenerator();
|
|
|
+ // 项目路径
|
|
|
+ String projectPath = System.getProperty("user.dir");
|
|
|
+
|
|
|
+ //全局配置
|
|
|
+ GlobalConfig gc = new GlobalConfig();
|
|
|
+ //生成文件输出根目录
|
|
|
+ gc.setOutputDir("E:\\temp");
|
|
|
+ //生成完成后不弹出文件框
|
|
|
+ gc.setOpen(false);
|
|
|
+ gc.setFileOverride(true); //文件覆盖
|
|
|
+ gc.setActiveRecord(false);// 不需要ActiveRecord特性的请改为false
|
|
|
+ gc.setEnableCache(false);// XML 二级缓存
|
|
|
+ gc.setBaseResultMap(true);// XML ResultMap
|
|
|
+ gc.setBaseColumnList(false);// XML columList
|
|
|
+ gc.setAuthor("fpy");// 作者
|
|
|
+ gc.setDateType(DateType.ONLY_DATE);
|
|
|
+
|
|
|
+ // 自定义文件命名,注意 %s 会自动填充表实体属性!
|
|
|
+ gc.setControllerName("%sController");
|
|
|
+ gc.setServiceName("%sService");
|
|
|
+ gc.setServiceImplName("%sServiceImpl");
|
|
|
+ gc.setMapperName("%sDao");
|
|
|
+ gc.setXmlName("%sDao");
|
|
|
+ autoGenerator.setGlobalConfig(gc);
|
|
|
+
|
|
|
+ // 数据源配置
|
|
|
+ DataSourceConfig dsc = new DataSourceConfig();
|
|
|
+ dsc.setDbType(DbType.MYSQL); //设置数据库类型,我是postgresql
|
|
|
+ dsc.setDriverName("com.mysql.cj.jdbc.Driver");
|
|
|
+ dsc.setUsername("root");
|
|
|
+ dsc.setPassword("rootfpy");
|
|
|
+ dsc.setUrl("jdbc:mysql://localhost:3306/incontrol?characterEncoding=utf8&failOverReadOnly=false&autoReconnect=true&roundRobinLoadBalance=true&serverTimezone=GMT%2B8&useSSL=false&allowMultiQueries=true&databaseTerm=SCHEMA"); //指定数据库
|
|
|
+ autoGenerator.setDataSource(dsc);
|
|
|
+
|
|
|
+ // 3、包的配置
|
|
|
+ PackageConfig pc = new PackageConfig();
|
|
|
+ // pc.setModuleName("blog"); // 生成到指定模块中
|
|
|
+ pc.setParent("com.rongwei");
|
|
|
+ pc.setEntity("bsentity.domain");
|
|
|
+ pc.setMapper("bscommon.sys.dao");
|
|
|
+ pc.setService("bscommon.sys.service");
|
|
|
+ pc.setServiceImpl("bscommon.sys.service.impl");
|
|
|
+ pc.setController("bsserver.controller");
|
|
|
+ autoGenerator.setPackageInfo(pc);
|
|
|
+
|
|
|
+ // 自定义模板
|
|
|
+ TemplateConfig templateConfig = new TemplateConfig();
|
|
|
+ templateConfig.setController("/gentemplates/controller.java.vm");
|
|
|
+ templateConfig.setEntity("/gentemplates/entity.java.vm");
|
|
|
+ templateConfig.setMapper("/gentemplates/mapper.java.vm");
|
|
|
+ templateConfig.setService("/gentemplates/service.java.vm");
|
|
|
+ templateConfig.setServiceImpl("/gentemplates/serviceImpl.java.vm");
|
|
|
+ autoGenerator.setTemplate(templateConfig);
|
|
|
+
|
|
|
+ // 4、策略配置
|
|
|
+ StrategyConfig strategy = new StrategyConfig();
|
|
|
+ strategy.setSuperEntityClass(BaseDo.class);
|
|
|
+ strategy.setRestControllerStyle(true);
|
|
|
+ // 设置要映射的表名(重要,需要修改的地方)
|
|
|
+ strategy.setInclude("srm_balance");
|
|
|
+ strategy.setNaming(NamingStrategy.underline_to_camel); // 自动转换表名的驼峰命名法
|
|
|
+ strategy.setColumnNaming(NamingStrategy.no_change); // 自动转换列名的驼峰命名法
|
|
|
+ strategy.setEntityLombokModel(true); // 是否使用lombox
|
|
|
+ strategy.setLogicDeleteFieldName("deleted"); // 配置逻辑删除字段
|
|
|
+ // 自动填充策略
|
|
|
+ TableFill gmtCreate = new TableFill("create", FieldFill.INSERT);
|
|
|
+ TableFill gmtModified = new TableFill("updated", FieldFill.INSERT);
|
|
|
+ ArrayList<TableFill> tableFills = new ArrayList<>();
|
|
|
+ tableFills.add(gmtCreate);
|
|
|
+ tableFills.add(gmtModified);
|
|
|
+// strategy.setTableFillList(tableFills);
|
|
|
+ // 乐观锁
|
|
|
+ strategy.setVersionFieldName("version");
|
|
|
+
|
|
|
+ autoGenerator.setStrategy(strategy);
|
|
|
+
|
|
|
+ // 自定义配置
|
|
|
+ InjectionConfig cfg = new InjectionConfig() {
|
|
|
+ @Override
|
|
|
+ public void initMap() {
|
|
|
+ // to do nothing
|
|
|
+ }
|
|
|
+ };
|
|
|
+ //自定义输出配置
|
|
|
+ List<FileOutConfig> focList = new ArrayList<>();
|
|
|
+ String controllerParentPath = projectPath + "\\business-server\\src\\main\\java\\com\\rongwei\\bsserver\\controller\\";
|
|
|
+ String entityParentPath = projectPath + "\\business-entity\\src\\main\\java\\com\\rongwei\\bsentity\\domain\\";
|
|
|
+ String commonPath = projectPath + "\\business-common\\src\\main\\java\\com\\rongwei\\bscommon\\sys\\";
|
|
|
+ // 自定义配置会被优先输出
|
|
|
+ focList.add(new FileOutConfig("/gentemplates/controller.java.vm") {
|
|
|
+ @Override
|
|
|
+ public String outputFile(TableInfo tableInfo) {
|
|
|
+ // 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
|
|
|
+ return controllerParentPath + tableInfo.getEntityName() + "Controller" + StringPool.DOT_JAVA;
|
|
|
+ }
|
|
|
+ });
|
|
|
+ focList.add(new FileOutConfig("/gentemplates/entity.java.vm") {
|
|
|
+ @Override
|
|
|
+ public String outputFile(TableInfo tableInfo) {
|
|
|
+ // 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
|
|
|
+ return entityParentPath + tableInfo.getEntityName() + "Do" + StringPool.DOT_JAVA;
|
|
|
+ }
|
|
|
+ });
|
|
|
+ focList.add(new FileOutConfig("/gentemplates/mapper.java.vm") {
|
|
|
+ @Override
|
|
|
+ public String outputFile(TableInfo tableInfo) {
|
|
|
+ // 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
|
|
|
+ return commonPath + "dao\\" + tableInfo.getEntityName() + "Dao" + StringPool.DOT_JAVA;
|
|
|
+ }
|
|
|
+ });
|
|
|
+ focList.add(new FileOutConfig("/gentemplates/service.java.vm") {
|
|
|
+ @Override
|
|
|
+ public String outputFile(TableInfo tableInfo) {
|
|
|
+ // 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
|
|
|
+ return commonPath + "service\\" + tableInfo.getEntityName() + "Service" + StringPool.DOT_JAVA;
|
|
|
+ }
|
|
|
+ });
|
|
|
+ focList.add(new FileOutConfig("/gentemplates/serviceImpl.java.vm") {
|
|
|
+ @Override
|
|
|
+ public String outputFile(TableInfo tableInfo) {
|
|
|
+ // 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
|
|
|
+ return commonPath + "service\\impl\\" + tableInfo.getEntityName() + "ServiceImpl" + StringPool.DOT_JAVA;
|
|
|
+ }
|
|
|
+ });
|
|
|
+ cfg.setFileOutConfigList(focList);
|
|
|
+
|
|
|
+ autoGenerator.setCfg(cfg);
|
|
|
+
|
|
|
+ autoGenerator.execute(); // 执行
|
|
|
+ }
|
|
|
+
|
|
|
+}
|