Skip to content

代码生成服务介绍何使用

介绍

服务新建完毕后,就需要生成后端代码. 也是在acuity-generator项目运行方法即可. 该项目是基于mybatis-plus-generator自定义的代码生成器,专门为天源云项目量身定做的代码生成器。

使用规则

  • 暂时只支持生成MySQL表,Oracle需要调整代码.
  • 参考 acuity-generator/src/test/java/下的代码
  • 每次执行前需配置参数,含代码生成目录(本地绝对目录),数据库连接,项目名、模块名,表列表等。目前已提供模板。
  • 如果表有前缀,可以配置 tablePrefix 属性。 例如:表名为f_file,则需要进行下列配置,生成的类会自动排除掉f_前缀。 new CodeGeneratorConfig().setTablePrefix("f_");
  • serviceName、childModuleName、childPackageName的区别
    • serviceName: 表示服务名 ,如 acuity-file 服务,就填 file
    • childModuleName: 表示模块名 ,如 acuity-msg 服务下面分为 acuity-msg、acuity-sms、acuity-email等, 在生成sms、email等模块时,就需要指定为 sms、email
    • childPackageName的区别: 表示子包名 ,用于在服务下,进行分包。如 acuity-authority 项目下有 auth、common、core等子包
      • 支持生成枚举类型的实体类字段
      • 支持生成RemoteData类型的实体类字段 (用于 关联数据字段自动注入)
  • 支持枚举类型改变存储路径。 参考CodeGeneratorConfig.setFiledTypes
  • 支持controller、service、dao、xml等各个模块 自定义生成策略: 覆盖、新增、忽略。参考:FileCreateConfig config = new FileCreateConfig(GenerateType.OVERRIDE);

数据库设计原则

  • 必须显式指定主键, 且不支持复合主键。

  • 任何表至少包含3个字段: bigint id、 datetime created_time、bigint created_by

  • 表、字段 必须加注释

  • 表名注释支持换行,第一行会被视为表名。 表的介绍请换行填写。

  • 字段的第一行视为字段简介,详细介绍和枚举类型请换行

  • 所有字段尽量根据业务设置合理的缺省值,尽量避免表中出现 NULL值

  • 当字段为外键时,字段名为: 关联表_id, 注释需要在字段注释基础上,换行加上 #关联表表名 来说明关联的哪张表。(注意英文#号)

  • 当字段为外键时,该字段的注释按照下面模板配置即可实现自动生成:

    java
    注释模板1:
    注释内容balabala
    @Echo(api = DICTIONARY_ITEM_FEIGN_CLASS) 
    
    注释模板2:
    用户
    @Echo(api = USER_ID_FEIGN_CLASS) 
    
    注释模板3:
    字典 (字典需要指定dictType)
    @Echo(api = "top.acuity.box.oauth.api.DictionaryApi", dictType = "Sex")
    注释模板1:
    注释内容balabala
    @Echo(api = DICTIONARY_ITEM_FEIGN_CLASS) 
    
    注释模板2:
    用户
    @Echo(api = USER_ID_FEIGN_CLASS) 
    
    注释模板3:
    字典 (字典需要指定dictType)
    @Echo(api = "top.acuity.box.oauth.api.DictionaryApi", dictType = "Sex")
  • 当字段为枚举时,该字段的注释按照下面模板配置即可实现自动生成:

    (注意: 注释内容 需要替换成当前字段的具体注释)
    
    注释模板1: 注释内容 #枚举类名{枚举值英文名:"枚举值英文注释";  ...}
    注释模板2: 注释内容 #枚举类名{枚举值英文名:val,"枚举值英文注释";  ...}
    注释模板3: 注释内容 #枚举类名{枚举值英文名:val,"枚举值英文注释",val2;  ...}
    注释模板4: 注释内容 #{枚举值英文名:"枚举值英文注释";  ...}
    其中枚举类名可以没有,如果没有,则生成的枚举值名为:表对应的实体名 + 当前字段对应的属性名(首字母大写) + Enum 
    枚举值例子:
      文件类型 #FileType{PAN:云盘数据;API:接口数据}
      数据类型 #DataType{DIR:1,目录;IMAGE:2,图片;VIDEO:3,视频;AUDIO:4,音频;DOC:5,文档;OTHER:6,其他}
      数据类型 #{DIR:目录;IMAGE:图片;VIDEO:视频;AUDIO:音频;DOC:文档;OTHER:其他}
    (注意: 注释内容 需要替换成当前字段的具体注释)
    
    注释模板1: 注释内容 #枚举类名{枚举值英文名:"枚举值英文注释";  ...}
    注释模板2: 注释内容 #枚举类名{枚举值英文名:val,"枚举值英文注释";  ...}
    注释模板3: 注释内容 #枚举类名{枚举值英文名:val,"枚举值英文注释",val2;  ...}
    注释模板4: 注释内容 #{枚举值英文名:"枚举值英文注释";  ...}
    其中枚举类名可以没有,如果没有,则生成的枚举值名为:表对应的实体名 + 当前字段对应的属性名(首字母大写) + Enum 
    枚举值例子:
      文件类型 #FileType{PAN:云盘数据;API:接口数据}
      数据类型 #DataType{DIR:1,目录;IMAGE:2,图片;VIDEO:3,视频;AUDIO:4,音频;DOC:5,文档;OTHER:6,其他}
      数据类型 #{DIR:目录;IMAGE:图片;VIDEO:视频;AUDIO:音频;DOC:文档;OTHER:其他}

创表SQL解释

sql
  `id` bigint(0) NOT NULL COMMENT 'ID',
  `name` varchar(24) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '名称',
  `stock` int(0) NOT NULL COMMENT '库存',
  `created_time` datetime(0) NULL DEFAULT NULL COMMENT '创建时间',
  `created_by` bigint(0) NULL DEFAULT NULL COMMENT '创建人',
  `updated_time` datetime(0) NULL DEFAULT NULL COMMENT '修改时间',
  `updated_by` bigint(0) NULL DEFAULT NULL COMMENT '修改人',
  `type_` text CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL COMMENT '商品类型 \n#ProductType{ordinary:普通;gift:赠品}',
  `type2` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL COMMENT '商品类型2 \n#{ordinary:普通;gift:赠品;}',
  `type3` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '学历 \n@Echo(api = DICTIONARY_ITEM_FEIGN_CLASS,  dictType = DictionaryType.EDUCATION)',
  `state` bit(1) NULL DEFAULT NULL COMMENT '状态',
  `test4` tinyint(0) NULL DEFAULT NULL COMMENT '测试',
  `test5` date NULL DEFAULT NULL COMMENT '时间',
  `test6` datetime(0) NULL DEFAULT NULL COMMENT '日期',
  `parent_id` bigint(0) NULL DEFAULT NULL COMMENT '父id',
  `label` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '名称',
  `sort_value` int(0) NULL DEFAULT NULL COMMENT '排序',
  `test7` char(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '测试字段 \n@Echo(api = "userApi") ',
  `user_id` bigint(0) NULL DEFAULT NULL COMMENT '用户 \n@Echo(api = USER_ID_FEIGN_CLASS)',
  `org_id` bigint(0) NULL DEFAULT NULL COMMENT '组织 \n@Echo(api = ORG_ID_FEIGN_CLASS)',
  PRIMARY KEY (`id`) USING BTREE
  `id` bigint(0) NOT NULL COMMENT 'ID',
  `name` varchar(24) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '名称',
  `stock` int(0) NOT NULL COMMENT '库存',
  `created_time` datetime(0) NULL DEFAULT NULL COMMENT '创建时间',
  `created_by` bigint(0) NULL DEFAULT NULL COMMENT '创建人',
  `updated_time` datetime(0) NULL DEFAULT NULL COMMENT '修改时间',
  `updated_by` bigint(0) NULL DEFAULT NULL COMMENT '修改人',
  `type_` text CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL COMMENT '商品类型 \n#ProductType{ordinary:普通;gift:赠品}',
  `type2` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL COMMENT '商品类型2 \n#{ordinary:普通;gift:赠品;}',
  `type3` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '学历 \n@Echo(api = DICTIONARY_ITEM_FEIGN_CLASS,  dictType = DictionaryType.EDUCATION)',
  `state` bit(1) NULL DEFAULT NULL COMMENT '状态',
  `test4` tinyint(0) NULL DEFAULT NULL COMMENT '测试',
  `test5` date NULL DEFAULT NULL COMMENT '时间',
  `test6` datetime(0) NULL DEFAULT NULL COMMENT '日期',
  `parent_id` bigint(0) NULL DEFAULT NULL COMMENT '父id',
  `label` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '名称',
  `sort_value` int(0) NULL DEFAULT NULL COMMENT '排序',
  `test7` char(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '测试字段 \n@Echo(api = "userApi") ',
  `user_id` bigint(0) NULL DEFAULT NULL COMMENT '用户 \n@Echo(api = USER_ID_FEIGN_CLASS)',
  `org_id` bigint(0) NULL DEFAULT NULL COMMENT '组织 \n@Echo(api = ORG_ID_FEIGN_CLASS)',
  PRIMARY KEY (`id`) USING BTREE

表注释解释

  1. type_ 字段的注释为: 商品类型\n#ProductType{ordinary:普通;gift:赠品;}
  2. type2 字段的注释为: 商品类型2\n#{ordinary:普通;gift:赠品;}'
  3. type3 字段的注释为: 字典类型\n@Echo(api = DICTIONARY_ITEM_FEIGN_CLASS, dictType = DictionaryType.EDUCATION)
  4. user_id 字段的注释为: 用户\n@Echo(api = USER_ID_FEIGN_CLASS)
  5. 使用 \n 换行, 则生成的代码中,这些字段的中文注释就是第一行:商品类型商品类型2字典类型用户 , type字段和type2 字段后面的注释会被识别生成枚举, type3和 user_id 被识别成RemoteData类型 。

acuity-cloud 项目生成代码

  1. 参考acuity-generator/src/test/java/cloud/TestCodeGenerator类, 在acuity-test 服务生成b_product

    java
    public static void main(String[] args) {
      CodeGeneratorConfig build = buildTestEntity();
    
      //mysql 账号密码
      build.setUsername("root");
      build.setPassword("root");
      build.setIsBoot(false);
    
      String path = "/Users/acuity/github/acuity-cloud/acuity-test";
      System.out.println("输出路径:" + path);
      build.setProjectRootPath(path);
      build.setProjectPrefix("acuity");
      // 指定全部代码的生成策略
      GenerateType generate = GenerateType.OVERRIDE;
    //        generate = null;
      // 构造器传入null,下面设置的策略(setGenerate*)才会生效, 构造器传入不为null时,下面设置的策略(setGenerate*)无效,将全部使用构造器传入的策略
      FileCreateConfig fileCreateConfig = new FileCreateConfig(generate);
      fileCreateConfig.setGenerateEntity(GenerateType.OVERRIDE);
      fileCreateConfig.setGenerateEnum(GenerateType.OVERRIDE);
      fileCreateConfig.setGenerateDto(GenerateType.OVERRIDE);
      fileCreateConfig.setGenerateXml(GenerateType.OVERRIDE);
      fileCreateConfig.setGenerateDao(GenerateType.IGNORE);
      fileCreateConfig.setGenerateServiceImpl(GenerateType.IGNORE);
      fileCreateConfig.setGenerateService(GenerateType.IGNORE);
      fileCreateConfig.setGenerateController(GenerateType.IGNORE);
      build.setFileCreateConfig(fileCreateConfig);
    
      //手动指定枚举类 生成的路径
      Set<EntityFiledType> filedTypes = new HashSet<>();
      filedTypes.addAll(Arrays.asList(
    //                EntityFiledType.builder().name("httpMethod").table("c_common_opt_log")
    // .packagePath("top.acuity.box.common.enums.HttpMethod").gen(GenerateType.IGNORE).build()
      ));
      build.setFiledTypes(filedTypes);
    
      build.setPackageBase("top.acuity.box." + build.getChildModuleName());
    
      // 运行
      CodeGenerator.run(build);
    }
    
    
    public static CodeGeneratorConfig buildTestEntity() {
      List<String> tables = Arrays.asList(
              "b_product"
      );
      CodeGeneratorConfig build = CodeGeneratorConfig.
              build("test", "", "acuity", "b_", tables);
      build.setSuperEntity(EntityType.ENTITY);
      build.setChildPackageName("");
      build.setUrl("jdbc:mysql://127.0.0.1:3306/acuity_extend_0000?serverTimezone=CTT&characterEncoding=utf8&useUnicode=true&useSSL=false&autoReconnect=true&zeroDateTimeBehavior=convertToNull");
      return build;
    }
    public static void main(String[] args) {
      CodeGeneratorConfig build = buildTestEntity();
    
      //mysql 账号密码
      build.setUsername("root");
      build.setPassword("root");
      build.setIsBoot(false);
    
      String path = "/Users/acuity/github/acuity-cloud/acuity-test";
      System.out.println("输出路径:" + path);
      build.setProjectRootPath(path);
      build.setProjectPrefix("acuity");
      // 指定全部代码的生成策略
      GenerateType generate = GenerateType.OVERRIDE;
    //        generate = null;
      // 构造器传入null,下面设置的策略(setGenerate*)才会生效, 构造器传入不为null时,下面设置的策略(setGenerate*)无效,将全部使用构造器传入的策略
      FileCreateConfig fileCreateConfig = new FileCreateConfig(generate);
      fileCreateConfig.setGenerateEntity(GenerateType.OVERRIDE);
      fileCreateConfig.setGenerateEnum(GenerateType.OVERRIDE);
      fileCreateConfig.setGenerateDto(GenerateType.OVERRIDE);
      fileCreateConfig.setGenerateXml(GenerateType.OVERRIDE);
      fileCreateConfig.setGenerateDao(GenerateType.IGNORE);
      fileCreateConfig.setGenerateServiceImpl(GenerateType.IGNORE);
      fileCreateConfig.setGenerateService(GenerateType.IGNORE);
      fileCreateConfig.setGenerateController(GenerateType.IGNORE);
      build.setFileCreateConfig(fileCreateConfig);
    
      //手动指定枚举类 生成的路径
      Set<EntityFiledType> filedTypes = new HashSet<>();
      filedTypes.addAll(Arrays.asList(
    //                EntityFiledType.builder().name("httpMethod").table("c_common_opt_log")
    // .packagePath("top.acuity.box.common.enums.HttpMethod").gen(GenerateType.IGNORE).build()
      ));
      build.setFiledTypes(filedTypes);
    
      build.setPackageBase("top.acuity.box." + build.getChildModuleName());
    
      // 运行
      CodeGenerator.run(build);
    }
    
    
    public static CodeGeneratorConfig buildTestEntity() {
      List<String> tables = Arrays.asList(
              "b_product"
      );
      CodeGeneratorConfig build = CodeGeneratorConfig.
              build("test", "", "acuity", "b_", tables);
      build.setSuperEntity(EntityType.ENTITY);
      build.setChildPackageName("");
      build.setUrl("jdbc:mysql://127.0.0.1:3306/acuity_extend_0000?serverTimezone=CTT&characterEncoding=utf8&useUnicode=true&useSSL=false&autoReconnect=true&zeroDateTimeBehavior=convertToNull");
      return build;
    }
  2. 若还有需要, 可以继续在test服务man模块生成b_order表的代码

    java
    public static void main(String[] args) {
     CodeGeneratorConfig build = buildManEntity();
    
     //mysql 账号密码
     build.setUsername("root");
     build.setPassword("root");
     build.setIsBoot(false);
    
     String path = "/Users/acuity/github/acuity-cloud/acuity-test";
     System.out.println("输出路径:" + path);
     build.setProjectRootPath(path);
     build.setProjectPrefix("acuity-");
     // 指定全部代码的生成策略
     GenerateType generate = GenerateType.OVERRIDE;
    //        generate = null;
     // 构造器传入null,下面设置的策略(setGenerate*)才会生效, 构造器传入不为null时,下面设置的策略(setGenerate*)无效,将全部使用构造器传入的策略
     FileCreateConfig fileCreateConfig = new FileCreateConfig(generate);
     fileCreateConfig.setGenerateEntity(GenerateType.OVERRIDE);
     fileCreateConfig.setGenerateEnum(GenerateType.OVERRIDE);
     fileCreateConfig.setGenerateDto(GenerateType.OVERRIDE);
     fileCreateConfig.setGenerateXml(GenerateType.OVERRIDE);
     fileCreateConfig.setGenerateDao(GenerateType.IGNORE);
     fileCreateConfig.setGenerateServiceImpl(GenerateType.IGNORE);
     fileCreateConfig.setGenerateService(GenerateType.IGNORE);
     fileCreateConfig.setGenerateController(GenerateType.IGNORE);
     build.setFileCreateConfig(fileCreateConfig);
    
     //手动指定枚举类 生成的路径
     Set<EntityFiledType> filedTypes = new HashSet<>();
     filedTypes.addAll(Arrays.asList(
    //                EntityFiledType.builder().name("httpMethod").table("c_common_opt_log")
    //                        .packagePath("top.acuity.box.common.enums.HttpMethod").gen(GenerateType.IGNORE).build()
     ));
     build.setFiledTypes(filedTypes);
    
     build.setPackageBase("top.acuity.box." + build.getChildModuleName());
    
     // 运行
     CodeGenerator.run(build);
    }
    public static CodeGeneratorConfig buildManEntity() {
     List<String> tables = Arrays.asList(
             "b_order"
     );
     CodeGeneratorConfig build = CodeGeneratorConfig.
             build("test", "man", "acuity", "b_", tables);
     build.setSuperEntity(EntityType.ENTITY);
     build.setChildPackageName("");
     build.setUrl("jdbc:mysql://127.0.0.1:3306/acuity_extend_0000?serverTimezone=CTT&characterEncoding=utf8&useUnicode=true&useSSL=false&autoReconnect=true&zeroDateTimeBehavior=convertToNull");
    
     // 不在 man-entity 模块生成实体代码
     build.setIsGenEntity(false);
     return build;
    }
    public static void main(String[] args) {
     CodeGeneratorConfig build = buildManEntity();
    
     //mysql 账号密码
     build.setUsername("root");
     build.setPassword("root");
     build.setIsBoot(false);
    
     String path = "/Users/acuity/github/acuity-cloud/acuity-test";
     System.out.println("输出路径:" + path);
     build.setProjectRootPath(path);
     build.setProjectPrefix("acuity-");
     // 指定全部代码的生成策略
     GenerateType generate = GenerateType.OVERRIDE;
    //        generate = null;
     // 构造器传入null,下面设置的策略(setGenerate*)才会生效, 构造器传入不为null时,下面设置的策略(setGenerate*)无效,将全部使用构造器传入的策略
     FileCreateConfig fileCreateConfig = new FileCreateConfig(generate);
     fileCreateConfig.setGenerateEntity(GenerateType.OVERRIDE);
     fileCreateConfig.setGenerateEnum(GenerateType.OVERRIDE);
     fileCreateConfig.setGenerateDto(GenerateType.OVERRIDE);
     fileCreateConfig.setGenerateXml(GenerateType.OVERRIDE);
     fileCreateConfig.setGenerateDao(GenerateType.IGNORE);
     fileCreateConfig.setGenerateServiceImpl(GenerateType.IGNORE);
     fileCreateConfig.setGenerateService(GenerateType.IGNORE);
     fileCreateConfig.setGenerateController(GenerateType.IGNORE);
     build.setFileCreateConfig(fileCreateConfig);
    
     //手动指定枚举类 生成的路径
     Set<EntityFiledType> filedTypes = new HashSet<>();
     filedTypes.addAll(Arrays.asList(
    //                EntityFiledType.builder().name("httpMethod").table("c_common_opt_log")
    //                        .packagePath("top.acuity.box.common.enums.HttpMethod").gen(GenerateType.IGNORE).build()
     ));
     build.setFiledTypes(filedTypes);
    
     build.setPackageBase("top.acuity.box." + build.getChildModuleName());
    
     // 运行
     CodeGenerator.run(build);
    }
    public static CodeGeneratorConfig buildManEntity() {
     List<String> tables = Arrays.asList(
             "b_order"
     );
     CodeGeneratorConfig build = CodeGeneratorConfig.
             build("test", "man", "acuity", "b_", tables);
     build.setSuperEntity(EntityType.ENTITY);
     build.setChildPackageName("");
     build.setUrl("jdbc:mysql://127.0.0.1:3306/acuity_extend_0000?serverTimezone=CTT&characterEncoding=utf8&useUnicode=true&useSSL=false&autoReconnect=true&zeroDateTimeBehavior=convertToNull");
    
     // 不在 man-entity 模块生成实体代码
     build.setIsGenEntity(false);
     return build;
    }
  3. acuity-cloud/acuity-test/acuity-test-server/pom.xml里面加入下面的代码, 点一下Maven的reimport ( 已经加入的请勿重复加入)

    xml
    <dependency>
        <groupId>top.acuity.box</groupId>
        <artifactId>acuity-man-controller</artifactId>
        <version>${acuity-project.version}</version>
    </dependency>
    <dependency>
        <groupId>top.acuity.box</groupId>
        <artifactId>acuity-test-controller</artifactId>
        <version>${acuity-project.version}</version>
    </dependency>
    <dependency>
        <groupId>top.acuity.box</groupId>
        <artifactId>acuity-man-controller</artifactId>
        <version>${acuity-project.version}</version>
    </dependency>
    <dependency>
        <groupId>top.acuity.box</groupId>
        <artifactId>acuity-test-controller</artifactId>
        <version>${acuity-project.version}</version>
    </dependency>
  4. 在nacos的acuity-test-server.yml 里面加入新建服务的swagger配置

    yaml
    acuity: 
      mall:
        title: 新增的商城服务
        base-package: top.acuity.box.test.controller;top.acuity.box.man.controller
    acuity: 
      mall:
        title: 新增的商城服务
        base-package: top.acuity.box.test.controller;top.acuity.box.man.controller
  5. 启动项目,并调试新生的代码.

acuity-boot 项目生成代码

  1. 参考acuity-generator/src/test/java/boot/TestCodeGenerator类, 在acuity-mall 模块生成b_product

    java
    public static void main(String[] args) {
     CodeGeneratorConfig build = buildMallEntity();
     // CodeGeneratorConfig build = buildManEntity();
    
     //mysql 账号密码
     build.setUsername("root");
     build.setPassword("root");
     // 指定是boot项目
     build.setIsBoot(true);
    
     String path = "/Users/acuity/github/acuity-boot/acuity-mall";
     System.out.println("输出路径:" + path);
     build.setProjectRootPath(path);
     // 项目前缀
     build.setProjectPrefix("acuity-");
     // 指定全部代码的生成策略
     GenerateType generate = GenerateType.OVERRIDE;
    //        generate = null;
     // 构造器传入null,下面设置的策略(setGenerate*)才会生效, 构造器传入不为null时,下面设置的策略(setGenerate*)无效,将全部使用构造器传入的策略
     FileCreateConfig fileCreateConfig = new FileCreateConfig(generate);
     fileCreateConfig.setGenerateEntity(GenerateType.OVERRIDE);
     fileCreateConfig.setGenerateEnum(GenerateType.OVERRIDE);
     fileCreateConfig.setGenerateDto(GenerateType.OVERRIDE);
     // 覆盖生成Xml
     fileCreateConfig.setGenerateXml(GenerateType.OVERRIDE);
     // 忽略生成Dao
     fileCreateConfig.setGenerateDao(GenerateType.IGNORE);
     fileCreateConfig.setGenerateServiceImpl(GenerateType.IGNORE);
     fileCreateConfig.setGenerateService(GenerateType.IGNORE);
     fileCreateConfig.setGenerateController(GenerateType.IGNORE);
     build.setFileCreateConfig(fileCreateConfig);
    
     //手动指定枚举类 生成的路径
     Set<EntityFiledType> filedTypes = new HashSet<>();
     filedTypes.addAll(Arrays.asList(
    //                EntityFiledType.builder().name("httpMethod").table("c_common_opt_log")
    //                        .packagePath("top.acuity.box.common.enums.HttpMethod").gen(GenerateType.IGNORE).build()
     ));
     build.setFiledTypes(filedTypes);
    
     build.setPackageBase("top.acuity.box." + build.getChildModuleName());
    
     // 运行
     CodeGenerator.run(build);
    }
    public static CodeGeneratorConfig buildMallEntity() {
     List<String> tables = Arrays.asList(
             "b_product"
     );
     CodeGeneratorConfig build = CodeGeneratorConfig.
             build("mall", "mall", "acuity", "b_", tables);
     build.setSuperEntity(EntityType.ENTITY);
     build.setChildPackageName("");
     build.setUrl("jdbc:mysql://127.0.0.1:3306/acuity_extend_0000?serverTimezone=CTT&characterEncoding=utf8&useUnicode=true&useSSL=false&autoReconnect=true&zeroDateTimeBehavior=convertToNull");
     return build;
    }
    public static void main(String[] args) {
     CodeGeneratorConfig build = buildMallEntity();
     // CodeGeneratorConfig build = buildManEntity();
    
     //mysql 账号密码
     build.setUsername("root");
     build.setPassword("root");
     // 指定是boot项目
     build.setIsBoot(true);
    
     String path = "/Users/acuity/github/acuity-boot/acuity-mall";
     System.out.println("输出路径:" + path);
     build.setProjectRootPath(path);
     // 项目前缀
     build.setProjectPrefix("acuity-");
     // 指定全部代码的生成策略
     GenerateType generate = GenerateType.OVERRIDE;
    //        generate = null;
     // 构造器传入null,下面设置的策略(setGenerate*)才会生效, 构造器传入不为null时,下面设置的策略(setGenerate*)无效,将全部使用构造器传入的策略
     FileCreateConfig fileCreateConfig = new FileCreateConfig(generate);
     fileCreateConfig.setGenerateEntity(GenerateType.OVERRIDE);
     fileCreateConfig.setGenerateEnum(GenerateType.OVERRIDE);
     fileCreateConfig.setGenerateDto(GenerateType.OVERRIDE);
     // 覆盖生成Xml
     fileCreateConfig.setGenerateXml(GenerateType.OVERRIDE);
     // 忽略生成Dao
     fileCreateConfig.setGenerateDao(GenerateType.IGNORE);
     fileCreateConfig.setGenerateServiceImpl(GenerateType.IGNORE);
     fileCreateConfig.setGenerateService(GenerateType.IGNORE);
     fileCreateConfig.setGenerateController(GenerateType.IGNORE);
     build.setFileCreateConfig(fileCreateConfig);
    
     //手动指定枚举类 生成的路径
     Set<EntityFiledType> filedTypes = new HashSet<>();
     filedTypes.addAll(Arrays.asList(
    //                EntityFiledType.builder().name("httpMethod").table("c_common_opt_log")
    //                        .packagePath("top.acuity.box.common.enums.HttpMethod").gen(GenerateType.IGNORE).build()
     ));
     build.setFiledTypes(filedTypes);
    
     build.setPackageBase("top.acuity.box." + build.getChildModuleName());
    
     // 运行
     CodeGenerator.run(build);
    }
    public static CodeGeneratorConfig buildMallEntity() {
     List<String> tables = Arrays.asList(
             "b_product"
     );
     CodeGeneratorConfig build = CodeGeneratorConfig.
             build("mall", "mall", "acuity", "b_", tables);
     build.setSuperEntity(EntityType.ENTITY);
     build.setChildPackageName("");
     build.setUrl("jdbc:mysql://127.0.0.1:3306/acuity_extend_0000?serverTimezone=CTT&characterEncoding=utf8&useUnicode=true&useSSL=false&autoReconnect=true&zeroDateTimeBehavior=convertToNull");
     return build;
    }
  2. 若还有需要, 可以继续在man模块生成b_order表的代码

    java
    public static void main(String[] args) {
     CodeGeneratorConfig build = buildManEntity();
    
     //mysql 账号密码
     build.setUsername("root");
     build.setPassword("root");
     // 指定是boot项目
     build.setIsBoot(true);
    
     String path = "/Users/acuity/github/acuity-boot/acuity-mall";
     System.out.println("输出路径:" + path);
     build.setProjectRootPath(path);
     // 项目前缀
     build.setProjectPrefix("acuity-");
     // 指定全部代码的生成策略
     GenerateType generate = GenerateType.OVERRIDE;
    //        generate = null;
     // 构造器传入null,下面设置的策略(setGenerate*)才会生效, 构造器传入不为null时,下面设置的策略(setGenerate*)无效,将全部使用构造器传入的策略
     FileCreateConfig fileCreateConfig = new FileCreateConfig(generate);
     fileCreateConfig.setGenerateEntity(GenerateType.OVERRIDE);
     fileCreateConfig.setGenerateEnum(GenerateType.OVERRIDE);
     fileCreateConfig.setGenerateDto(GenerateType.OVERRIDE);
     // 覆盖生成Xml
     fileCreateConfig.setGenerateXml(GenerateType.OVERRIDE);
     // 忽略生成Dao
     fileCreateConfig.setGenerateDao(GenerateType.IGNORE);
     fileCreateConfig.setGenerateServiceImpl(GenerateType.IGNORE);
     fileCreateConfig.setGenerateService(GenerateType.IGNORE);
     fileCreateConfig.setGenerateController(GenerateType.IGNORE);
     build.setFileCreateConfig(fileCreateConfig);
    
     //手动指定枚举类 生成的路径
     Set<EntityFiledType> filedTypes = new HashSet<>();
     filedTypes.addAll(Arrays.asList(
    //                EntityFiledType.builder().name("httpMethod").table("c_common_opt_log")
    //                        .packagePath("top.acuity.box.common.enums.HttpMethod").gen(GenerateType.IGNORE).build()
     ));
     build.setFiledTypes(filedTypes);
    
     build.setPackageBase("top.acuity.box." + build.getChildModuleName());
    
     // 运行
     CodeGenerator.run(build);
    }
    
     public static CodeGeneratorConfig buildManEntity() {
         List<String> tables = Arrays.asList(
                 "b_order"
         );
         CodeGeneratorConfig build = CodeGeneratorConfig.
                 build("mall", "man", "acuity", "b_", tables);
         build.setSuperEntity(EntityType.ENTITY);
         build.setChildPackageName("");
         build.setUrl("jdbc:mysql://127.0.0.1:3306/acuity_extend_0000?serverTimezone=CTT&characterEncoding=utf8&useUnicode=true&useSSL=false&autoReconnect=true&zeroDateTimeBehavior=convertToNull");
     
         build.setIsGenEntity(false);
         return build;
     
     }
    public static void main(String[] args) {
     CodeGeneratorConfig build = buildManEntity();
    
     //mysql 账号密码
     build.setUsername("root");
     build.setPassword("root");
     // 指定是boot项目
     build.setIsBoot(true);
    
     String path = "/Users/acuity/github/acuity-boot/acuity-mall";
     System.out.println("输出路径:" + path);
     build.setProjectRootPath(path);
     // 项目前缀
     build.setProjectPrefix("acuity-");
     // 指定全部代码的生成策略
     GenerateType generate = GenerateType.OVERRIDE;
    //        generate = null;
     // 构造器传入null,下面设置的策略(setGenerate*)才会生效, 构造器传入不为null时,下面设置的策略(setGenerate*)无效,将全部使用构造器传入的策略
     FileCreateConfig fileCreateConfig = new FileCreateConfig(generate);
     fileCreateConfig.setGenerateEntity(GenerateType.OVERRIDE);
     fileCreateConfig.setGenerateEnum(GenerateType.OVERRIDE);
     fileCreateConfig.setGenerateDto(GenerateType.OVERRIDE);
     // 覆盖生成Xml
     fileCreateConfig.setGenerateXml(GenerateType.OVERRIDE);
     // 忽略生成Dao
     fileCreateConfig.setGenerateDao(GenerateType.IGNORE);
     fileCreateConfig.setGenerateServiceImpl(GenerateType.IGNORE);
     fileCreateConfig.setGenerateService(GenerateType.IGNORE);
     fileCreateConfig.setGenerateController(GenerateType.IGNORE);
     build.setFileCreateConfig(fileCreateConfig);
    
     //手动指定枚举类 生成的路径
     Set<EntityFiledType> filedTypes = new HashSet<>();
     filedTypes.addAll(Arrays.asList(
    //                EntityFiledType.builder().name("httpMethod").table("c_common_opt_log")
    //                        .packagePath("top.acuity.box.common.enums.HttpMethod").gen(GenerateType.IGNORE).build()
     ));
     build.setFiledTypes(filedTypes);
    
     build.setPackageBase("top.acuity.box." + build.getChildModuleName());
    
     // 运行
     CodeGenerator.run(build);
    }
    
     public static CodeGeneratorConfig buildManEntity() {
         List<String> tables = Arrays.asList(
                 "b_order"
         );
         CodeGeneratorConfig build = CodeGeneratorConfig.
                 build("mall", "man", "acuity", "b_", tables);
         build.setSuperEntity(EntityType.ENTITY);
         build.setChildPackageName("");
         build.setUrl("jdbc:mysql://127.0.0.1:3306/acuity_extend_0000?serverTimezone=CTT&characterEncoding=utf8&useUnicode=true&useSSL=false&autoReconnect=true&zeroDateTimeBehavior=convertToNull");
     
         build.setIsGenEntity(false);
         return build;
     
     }
  3. acuity-boot/pom.xml 里面加入如下配置 ( 已经加入的请勿重复加入 )

    xml
    <module>acuity-mall</module>
    <module>acuity-mall</module>
  4. 生成后代码结构

  5. acuity-boot/acuity-boot-server/pom.xml里面加入下面的代码, 点一下Maven的reimport ( 已经加入的请勿重复加入)

    xml
    <dependency>
        <groupId>top.acuity.box</groupId>
        <artifactId>acuity-man-controller</artifactId>
        <version>${acuity-project.version}</version>
    </dependency>
    <dependency>
        <groupId>top.acuity.box</groupId>
        <artifactId>acuity-mall-controller</artifactId>
        <version>${acuity-project.version}</version>
    </dependency>
    <dependency>
        <groupId>top.acuity.box</groupId>
        <artifactId>acuity-man-controller</artifactId>
        <version>${acuity-project.version}</version>
    </dependency>
    <dependency>
        <groupId>top.acuity.box</groupId>
        <artifactId>acuity-mall-controller</artifactId>
        <version>${acuity-project.version}</version>
    </dependency>
  6. acuity-boot/acuity-boot-server/src/main/resources/application.yml 里面加入新建服务的swagger配置

    yaml
    acuity: 
      mall:
        title: 新增的商城服务
        base-package: top.acuity.box.mall.controller;top.acuity.box.man.controller
    acuity: 
      mall:
        title: 新增的商城服务
        base-package: top.acuity.box.mall.controller;top.acuity.box.man.controller
  7. 启动项目,并调试新生的代码.

注意事项

  1. 若想生成类似权限服务下面的子包(top.acuity.box.authority.dao.common), 可以设置 setChildPackageName("")
  2. 如果想让生成的枚举类不在默认的路径, 而是使用全局的枚举类, 可以设置 setFiledTypes() . 如 Sex、HttpMethod

欢迎使用天源云Saas快速开发系统