子模块

ruoyi-generator

导入表信息

导入表信息时系统会默认读取ruoyi-generator/src/main/resources/generator.yml路径下的配置,配置如下

# 代码生成
gen:
  # 作者
  author: ruoyi
  # 默认生成包路径 system 需改成自己的模块名称 如 system monitor tool
  packageName: com.ruoyi.demo
  # 自动去除表前缀,默认是false
  autoRemovePre: true
  # 表前缀(生成类名不会包含表前缀,多个用逗号分隔)
  tablePrefix: sys_

生成前配置

   自定义路径的方式可以直接生成到指定的子模块目录下

模板解析

代码模板存放位置,模板中填充的字段来自
ruoyi-generator/src/main/java/com/ruoyi/generator/util/VelocityUtils.java
中的 prepareContext() 方法

prepareContext()中实现了字段的值注入到VelocityContext对象中会被

ruoyi-generator/src/main/java/com/ruoyi/generator/service/GenTableServiceImpl.java
中的generatorCode()方法调用
generatorCode()中把VelocityContext
对象中的数据写入到Template模板里

自定义生成路径

generatorCode() 方法中 有过滤 “sql.vm”, “api.js.vm”, “index.vue.vm”, “index-tree.vue.vm” 文件的代码,在实际使用时可以修改下过滤文件的,比如去除 “api.js.vm”, “index.vue.vm”,
这样系统就会生成vue2 相关的代码

鉴于ruoyi的代码生成模块在前台设置生成路径时需要每次都填写,故此在后端.yml文件中实现路径存储并更改部分代码实现后端自定义生成路径

generator.yml

增加代码的存放路径
java代码存放路径
javaPath: F:/Amy/project/RuoYi-Vue-demo/java/ruoyi-demo/src

vue代码存放路径
vuePath: F:/Amy/project/RuoYi-Vue-demo/vue/ruoyi-ui/src

# 代码生成
gen:
  # 作者
  author: ruoyi
  # 默认生成包路径 system 需改成自己的模块名称 如 system monitor tool
  packageName: com.ruoyi.demo
  # 自动去除表前缀,默认是false
  autoRemovePre: false
  # 表前缀(生成类名不会包含表前缀,多个用逗号分隔)
  tablePrefix: sys_
  # 代码存放路径
  javaPath: F:/Amy/project/RuoYi-Vue-demo/java/ruoyi-demo/src
  # vue代码路径
  vuePath: F:/Amy/project/RuoYi-Vue-demo/vue/ruoyi-ui/src

GenConfig.class读取代码生成相关配置

增加存放路径的读取
private static String javaPath;
private static String vuePath;

/**
 * 读取代码生成相关配置
 *
 * @author ruoyi
 */
@Component
@ConfigurationProperties(prefix = "gen")
@PropertySource(value = {"classpath:generator.yml"})
public class GenConfig {
    /**
     * 作者
     */
    public static String author;

    /**
     * 生成包路径
     */
    public static String packageName;

    /**
     * 自动去除表前缀,默认是false
     */
    public static boolean autoRemovePre;

    /**
     * 表前缀(类名不会包含表前缀)
     */
    public static String tablePrefix;

    /**
     * 代码生成路径
     */
    private static String javaPath;

    /**
     * 代码生成路径
     */
    private static String vuePath;



    public static String getAuthor() {
        return author;
    }

    @Value("${author}")
    public void setAuthor(String author) {
        GenConfig.author = author;
    }

    public static String getPackageName() {
        return packageName;
    }

    @Value("${packageName}")
    public void setPackageName(String packageName) {
        GenConfig.packageName = packageName;
    }

    public static boolean getAutoRemovePre() {
        return autoRemovePre;
    }

    @Value("${autoRemovePre}")
    public void setAutoRemovePre(boolean autoRemovePre) {
        GenConfig.autoRemovePre = autoRemovePre;
    }

    public static String getTablePrefix() {
        return tablePrefix;
    }

    @Value("${tablePrefix}")
    public void setTablePrefix(String tablePrefix) {
        GenConfig.tablePrefix = tablePrefix;
    }



    @Value("${javaPath}")
    public void setJavaPath(String path) {
        GenConfig.javaPath = path;
    }

    public static String getJavaPath() {
        return javaPath;
    }

    @Value("${vuePath}")
    public void setVuePath(String vuePath) {
        GenConfig.vuePath = vuePath;
    }

    public static String getVuePath() {
        return vuePath;
    }

}

生成代码(自定义路径)generatorCode()

如果table.getGenPath()的值等于"/",

来自com.ruoyi.generator.service.GenTableServiceImpl.class

  /**
     * 生成代码(自定义路径)
     *
     * @param tableName 表名称
     */
    @Override
    public void generatorCode(String tableName) {
        // 查询表信息
        GenTable table = genTableMapper.selectGenTableByName(tableName);
        //如果地址是默认则使用配置文件中的路径地址
        if (StringUtils.equals(table.getGenPath(), "/")) {
            table.setGenPath(GenConfig.getJavaPath());
            table.setVuePath(GenConfig.getVuePath());
        }


        // 设置主子表信息
        setSubTable(table);
        // 设置主键列信息
        setPkColumn(table);

        VelocityInitializer.initVelocity();

        VelocityContext context = VelocityUtils.prepareContext(table);

        // 获取模板列表
        List<String> templates = VelocityUtils.getTemplateList(table.getTplCategory());
        for (String template : templates) {
            //过滤一些文件
            if (!StringUtils.containsAny(template, "sql.vm", "api.js.vm", "index.vue.vm", "index-tree.vue.vm")) {
                // 渲染模板
                StringWriter sw = new StringWriter();
                Template tpl = Velocity.getTemplate(template, Constants.UTF8);
                tpl.merge(context, sw);
                try {
                    String path = getGenPath(table, template, table.getGenPath());
                    FileUtils.writeStringToFile(new File(path), sw.toString(), CharsetKit.UTF_8);
                } catch (IOException e) {
                    throw new ServiceException("渲染模板失败,表名:" + table.getTableName());
                }
            }

            //过滤一些文件
            if (StringUtils.containsAny(template, "api.js.vm", "index.vue.vm")) {
                // 渲染模板
                StringWriter sw = new StringWriter();
                Template tpl = Velocity.getTemplate(template, Constants.UTF8);
                tpl.merge(context, sw);
                try {
                    String path = getGenPath(table, template, table.getVuePath());
                    FileUtils.writeStringToFile(new File(path), sw.toString(), CharsetKit.UTF_8);
                } catch (IOException e) {
                    throw new ServiceException("渲染模板失败,表名:" + table.getTableName());
                }
            }
        }
    }

重写getGenPath()获取代码生成地址

 /**
     * 获取代码生成地址
     *
     * @param table    业务表信息
     * @param template 模板文件路径
     * @param genPath  要生成代码的路径
     * @return 生成地址
     */
    public static String getGenPath(GenTable table, String template, String genPath) {
        return genPath + File.separator + VelocityUtils.getFileNameNoVuePath(template, table);
    }

重写getFileNameNoVuePath()

/**
     * 获取文件名
     */
    public static String getFileNameNoVuePath(String template, GenTable genTable) {
        // 文件名称
        String fileName = "";
        // 包路径
        String packageName = genTable.getPackageName();
        // 模块名
        String moduleName = genTable.getModuleName();
        // 大写类名
        String className = genTable.getClassName();
        // 业务名称
        String businessName = genTable.getBusinessName();

        String javaPath = PROJECT_PATH + "/" + StringUtils.replace(packageName, ".", "/");
        String mybatisPath = MYBATIS_PATH + "/" + moduleName;

        if (template.contains("domain.java.vm")) {
            fileName = StringUtils.format("{}/domain/{}.java", javaPath, className);
        }
        if (template.contains("sub-domain.java.vm") && StringUtils.equals(GenConstants.TPL_SUB, genTable.getTplCategory())) {
            fileName = StringUtils.format("{}/domain/{}.java", javaPath, genTable.getSubTable().getClassName());
        } else if (template.contains("mapper.java.vm")) {
            fileName = StringUtils.format("{}/mapper/{}Mapper.java", javaPath, className);
        } else if (template.contains("service.java.vm")) {
            fileName = StringUtils.format("{}/service/I{}Service.java", javaPath, className);
        } else if (template.contains("serviceImpl.java.vm")) {
            fileName = StringUtils.format("{}/service/impl/{}ServiceImpl.java", javaPath, className);
        } else if (template.contains("controller.java.vm")) {
            fileName = StringUtils.format("{}/controller/{}Controller.java", javaPath, className);
        } else if (template.contains("mapper.xml.vm")) {
            fileName = StringUtils.format("{}/{}Mapper.xml", mybatisPath, className);
        } else if (template.contains("sql.vm")) {
            fileName = businessName + "Menu.sql";
        } else if (template.contains("api.js.vm")) {
            fileName = StringUtils.format("/api/{}/{}.js", moduleName, businessName);
        } else if (template.contains("index.vue.vm")) {
            fileName = StringUtils.format("/views/{}/{}/index.vue", moduleName, businessName);
        } else if (template.contains("index-tree.vue.vm")) {
            fileName = StringUtils.format("/views/{}/{}/index.vue", moduleName, businessName);
        }
        return fileName;
    }
Logo

快速构建 Web 应用程序

更多推荐