Skip to content

生成acuity-web-pro代码

前端代码生成器支持生成2种样式的页面: 分页列表+弹窗新增/修改 、 树形结构页面。

参考: acuity-generator-plus/src/test/java/cloud/TestWebPlusGenerator 类。

生成 分页列表+弹窗新增/修改

  1. 参考如下代码生成 def_user 表的代码

    java
    /**
    * 注意,想要在这里直接运行,需要手动增加 mysql 驱动
    * @param args
    */
    public static void main(String[] args) {
     // 生成前端页面,一定要设置成true
     FileCreateConfig fileCreateConfig = new FileCreateConfig(null, true);
     CodeGeneratorConfig build = buildOrgEntity(fileCreateConfig);
      
     // 项目前缀,改了影响src下面所有的acuity文件夹
     build.setProjectPrefix("acuity");
      
     // 设置后台字段类型和ts字段类型的映射关系
     setMapping(build);
      
     //mysql 账号密码
     build.setUsername("root");
     build.setPassword("root");
      
     // 文件生成策略
     build.setFileCreateConfig(fileCreateConfig);
      
     // 前端代码的绝对路径
     String vuePath = "/Users/acuity/gitlab/acuity-web-plus";
     build.setProjectRootPath(vuePath);
      
     //手动指定枚举类生成的路径, 不配置,则默认跟实体类平级,存放在enumeration包下
     Set<EntityFiledType> filedTypes = new HashSet<>();
     filedTypes.addAll(Arrays.asList(
     ));
     build.setFiledTypes(filedTypes);
      
     // 自定义前端页面字段的显示样式, 不填写时,默认生成全字段
     buildVue(build);
      
     //生成代码
     VueGenerator.run(build);
    }
    
    private static void setMapping(CodeGeneratorConfig build) {
    // java 类型 和 ts 类型的对应关系
        Map<String, String> map = new HashMap<>();
        map.put("String", "string");
        map.put("Date", "string");
        map.put("LocalDateTime", "string");
        map.put("LocalDate", "string");
        map.put("LocalTime", "string");
        map.put("Long", "string");
        map.put("Integer", "number");
        map.put("BigDecimal", "string");
        map.put("BigInteger", "string");
        map.put("Float", "number");
        map.put("Double", "number");
        map.put("Boolean", "boolean");
        map.put("Enum", "Enum");
        build.setFieldTypeMapping(map);
    }
    
    private static void buildVue(CodeGeneratorConfig build) {
        CodeGeneratorConfig.Vue vue = new CodeGeneratorConfig.Vue();
    // 指定生成的代码是 acuity-web-plus项目的
        vue.setVersion(VueVersion.webplus);
    
        // 生成的代码位于前端项目 src 下的什么路径?  默认是:  src/views/acuity
    
    //   vue.setViewsPath("views" + File.separator + build.getProjectPrefix());
    
        // 程序自动根据 表设计情况 为每个字段选择合适显示规则, 若不满足,则在此添加字段后修改即可
        Map<String, Map<String, GenTableColumn>> map = buildTableFieldMap();
        vue.setTableFieldMap(map);
        build.setVue(vue);
    
    }
    // 自定义表的字段 按照上面组件显示、在编辑、列表页是否显示等。 
    private static Map<String, Map<String, GenTableColumn>> buildTableFieldMap() {
        Map<String, Map<String, GenTableColumn>> map = new HashMap<>();
        List<GenTableColumnService> list = Arrays.asList(
                // org
                new GenUser(),
                new GenStation(),
                // system
                new GenApplication(),
                new GenArea(),
                new GenDictionary(),
                new GenLoginLog(),
                new GenOptLog(),
                new GenParameter(),
                new GenRole()
        );
        list.forEach(item -> map.putAll(item.map()));
        return map;
    }
    
    public static CodeGeneratorConfig buildOrgEntity(FileCreateConfig fileCreateConfig) {
        // 配置需要生成的表
        List<String> tables = Arrays.asList(
                "c_station",
                "c_user"
        );
        CodeGeneratorConfig build = CodeGeneratorConfig.
                buildVue("authority",  // 服务名 必填
                        "c_",            // 表前缀
                        tables);
    // 模糊查询表名
    //        build.setLikeTable(new LikeTable("b\\_", SqlLike.RIGHT));
    
        //父类是Entity
        build.setSuperEntity(EntityType.ENTITY);
        
        //生成的前端页面位于 src/${build.getVue().getViewsPath()}/${childPackageName} 目录下
        build.setChildPackageName("org");
        
        // 数据库信息
        build.setUrl("jdbc:mysql://127.0.0.1:3306/acuity_base_0000?serverTimezone=CTT&characterEncoding=utf8&useUnicode=true&useSSL=false&autoReconnect=true&zeroDateTimeBehavior=convertToNull");
    
    // 生成页面页面页 + 编辑页
        fileCreateConfig.setGeneratePageIndex(GenerateType.OVERRIDE);
        fileCreateConfig.setGenerateApi(GenerateType.OVERRIDE);
        fileCreateConfig.setGenerateEdit(GenerateType.OVERRIDE);
    
    // 不生成树形结构页
        fileCreateConfig.setGenerateTreeIndex(GenerateType.IGNORE);
    
        // 是否生成导入导出功能以及相关的接口
        build.setIsGenerateExportApi(true);
        return build;
    
    }
    /**
    * 注意,想要在这里直接运行,需要手动增加 mysql 驱动
    * @param args
    */
    public static void main(String[] args) {
     // 生成前端页面,一定要设置成true
     FileCreateConfig fileCreateConfig = new FileCreateConfig(null, true);
     CodeGeneratorConfig build = buildOrgEntity(fileCreateConfig);
      
     // 项目前缀,改了影响src下面所有的acuity文件夹
     build.setProjectPrefix("acuity");
      
     // 设置后台字段类型和ts字段类型的映射关系
     setMapping(build);
      
     //mysql 账号密码
     build.setUsername("root");
     build.setPassword("root");
      
     // 文件生成策略
     build.setFileCreateConfig(fileCreateConfig);
      
     // 前端代码的绝对路径
     String vuePath = "/Users/acuity/gitlab/acuity-web-plus";
     build.setProjectRootPath(vuePath);
      
     //手动指定枚举类生成的路径, 不配置,则默认跟实体类平级,存放在enumeration包下
     Set<EntityFiledType> filedTypes = new HashSet<>();
     filedTypes.addAll(Arrays.asList(
     ));
     build.setFiledTypes(filedTypes);
      
     // 自定义前端页面字段的显示样式, 不填写时,默认生成全字段
     buildVue(build);
      
     //生成代码
     VueGenerator.run(build);
    }
    
    private static void setMapping(CodeGeneratorConfig build) {
    // java 类型 和 ts 类型的对应关系
        Map<String, String> map = new HashMap<>();
        map.put("String", "string");
        map.put("Date", "string");
        map.put("LocalDateTime", "string");
        map.put("LocalDate", "string");
        map.put("LocalTime", "string");
        map.put("Long", "string");
        map.put("Integer", "number");
        map.put("BigDecimal", "string");
        map.put("BigInteger", "string");
        map.put("Float", "number");
        map.put("Double", "number");
        map.put("Boolean", "boolean");
        map.put("Enum", "Enum");
        build.setFieldTypeMapping(map);
    }
    
    private static void buildVue(CodeGeneratorConfig build) {
        CodeGeneratorConfig.Vue vue = new CodeGeneratorConfig.Vue();
    // 指定生成的代码是 acuity-web-plus项目的
        vue.setVersion(VueVersion.webplus);
    
        // 生成的代码位于前端项目 src 下的什么路径?  默认是:  src/views/acuity
    
    //   vue.setViewsPath("views" + File.separator + build.getProjectPrefix());
    
        // 程序自动根据 表设计情况 为每个字段选择合适显示规则, 若不满足,则在此添加字段后修改即可
        Map<String, Map<String, GenTableColumn>> map = buildTableFieldMap();
        vue.setTableFieldMap(map);
        build.setVue(vue);
    
    }
    // 自定义表的字段 按照上面组件显示、在编辑、列表页是否显示等。 
    private static Map<String, Map<String, GenTableColumn>> buildTableFieldMap() {
        Map<String, Map<String, GenTableColumn>> map = new HashMap<>();
        List<GenTableColumnService> list = Arrays.asList(
                // org
                new GenUser(),
                new GenStation(),
                // system
                new GenApplication(),
                new GenArea(),
                new GenDictionary(),
                new GenLoginLog(),
                new GenOptLog(),
                new GenParameter(),
                new GenRole()
        );
        list.forEach(item -> map.putAll(item.map()));
        return map;
    }
    
    public static CodeGeneratorConfig buildOrgEntity(FileCreateConfig fileCreateConfig) {
        // 配置需要生成的表
        List<String> tables = Arrays.asList(
                "c_station",
                "c_user"
        );
        CodeGeneratorConfig build = CodeGeneratorConfig.
                buildVue("authority",  // 服务名 必填
                        "c_",            // 表前缀
                        tables);
    // 模糊查询表名
    //        build.setLikeTable(new LikeTable("b\\_", SqlLike.RIGHT));
    
        //父类是Entity
        build.setSuperEntity(EntityType.ENTITY);
        
        //生成的前端页面位于 src/${build.getVue().getViewsPath()}/${childPackageName} 目录下
        build.setChildPackageName("org");
        
        // 数据库信息
        build.setUrl("jdbc:mysql://127.0.0.1:3306/acuity_base_0000?serverTimezone=CTT&characterEncoding=utf8&useUnicode=true&useSSL=false&autoReconnect=true&zeroDateTimeBehavior=convertToNull");
    
    // 生成页面页面页 + 编辑页
        fileCreateConfig.setGeneratePageIndex(GenerateType.OVERRIDE);
        fileCreateConfig.setGenerateApi(GenerateType.OVERRIDE);
        fileCreateConfig.setGenerateEdit(GenerateType.OVERRIDE);
    
    // 不生成树形结构页
        fileCreateConfig.setGenerateTreeIndex(GenerateType.IGNORE);
    
        // 是否生成导入导出功能以及相关的接口
        build.setIsGenerateExportApi(true);
        return build;
    
    }
  2. 生成代码后, 使用vscode 打开代码自动格式化一下,并删除多余的 import xxx from yyy;

  3. 注意:代码生成器只是一个辅助工具,请不要过度依赖它。 生成的代码只能满足简单的CRUD,并不满足业务需求,还需要按照业务进行调整才行。

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