|
@@ -0,0 +1,180 @@
|
|
|
+package com.fjhx.modular;
|
|
|
+
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
+import com.baomidou.mybatisplus.annotation.DbType;
|
|
|
+import com.baomidou.mybatisplus.annotation.FieldFill;
|
|
|
+import com.baomidou.mybatisplus.annotation.IdType;
|
|
|
+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 java.util.Arrays;
|
|
|
+import java.util.HashMap;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 后端代码生成工具
|
|
|
+ * <p>
|
|
|
+ * 只为代码生成,不被任何模块关联
|
|
|
+ * <p>
|
|
|
+ * 使用:创建generator同级文件夹modular
|
|
|
+ * 赋值public static字段
|
|
|
+ * 执行execute()方法
|
|
|
+ */
|
|
|
+public class Storage {
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ execute();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 数据库名
|
|
|
+ public static String DATABASE_NAME = "storage";
|
|
|
+
|
|
|
+ // 模块名称
|
|
|
+ public static String MODULAR_NAME = "storage";
|
|
|
+
|
|
|
+ // 需要生成的表名称,多表用,隔开
|
|
|
+ public static String INCLUDE = "stock_correct";
|
|
|
+
|
|
|
+ // mysql连接
|
|
|
+ public static String MYSQL_URL = "rm-wz9f1jcr5466b42415o.mysql.rds.aliyuncs.com:3306";
|
|
|
+ public static String USER_NAME = "printer";
|
|
|
+ public static String PASSWORD = "RLP@uBr-w4G#AZ5";
|
|
|
+
|
|
|
+ // 业务位置
|
|
|
+ public static String SERVICE_PATH = "hx-service";
|
|
|
+
|
|
|
+ public static String BASE_ENTITY_PATH = "com.fjhx.base.BaseEntity";
|
|
|
+ public static String BASE_SERVICE_PATH = "com.fjhx.base.BaseService";
|
|
|
+
|
|
|
+ // 执行代码生成
|
|
|
+ public static void execute() {
|
|
|
+
|
|
|
+ MODULAR_NAME = ObjectUtil.isEmpty(MODULAR_NAME) ? DATABASE_NAME : MODULAR_NAME;
|
|
|
+
|
|
|
+ // 实体类所在模块
|
|
|
+ String entityChildren = "/hx-service-api/" + MODULAR_NAME.replace("_", "-") + "-api";
|
|
|
+
|
|
|
+ // 业务所在模块
|
|
|
+ String children = "/" + SERVICE_PATH + "/" + MODULAR_NAME.replace("_", "-");
|
|
|
+
|
|
|
+ // 生成的包路径
|
|
|
+ String parent = "com.fjhx";
|
|
|
+
|
|
|
+ // 生成模块的名称
|
|
|
+ String moduleName = INCLUDE.split(",")[0].split("_")[0];
|
|
|
+
|
|
|
+ // 创建代码生成器
|
|
|
+ AutoGenerator mpg = new AutoGenerator();
|
|
|
+
|
|
|
+ // 全局配置
|
|
|
+ GlobalConfig gc = new GlobalConfig();
|
|
|
+ gc.setOutputDir(System.getProperty("user.dir") + children + "/src/main/java");
|
|
|
+ gc.setOpen(false); //生成后是否打开资源管理器
|
|
|
+ gc.setFileOverride(false); //重新生成时文件是否覆盖
|
|
|
+ gc.setServiceName("%sService"); //去掉Service接口的首字母
|
|
|
+ gc.setDateType(DateType.ONLY_DATE);//定义生成的实体类中日期类型
|
|
|
+ gc.setSwagger2(false);//开启Swagger2模式
|
|
|
+ gc.setIdType(IdType.ASSIGN_ID); //主键策略
|
|
|
+ mpg.setGlobalConfig(gc);
|
|
|
+
|
|
|
+ // 数据源配置
|
|
|
+ DataSourceConfig dsc = new DataSourceConfig();
|
|
|
+ dsc.setUrl("jdbc:mysql://" + MYSQL_URL + "/" + DATABASE_NAME + "?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull" +
|
|
|
+ "&useSSL=true&serverTimezone=GMT%2B8&nullCatalogMeansCurrent=true");
|
|
|
+ dsc.setDriverName("com.mysql.cj.jdbc.Driver");
|
|
|
+ dsc.setUsername(USER_NAME);
|
|
|
+ dsc.setPassword(PASSWORD);
|
|
|
+ dsc.setDbType(DbType.MYSQL);
|
|
|
+ mpg.setDataSource(dsc);
|
|
|
+
|
|
|
+ // 包配置
|
|
|
+ PackageConfig pc = new PackageConfig();
|
|
|
+ pc.setParent(parent);// 包路径
|
|
|
+ pc.setModuleName(moduleName); //模块名
|
|
|
+ pc.setXml("mapper");
|
|
|
+ pc.setEntity("entity");
|
|
|
+ mpg.setPackageInfo(pc);
|
|
|
+
|
|
|
+ // 策略配置
|
|
|
+ StrategyConfig strategy = new StrategyConfig();
|
|
|
+ strategy.setInclude(INCLUDE.split(","));
|
|
|
+ // strategy.setTablePrefix(PREFIX); //生成实体时去掉表前缀
|
|
|
+ strategy.setNaming(NamingStrategy.underline_to_camel); //数据库表映射到实体的命名策略(下划线转驼峰命名)
|
|
|
+ strategy.setColumnNaming(NamingStrategy.underline_to_camel); //数据库表字段映射到实体的命名策略(下划线转驼峰命名)
|
|
|
+ strategy.setEntityLombokModel(true); // 开启lombok
|
|
|
+ strategy.setChainModel(false); // lombok实体链式调用
|
|
|
+ strategy.setRestControllerStyle(true); //restful api风格控制器
|
|
|
+ strategy.setVersionFieldName("version"); // 乐观锁字段名
|
|
|
+ strategy.setLogicDeleteFieldName("delFlag"); // 逻辑删除字段名
|
|
|
+ strategy.setSuperEntityColumns("id", "create_user", "create_time", "update_user", "update_time", "tenant_id");
|
|
|
+ strategy.setCapitalMode(false);
|
|
|
+ strategy.setEntitySerialVersionUID(false);
|
|
|
+ // service继承规则
|
|
|
+ strategy.setSuperServiceClass(BASE_SERVICE_PATH);
|
|
|
+ // 实体继承规则
|
|
|
+ strategy.setSuperEntityClass(BASE_ENTITY_PATH);
|
|
|
+ strategy.setTableFillList(Arrays.asList(
|
|
|
+ new TableFill("del_flag", FieldFill.INSERT),
|
|
|
+ new TableFill("version", FieldFill.INSERT)));
|
|
|
+ strategy.setLogicDeleteFieldName("del_flag");
|
|
|
+ mpg.setStrategy(strategy);
|
|
|
+
|
|
|
+ // 自定义模板
|
|
|
+ TemplateConfig templateConfig = new TemplateConfig();
|
|
|
+ templateConfig.setController("codeTemplates\\controller.java");
|
|
|
+ templateConfig.setService("codeTemplates\\service.java");
|
|
|
+ templateConfig.setServiceImpl("codeTemplates\\serviceImpl.java");
|
|
|
+ templateConfig.setMapper("codeTemplates\\mapper.java");
|
|
|
+ templateConfig.setEntity(null);
|
|
|
+
|
|
|
+ mpg.setTemplate(templateConfig);
|
|
|
+
|
|
|
+ InjectionConfig ic = new InjectionConfig() {
|
|
|
+ @Override
|
|
|
+ public void initMap() {
|
|
|
+ HashMap<String, Object> map = new HashMap<>();
|
|
|
+ map.put("entityName", parent + ".entity." + moduleName);
|
|
|
+ map.put("voPath", parent + ".params." + moduleName);
|
|
|
+ setMap(map);
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ // 实体模板配置
|
|
|
+ FileOutConfig entity = new FileOutConfig("/codeTemplates/entity.java.vm") {
|
|
|
+ @Override
|
|
|
+ public String outputFile(TableInfo tableInfo) {
|
|
|
+ return System.getProperty("user.dir") + entityChildren + "/src/main/java/com/fjhx/entity/"
|
|
|
+ + moduleName + "/" + tableInfo.getEntityName() + ".java";
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ // VO模板配置
|
|
|
+ FileOutConfig entityVo = new FileOutConfig("/codeTemplates/vo.java.vm") {
|
|
|
+ @Override
|
|
|
+ public String outputFile(TableInfo tableInfo) {
|
|
|
+ return System.getProperty("user.dir") + entityChildren + "/src/main/java/com/fjhx/params/"
|
|
|
+ + moduleName + "/" + tableInfo.getEntityName() + "Vo.java";
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ // Ex模板配置
|
|
|
+ FileOutConfig entityEx = new FileOutConfig("/codeTemplates/ex.java.vm") {
|
|
|
+ @Override
|
|
|
+ public String outputFile(TableInfo tableInfo) {
|
|
|
+ return System.getProperty("user.dir") + entityChildren + "/src/main/java/com/fjhx/params/"
|
|
|
+ + moduleName + "/" + tableInfo.getEntityName() + "Ex.java";
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ ic.setFileOutConfigList(Arrays.asList(entity, entityVo, entityEx));
|
|
|
+ mpg.setCfg(ic);
|
|
|
+
|
|
|
+ // 执行
|
|
|
+ mpg.execute();
|
|
|
+ }
|
|
|
+
|
|
|
+}
|