若依前后端分离 代码生成修改(2)

修改若依代码生成模块

  • 目标: 多级目录情况下 实现 模块名/mvc名[controller/…]/功能名/XXXController
  • 原格式: 多级目录: 模块名/功能名/mvc名[controller/…]/xxxController
  • 修改共分为两部分: 文件路径 和 模板代码中的引用路径

若依代码生成配置

1. 修改生成文件路径

Com.ruoyi.generator.util.VelocityUtils.java#getFileName() 方法, 主要修改生成路径的字符串拼接

主要功能: public static String appendPath(String prefix, String fix, String[] elongate, String suffix) (自定义) 提供

prefix: 前缀 main/java/..包名

fix: 固定 mvc(Controller/…)

elongate: 变长 子包名多级

suffix: 后缀 构造xx.java/xx.mapper/xx.vue ….

package com.ruoyi.generator.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

import com.ruoyi.generator.config.GenConfig;
import org.apache.velocity.VelocityContext;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.constant.GenConstants;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.generator.domain.GenTable;
import com.ruoyi.generator.domain.GenTableColumn;

/**
 * 模板处理工具类
 *
 * @author ruoyi
 */
public class VelocityUtils {
    /**
     * 项目空间路径
     */
    private static final String PROJECT_PATH = "main/java";

    /**
     * mybatis空间路径
     */
    private static final String MYBATIS_PATH = "main/resources/mapper";

    /**
     * 默认上级菜单,系统工具
     */
    private static final String DEFAULT_PARENT_MENU_ID = "3";

    /**
     * 设置模板变量信息
     *
     * @return 模板列表
     */
    public static VelocityContext prepareContext(GenTable genTable) {
        String moduleName = genTable.getModuleName(); // 模块名
        String businessName = genTable.getBusinessName(); // 业务名
        String packageName = genTable.getPackageName(); // 包名
        String configPackageName = GenConfig.getPackageName();
        String[] dirs = getDirs(configPackageName, packageName);
        String dir = dirs[0];
        packageName = configPackageName+"."+dir; //修改包名

        String subpackage = getLastAsSubpackage(dirs).trim();//获得子包名
        String modulePackage = appendSubVueJsPath(dirs);
        String tplCategory = genTable.getTplCategory(); // 生成模板的类型
        String functionName = genTable.getFunctionName(); // 方法名

        VelocityContext velocityContext = new VelocityContext();
        velocityContext.put("tplCategory", genTable.getTplCategory());
        velocityContext.put("tableName", genTable.getTableName());
        velocityContext.put("functionName", StringUtils.isNotEmpty(functionName) ? functionName : "【请填写功能名称】");
        velocityContext.put("ClassName", genTable.getClassName());
        velocityContext.put("className", StringUtils.uncapitalize(genTable.getClassName()));
        velocityContext.put("moduleName", genTable.getModuleName());
        velocityContext.put("BusinessName", StringUtils.capitalize(genTable.getBusinessName()));
        velocityContext.put("businessName", genTable.getBusinessName());
        velocityContext.put("basePackage", getPackagePrefix(packageName));
        velocityContext.put("packageName", packageName);
        velocityContext.put("author", genTable.getFunctionAuthor());
        velocityContext.put("datetime", DateUtils.getDate());
        velocityContext.put("pkColumn", genTable.getPkColumn());
        velocityContext.put("importList", getImportList(genTable.getColumns()));
        velocityContext.put("permissionPrefix", getPermissionPrefix(moduleName, businessName));
        velocityContext.put("columns", genTable.getColumns());
        velocityContext.put("table", genTable);
        velocityContext.put("subpackage", subpackage);
        velocityContext.put("modulePackage", modulePackage);
        setMenuVelocityContext(velocityContext, genTable);
        if (GenConstants.TPL_TREE.equals(tplCategory)) {
            setTreeVelocityContext(velocityContext, genTable);
        }
        return velocityContext;
    }

    public static void setMenuVelocityContext(VelocityContext context, GenTable genTable) {
        String options = genTable.getOptions();
        JSONObject paramsObj = JSONObject.parseObject(options);
        String parentMenuId = getParentMenuId(paramsObj);
        context.put("parentMenuId", parentMenuId);
    }

    public static void setTreeVelocityContext(VelocityContext context, GenTable genTable) {
        String options = genTable.getOptions();
        JSONObject paramsObj = JSONObject.parseObject(options);
        String treeCode = getTreecode(paramsObj);
        String treeParentCode = getTreeParentCode(paramsObj);
        String treeName = getTreeName(paramsObj);

        context.put("treeCode", treeCode);
        context.put("treeParentCode", treeParentCode);
        context.put("treeName", treeName);
        context.put("expandColumn", getExpandColumn(genTable));
        if (paramsObj.containsKey(GenConstants.TREE_PARENT_CODE)) {
            context.put("tree_parent_code", paramsObj.getString(GenConstants.TREE_PARENT_CODE));
        }
        if (paramsObj.containsKey(GenConstants.TREE_NAME)) {
            context.put("tree_name", paramsObj.getString(GenConstants.TREE_NAME));
        }
    }

    /**
     * 获取模板信息
     *
     * @return 模板列表
     */
    public static List<String> getTemplateList(String tplCategory) {
        List<String> templates = new ArrayList<String>();
        templates.add("vm/java/domain.java.vm");
        templates.add("vm/java/mapper.java.vm");
        templates.add("vm/java/service.java.vm");
        templates.add("vm/java/serviceImpl.java.vm");
        templates.add("vm/java/controller.java.vm");
        templates.add("vm/xml/mapper.xml.vm");
        templates.add("vm/sql/sql.vm");
        templates.add("vm/js/api.js.vm");
        if (GenConstants.TPL_CRUD.equals(tplCategory)) {
            templates.add("vm/vue/index.vue.vm");
        } else if (GenConstants.TPL_TREE.equals(tplCategory)) {
            templates.add("vm/vue/index-tree.vue.vm");
        }
        return templates;
    }

    /**
     * 获取文件名
     */
    public static String getFileName(String template, GenTable genTable) {
        // 文件名称
        String fileName = "";
        // 包路径 com.ruoyi.waidaipc系统名 下一级是 系统的的模块名 剩下的除去最后一级 都是子包
        String packageName = genTable.getPackageName(); //代码生成表中保存的 包名

        String packageNameConfig = GenConfig.getPackageName();//系统名

        // 模块名
        String moduleName = genTable.getModuleName();
        // 大写类名
        String className = genTable.getClassName();
        // 业务名称
        String businessName = genTable.getBusinessName();

        String[] dirs = getDirs(packageNameConfig, packageName);
        String moduleDir = dirs[0];//项目模块包
        String[] subDirs = Arrays.copyOfRange(dirs,1,dirs.length);//动态 拼子包

        String javaPath = PROJECT_PATH + "/" + StringUtils.replace(packageNameConfig, ".", "/")+"/"+moduleDir;
        String mybatisPath = MYBATIS_PATH;
        String vuePath = "vue";


        if (template.contains("domain.java.vm")) {
            fileName = appendPath(javaPath, "/domain", subDirs, "/" + className + ".java");
        } else if (template.contains("mapper.java.vm")) {
            fileName = appendPath(javaPath, "/mapper", subDirs, "/" + className + "Mapper.java");
        } else if (template.contains("service.java.vm")) {
            fileName = appendPath(javaPath, "/service", subDirs, "/I" + className + "Service.java");
        } else if (template.contains("serviceImpl.java.vm")) {
            fileName = appendPath(javaPath, "/service/impl", subDirs, "/" + className + "ServiceImpl.java");
        } else if (template.contains("controller.java.vm")) {
            fileName = appendPath(javaPath, "/controller", subDirs, "/" + className + "Controller.java");
        } else if (template.contains("mapper.xml.vm")) {
            fileName = appendPath(mybatisPath, "", dirs , "/" + className + "Mapper.xml");//全部路径少最后一级
        } else if (template.contains("sql.vm")) {
            fileName = appendPath("", "", null, businessName + "Menu.sql");//无需目录
        } else if (template.contains("api.js.vm")) {
            fileName = appendPath(vuePath, "/api", dirs, "/" + businessName + ".js");//全部路径少最后一级
        } else if (template.contains("index.vue.vm")) {
            fileName = appendPath(vuePath, "/views", dirs,  "/"+businessName+"/index.vue");//需要全部路径
        } else if (template.contains("index-tree.vue.vm")) {
            fileName = appendPath(vuePath, "/views", dirs,  "/"+businessName+"/index.vue");//需要全部路径
        }
        return fileName;
    }

    /**
     * 获取包前缀
     *
     * @param packageName 包名称
     * @return 包前缀名称
     */
    public static String getPackagePrefix(String packageName) {
        int lastIndex = packageName.lastIndexOf(".");
        String basePackage = StringUtils.substring(packageName, 0, lastIndex);
        return basePackage;
    }

    /**
     * 根据列类型获取导入包
     *
     * @param columns 列集合
     * @return 返回需要导入的包列表
     */
    public static HashSet<String> getImportList(List<GenTableColumn> columns) {
        HashSet<String> importList = new HashSet<String>();
        for (GenTableColumn column : columns) {
            if (!column.isSuperColumn() && GenConstants.TYPE_DATE.equals(column.getJavaType())) {
                importList.add("java.util.Date");
                importList.add("com.fasterxml.jackson.annotation.JsonFormat");
            } else if (!column.isSuperColumn() && GenConstants.TYPE_BIGDECIMAL.equals(column.getJavaType())) {
                importList.add("java.math.BigDecimal");
            }
        }
        return importList;
    }

    /**
     * 获取权限前缀
     *
     * @param moduleName   模块名称
     * @param businessName 业务名称
     * @return 返回权限前缀
     */
    public static String getPermissionPrefix(String moduleName, String businessName) {
        return StringUtils.format("{}:{}", moduleName, businessName);
    }

    /**
     * 获取上级菜单ID字段
     *
     * @param paramsObj 生成其他选项
     * @return 上级菜单ID字段
     */
    public static String getParentMenuId(JSONObject paramsObj) {
        if (StringUtils.isNotEmpty(paramsObj) && paramsObj.containsKey(GenConstants.PARENT_MENU_ID)) {
            return paramsObj.getString(GenConstants.PARENT_MENU_ID);
        }
        return DEFAULT_PARENT_MENU_ID;
    }

    /**
     * 获取树编码
     *
     * @param paramsObj 生成其他选项
     * @return 树编码
     */
    public static String getTreecode(JSONObject paramsObj) {
        if (paramsObj.containsKey(GenConstants.TREE_CODE)) {
            return StringUtils.toCamelCase(paramsObj.getString(GenConstants.TREE_CODE));
        }
        return StringUtils.EMPTY;
    }

    /**
     * 获取树父编码
     *
     * @param paramsObj 生成其他选项
     * @return 树父编码
     */
    public static String getTreeParentCode(JSONObject paramsObj) {
        if (paramsObj.containsKey(GenConstants.TREE_PARENT_CODE)) {
            return StringUtils.toCamelCase(paramsObj.getString(GenConstants.TREE_PARENT_CODE));
        }
        return StringUtils.EMPTY;
    }

    /**
     * 获取树名称
     *
     * @param paramsObj 生成其他选项
     * @return 树名称
     */
    public static String getTreeName(JSONObject paramsObj) {
        if (paramsObj.containsKey(GenConstants.TREE_NAME)) {
            return StringUtils.toCamelCase(paramsObj.getString(GenConstants.TREE_NAME));
        }
        return StringUtils.EMPTY;
    }

    /**
     * 获取需要在哪一列上面显示展开按钮
     *
     * @param genTable 业务表对象
     * @return 展开按钮列序号
     */
    public static int getExpandColumn(GenTable genTable) {
        String options = genTable.getOptions();
        JSONObject paramsObj = JSONObject.parseObject(options);
        String treeName = paramsObj.getString(GenConstants.TREE_NAME);
        int num = 0;
        for (GenTableColumn column : genTable.getColumns()) {
            if (column.isList()) {
                num++;
                String columnName = column.getColumnName();
                if (columnName.equals(treeName)) {
                    break;
                }
            }
        }
        return num;
    }

    /**
     * 表单中设置的包路径的最后一级 作为Controller, Service, Mapper 子包名
     *
     * @param dirs 表单中设置的包路径
     * @return
     */
    public static String getLastAsSubpackage(String dirs []) {
        StringBuilder sb = new StringBuilder();
        if (dirs!=null && dirs.length!=0){
            for (int i = 1; i < dirs.length; i++) {
                sb.append(".").append(dirs[i]);
            }
        }
        return sb.toString();
    }


    public static String[] getDirs(String configPackage, String dbPackage) {
        String[] split1 = configPackage.split("\\.");
        String[] split2 = dbPackage.split("\\.");
        String[] strings = Arrays.copyOfRange(split2, split1.length, split2.length);
        return strings;
    }

    public static String appendPath(String prefix, String fix, String[] elongate, String suffix) {
        StringBuilder sb = new StringBuilder();
        sb.append(prefix).append(fix);
        if ( elongate != null && elongate .length !=0) {
            for (String s : elongate) {
                sb.append("/").append(s);
            }
        }
        sb.append(suffix);
        return sb.toString();
    }

    /**
     *  生成 vue 导入js的多级目录
     * @param dirs
     * @return
     */
    public static String appendSubVueJsPath(String [] dirs){
        StringBuilder sb = new StringBuilder();
        if(dirs!=null&&dirs.length!=0){
            for (int i = 0; i < dirs.length-1; i++) {
                sb.append("/").append(dirs[i]);
            }
        }
        return sb.toString();
    }
}

2. 修改模板代码引用路径

文件所在包: ruoyi-generator/src/main/resources/vm/xx/xx.vm

Com.ruoyi.generator.util.VelocityUtils.java#prepareContext() 方法, 主要提供模板所需参数的上下文 添加额外参数 以供模块获取

velocityContext.put("subpackage", subpackage); //java 导包时 模板需要多级的参数

velocityContext.put("modulePackage", modulePackage);//vue 到入 js包时 需要的多级参数

#evaluate("#if($!{subpackage}!='')$!{subpackage}#end") //动态计算 是否有值进行拼接 

java类的修改 (Class , mapper controller,service ,impl) .vm文件

package ${packageName}.controller#evaluate("#if($!{subpackage}!='')$!{subpackage}#end");

import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import ${packageName}.domain#evaluate("#if($!{subpackage}!='')$!{subpackage}#end").${ClassName};//修改这里
import ${packageName}.service#evaluate("#if($!{subpackage}!='')$!{subpackage}#end").I${ClassName}Service; //修改这里
import com.ruoyi.common.utils.poi.ExcelUtil;
#if($table.crud)
import com.ruoyi.common.core.page.TableDataInfo;
#elseif($table.tree)
#end

vue的修改

//${modulePackage}
import { list${BusinessName}, get${BusinessName}, del${BusinessName}, add${BusinessName}, update${BusinessName}, export${BusinessName} } from "@/api${modulePackage}/${moduleName}/${businessName}";
posted @ 2023-04-18 12:20  ACMAN-Mr.Lee  阅读(844)  评论(0编辑  收藏  举报