完成代码生成器的 sql 生成

This commit is contained in:
YunaiV 2021-02-12 12:58:41 +08:00
parent b8351f50f2
commit 937c51f4ec
14 changed files with 421 additions and 520 deletions

View File

@ -1,30 +1,30 @@
package com.ruoyi.generator.config; package com.ruoyi.generator.config;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource; import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
/** /**
* 读取代码生成相关配置 * 读取代码生成相关配置
* *
* @author ruoyi * @author ruoyi
*/ */
@Component @Component
@ConfigurationProperties(prefix = "gen") @ConfigurationProperties(prefix = "gen")
@PropertySource(value = { "classpath:generator.yml" }) @PropertySource(value = { "classpath:generator.yml" })
public class GenConfig { public class GenConfig {
/** 作者 */ /** 作者 */
public static String author; public static String author;
/** 生成包路径 */ /** 生成包路径 */
public static String packageName; public static String packageName;
/** 自动去除表前缀默认是false */ /** 自动去除表前缀默认是false */
public static boolean autoRemovePre; public static boolean autoRemovePre;
/** 表前缀(类名不会包含表前缀) */ /** 表前缀(类名不会包含表前缀) */
public static String tablePrefix; public static String tablePrefix;
} }

View File

@ -1,55 +1,45 @@
package com.ruoyi.generator.domain; package com.ruoyi.generator.domain;
import java.util.List; import java.util.List;
import javax.validation.Valid; import javax.validation.Valid;
import javax.validation.constraints.NotBlank; import javax.validation.constraints.NotBlank;
import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.ArrayUtils;
import com.ruoyi.common.constant.GenConstants; import com.ruoyi.common.constant.GenConstants;
import com.ruoyi.common.core.domain.BaseEntity; import com.ruoyi.common.core.domain.BaseEntity;
import com.ruoyi.common.utils.StringUtils; import com.ruoyi.common.utils.StringUtils;
/** /**
* 业务表 gen_table * 业务表 gen_table
* *
* @author ruoyi * @author ruoyi
*/ */
public class GenTable extends BaseEntity { public class GenTable extends BaseEntity {
/** /**
* 生成包路径 * 生成包路径
*/ */
@NotBlank(message = "生成包路径不能为空") @NotBlank(message = "生成包路径不能为空")
private String packageName; private String packageName;
/** /**
* 其它生成选项 * 其它生成选项
*/ */
private String options; private String options;
/** /**
* 树编码字段 * 树编码字段
*/ */
private String treeCode; private String treeCode;
/** /**
* 树父编码字段 * 树父编码字段
*/ */
private String treeParentCode; private String treeParentCode;
/** /**
* 树名称字段 * 树名称字段
*/ */
private String treeName; private String treeName;
/** }
* 上级菜单ID字段
*/
private String parentMenuId;
/**
* 上级菜单名称字段
*/
private String parentMenuName;
}

View File

@ -1,272 +1,265 @@
package com.ruoyi.generator.util; package com.ruoyi.generator.util;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import org.apache.velocity.VelocityContext; import org.apache.velocity.VelocityContext;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.constant.GenConstants; import com.ruoyi.common.constant.GenConstants;
import com.ruoyi.common.utils.DateUtils; import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils; import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.generator.domain.GenTable; import com.ruoyi.generator.domain.GenTable;
import com.ruoyi.generator.domain.GenTableColumn; import com.ruoyi.generator.domain.GenTableColumn;
/** /**
* 模板处理工具类 * 模板处理工具类
* *
* @author ruoyi * @author ruoyi
*/ */
public class VelocityUtils { public class VelocityUtils {
/** /**
* 默认上级菜单系统工具 * 默认上级菜单系统工具
*/ */
private static final String DEFAULT_PARENT_MENU_ID = "3"; private static final String DEFAULT_PARENT_MENU_ID = "3";
/** /**
* 设置模板变量信息 * 设置模板变量信息
* *
* @return 模板列表 * @return 模板列表
*/ */
public static VelocityContext prepareContext(GenTable genTable) { public static VelocityContext prepareContext(GenTable genTable) {
String moduleName = genTable.getModuleName(); String moduleName = genTable.getModuleName();
String businessName = genTable.getBusinessName(); String businessName = genTable.getBusinessName();
String packageName = genTable.getPackageName(); String packageName = genTable.getPackageName();
String tplCategory = genTable.getTplCategory(); String tplCategory = genTable.getTplCategory();
String functionName = genTable.getFunctionName(); String functionName = genTable.getFunctionName();
VelocityContext velocityContext = new VelocityContext(); VelocityContext velocityContext = new VelocityContext();
velocityContext.put("tplCategory", genTable.getTplCategory()); velocityContext.put("tplCategory", genTable.getTplCategory());
velocityContext.put("tableName", genTable.getTableName()); velocityContext.put("tableName", genTable.getTableName());
velocityContext.put("functionName", StringUtils.isNotEmpty(functionName) ? functionName : "【请填写功能名称】"); velocityContext.put("functionName", StringUtils.isNotEmpty(functionName) ? functionName : "【请填写功能名称】");
velocityContext.put("ClassName", genTable.getClassName()); velocityContext.put("ClassName", genTable.getClassName());
velocityContext.put("className", StringUtils.uncapitalize(genTable.getClassName())); velocityContext.put("className", StringUtils.uncapitalize(genTable.getClassName()));
velocityContext.put("moduleName", genTable.getModuleName()); velocityContext.put("moduleName", genTable.getModuleName());
velocityContext.put("BusinessName", StringUtils.capitalize(genTable.getBusinessName())); velocityContext.put("BusinessName", StringUtils.capitalize(genTable.getBusinessName()));
velocityContext.put("businessName", genTable.getBusinessName()); velocityContext.put("businessName", genTable.getBusinessName());
velocityContext.put("basePackage", getPackagePrefix(packageName)); velocityContext.put("basePackage", getPackagePrefix(packageName));
velocityContext.put("packageName", packageName); velocityContext.put("packageName", packageName);
velocityContext.put("author", genTable.getFunctionAuthor()); velocityContext.put("author", genTable.getFunctionAuthor());
velocityContext.put("datetime", DateUtils.getDate()); velocityContext.put("datetime", DateUtils.getDate());
velocityContext.put("pkColumn", genTable.getPkColumn()); velocityContext.put("pkColumn", genTable.getPkColumn());
velocityContext.put("importList", getImportList(genTable.getColumns())); velocityContext.put("importList", getImportList(genTable.getColumns()));
velocityContext.put("permissionPrefix", getPermissionPrefix(moduleName, businessName)); velocityContext.put("permissionPrefix", getPermissionPrefix(moduleName, businessName));
velocityContext.put("columns", genTable.getColumns()); velocityContext.put("columns", genTable.getColumns());
velocityContext.put("table", genTable); velocityContext.put("table", genTable);
setMenuVelocityContext(velocityContext, genTable); setMenuVelocityContext(velocityContext, genTable);
if (GenConstants.TPL_TREE.equals(tplCategory)) { if (GenConstants.TPL_TREE.equals(tplCategory)) {
setTreeVelocityContext(velocityContext, genTable); setTreeVelocityContext(velocityContext, genTable);
} }
return velocityContext; return velocityContext;
} }
public static void setMenuVelocityContext(VelocityContext context, GenTable genTable) { public static void setTreeVelocityContext(VelocityContext context, GenTable genTable) {
String options = genTable.getOptions(); String options = genTable.getOptions();
JSONObject paramsObj = JSONObject.parseObject(options); JSONObject paramsObj = JSONObject.parseObject(options);
String parentMenuId = getParentMenuId(paramsObj); String treeCode = getTreecode(paramsObj);
context.put("parentMenuId", parentMenuId); String treeParentCode = getTreeParentCode(paramsObj);
} String treeName = getTreeName(paramsObj);
public static void setTreeVelocityContext(VelocityContext context, GenTable genTable) { context.put("treeCode", treeCode);
String options = genTable.getOptions(); context.put("treeParentCode", treeParentCode);
JSONObject paramsObj = JSONObject.parseObject(options); context.put("treeName", treeName);
String treeCode = getTreecode(paramsObj); context.put("expandColumn", getExpandColumn(genTable));
String treeParentCode = getTreeParentCode(paramsObj); if (paramsObj.containsKey(GenConstants.TREE_PARENT_CODE)) {
String treeName = getTreeName(paramsObj); context.put("tree_parent_code", paramsObj.getString(GenConstants.TREE_PARENT_CODE));
}
context.put("treeCode", treeCode); if (paramsObj.containsKey(GenConstants.TREE_NAME)) {
context.put("treeParentCode", treeParentCode); context.put("tree_name", paramsObj.getString(GenConstants.TREE_NAME));
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");
* @return 模板列表 templates.add("vm/java/serviceImpl.java.vm");
*/ templates.add("vm/java/controller.java.vm");
public static List<String> getTemplateList(String tplCategory) { templates.add("vm/xml/mapper.xml.vm");
List<String> templates = new ArrayList<String>(); templates.add("vm/sql/sql.vm");
templates.add("vm/java/domain.java.vm"); templates.add("vm/js/api.js.vm");
templates.add("vm/java/mapper.java.vm"); if (GenConstants.TPL_CRUD.equals(tplCategory)) {
templates.add("vm/java/service.java.vm"); templates.add("vm/vue/index.vue.vm");
templates.add("vm/java/serviceImpl.java.vm"); } else if (GenConstants.TPL_TREE.equals(tplCategory)) {
templates.add("vm/java/controller.java.vm"); templates.add("vm/vue/index-tree.vue.vm");
templates.add("vm/xml/mapper.xml.vm"); }
templates.add("vm/sql/sql.vm"); return templates;
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"); */
} public static String getFileName(String template, GenTable genTable) {
return templates; // 文件名称
} String fileName = "";
// 包路径
/** String packageName = genTable.getPackageName();
* 获取文件名 // 模块名
*/ String moduleName = genTable.getModuleName();
public static String getFileName(String template, GenTable genTable) { // 大写类名
// 文件名称 String className = genTable.getClassName();
String fileName = ""; // 业务名称
// 包路径 String businessName = genTable.getBusinessName();
String packageName = genTable.getPackageName();
// 模块名 String javaPath = PROJECT_PATH + "/" + StringUtils.replace(packageName, ".", "/");
String moduleName = genTable.getModuleName(); String mybatisPath = MYBATIS_PATH + "/" + moduleName;
// 大写类名 String vuePath = "vue";
String className = genTable.getClassName();
// 业务名称 if (template.contains("domain.java.vm")) {
String businessName = genTable.getBusinessName(); fileName = StringUtils.format("{}/domain/{}.java", javaPath, className);
} else if (template.contains("mapper.java.vm")) {
String javaPath = PROJECT_PATH + "/" + StringUtils.replace(packageName, ".", "/"); fileName = StringUtils.format("{}/mapper/{}Mapper.java", javaPath, className);
String mybatisPath = MYBATIS_PATH + "/" + moduleName; } else if (template.contains("service.java.vm")) {
String vuePath = "vue"; fileName = StringUtils.format("{}/service/I{}Service.java", javaPath, className);
} else if (template.contains("serviceImpl.java.vm")) {
if (template.contains("domain.java.vm")) { fileName = StringUtils.format("{}/service/impl/{}ServiceImpl.java", javaPath, className);
fileName = StringUtils.format("{}/domain/{}.java", javaPath, className); } else if (template.contains("controller.java.vm")) {
} else if (template.contains("mapper.java.vm")) { fileName = StringUtils.format("{}/controller/{}Controller.java", javaPath, className);
fileName = StringUtils.format("{}/mapper/{}Mapper.java", javaPath, className); } else if (template.contains("mapper.xml.vm")) {
} else if (template.contains("service.java.vm")) { fileName = StringUtils.format("{}/{}Mapper.xml", mybatisPath, className);
fileName = StringUtils.format("{}/service/I{}Service.java", javaPath, className); } else if (template.contains("sql.vm")) {
} else if (template.contains("serviceImpl.java.vm")) { fileName = businessName + "Menu.sql";
fileName = StringUtils.format("{}/service/impl/{}ServiceImpl.java", javaPath, className); } else if (template.contains("api.js.vm")) {
} else if (template.contains("controller.java.vm")) { fileName = StringUtils.format("{}/api/{}/{}.js", vuePath, moduleName, businessName);
fileName = StringUtils.format("{}/controller/{}Controller.java", javaPath, className); } else if (template.contains("index.vue.vm")) {
} else if (template.contains("mapper.xml.vm")) { fileName = StringUtils.format("{}/views/{}/{}/index.vue", vuePath, moduleName, businessName);
fileName = StringUtils.format("{}/{}Mapper.xml", mybatisPath, className); } else if (template.contains("index-tree.vue.vm")) {
} else if (template.contains("sql.vm")) { fileName = StringUtils.format("{}/views/{}/{}/index.vue", vuePath, moduleName, businessName);
fileName = businessName + "Menu.sql"; }
} else if (template.contains("api.js.vm")) { return fileName;
fileName = StringUtils.format("{}/api/{}/{}.js", vuePath, moduleName, businessName); }
} else if (template.contains("index.vue.vm")) {
fileName = StringUtils.format("{}/views/{}/{}/index.vue", vuePath, moduleName, businessName); /**
} else if (template.contains("index-tree.vue.vm")) { * 获取包前缀
fileName = StringUtils.format("{}/views/{}/{}/index.vue", vuePath, moduleName, businessName); *
} * @param packageName 包名称
return fileName; * @return 包前缀名称
} */
public static String getPackagePrefix(String packageName) {
/** int lastIndex = packageName.lastIndexOf(".");
* 获取包前缀 String basePackage = StringUtils.substring(packageName, 0, lastIndex);
* return basePackage;
* @param packageName 包名称 }
* @return 包前缀名称
*/ /**
public static String getPackagePrefix(String packageName) { * 根据列类型获取导入包
int lastIndex = packageName.lastIndexOf("."); *
String basePackage = StringUtils.substring(packageName, 0, lastIndex); * @param columns 列集合
return basePackage; * @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())) {
* @param columns 列集合 importList.add("java.util.Date");
* @return 返回需要导入的包列表 importList.add("com.fasterxml.jackson.annotation.JsonFormat");
*/ } else if (!column.isSuperColumn() && GenConstants.TYPE_BIGDECIMAL.equals(column.getJavaType())) {
public static HashSet<String> getImportList(List<GenTableColumn> columns) { importList.add("java.math.BigDecimal");
HashSet<String> importList = new HashSet<String>(); }
for (GenTableColumn column : columns) { }
if (!column.isSuperColumn() && GenConstants.TYPE_DATE.equals(column.getJavaType())) { return importList;
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"); * 获取权限前缀
} *
} * @param moduleName 模块名称
return importList; * @param businessName 业务名称
} * @return 返回权限前缀
*/
/** public static String getPermissionPrefix(String moduleName, String businessName) {
* 获取权限前缀 return StringUtils.format("{}:{}", moduleName, businessName);
* }
* @param moduleName 模块名称
* @param businessName 业务名称 /**
* @return 返回权限前缀 * 获取上级菜单ID字段
*/ *
public static String getPermissionPrefix(String moduleName, String businessName) { * @param paramsObj 生成其他选项
return StringUtils.format("{}:{}", moduleName, businessName); * @return 上级菜单ID字段
} */
public static String getParentMenuId(JSONObject paramsObj) {
/** if (StringUtils.isNotEmpty(paramsObj) && paramsObj.containsKey(GenConstants.PARENT_MENU_ID)) {
* 获取上级菜单ID字段 return paramsObj.getString(GenConstants.PARENT_MENU_ID);
* }
* @param paramsObj 生成其他选项 return DEFAULT_PARENT_MENU_ID;
* @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); *
} * @param paramsObj 生成其他选项
return DEFAULT_PARENT_MENU_ID; * @return 树编码
} */
public static String getTreecode(JSONObject paramsObj) {
/** if (paramsObj.containsKey(GenConstants.TREE_CODE)) {
* 获取树编码 return StringUtils.toCamelCase(paramsObj.getString(GenConstants.TREE_CODE));
* }
* @param paramsObj 生成其他选项 return StringUtils.EMPTY;
* @return 树编码 }
*/
public static String getTreecode(JSONObject paramsObj) { /**
if (paramsObj.containsKey(GenConstants.TREE_CODE)) { * 获取树父编码
return StringUtils.toCamelCase(paramsObj.getString(GenConstants.TREE_CODE)); *
} * @param paramsObj 生成其他选项
return StringUtils.EMPTY; * @return 树父编码
} */
public static String getTreeParentCode(JSONObject paramsObj) {
/** if (paramsObj.containsKey(GenConstants.TREE_PARENT_CODE)) {
* 获取树父编码 return StringUtils.toCamelCase(paramsObj.getString(GenConstants.TREE_PARENT_CODE));
* }
* @param paramsObj 生成其他选项 return StringUtils.EMPTY;
* @return 树父编码 }
*/
public static String getTreeParentCode(JSONObject paramsObj) { /**
if (paramsObj.containsKey(GenConstants.TREE_PARENT_CODE)) { * 获取树名称
return StringUtils.toCamelCase(paramsObj.getString(GenConstants.TREE_PARENT_CODE)); *
} * @param paramsObj 生成其他选项
return StringUtils.EMPTY; * @return 树名称
} */
public static String getTreeName(JSONObject paramsObj) {
/** if (paramsObj.containsKey(GenConstants.TREE_NAME)) {
* 获取树名称 return StringUtils.toCamelCase(paramsObj.getString(GenConstants.TREE_NAME));
* }
* @param paramsObj 生成其他选项 return StringUtils.EMPTY;
* @return 树名称 }
*/
public static String getTreeName(JSONObject paramsObj) { /**
if (paramsObj.containsKey(GenConstants.TREE_NAME)) { * 获取需要在哪一列上面显示展开按钮
return StringUtils.toCamelCase(paramsObj.getString(GenConstants.TREE_NAME)); *
} * @param genTable 业务表对象
return StringUtils.EMPTY; * @return 展开按钮列序号
} */
public static int getExpandColumn(GenTable genTable) {
/** String options = genTable.getOptions();
* 获取需要在哪一列上面显示展开按钮 JSONObject paramsObj = JSONObject.parseObject(options);
* String treeName = paramsObj.getString(GenConstants.TREE_NAME);
* @param genTable 业务表对象 int num = 0;
* @return 展开按钮列序号 for (GenTableColumn column : genTable.getColumns()) {
*/ if (column.isList()) {
public static int getExpandColumn(GenTable genTable) { num++;
String options = genTable.getOptions(); String columnName = column.getColumnName();
JSONObject paramsObj = JSONObject.parseObject(options); if (columnName.equals(treeName)) {
String treeName = paramsObj.getString(GenConstants.TREE_NAME); break;
int num = 0; }
for (GenTableColumn column : genTable.getColumns()) { }
if (column.isList()) { }
num++; return num;
String columnName = column.getColumnName(); }
if (columnName.equals(treeName)) { }
break;
}
}
}
return num;
}
}

View File

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

View File

@ -1,114 +0,0 @@
package ${packageName}.controller;
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.${ClassName};
import ${packageName}.service.I${ClassName}Service;
import com.ruoyi.common.utils.poi.ExcelUtil;
#if($table.crud)
import com.ruoyi.common.core.page.TableDataInfo;
#elseif($table.tree)
#end
/**
* ${functionName}Controller
*
* @author ${author}
* @date ${datetime}
*/
@RestController
@RequestMapping("/${moduleName}/${businessName}")
public class ${ClassName}Controller extends BaseController
{
@Autowired
private I${ClassName}Service ${className}Service;
/**
* 查询${functionName}列表
*/
@PreAuthorize("@ss.hasPermi('${permissionPrefix}:list')")
@GetMapping("/list")
#if($table.crud)
public TableDataInfo list(${ClassName} ${className})
{
startPage();
List<${ClassName}> list = ${className}Service.select${ClassName}List(${className});
return getDataTable(list);
}
#elseif($table.tree)
public AjaxResult list(${ClassName} ${className})
{
List<${ClassName}> list = ${className}Service.select${ClassName}List(${className});
return AjaxResult.success(list);
}
#end
/**
* 导出${functionName}列表
*/
@PreAuthorize("@ss.hasPermi('${permissionPrefix}:export')")
@Log(title = "${functionName}", businessType = BusinessType.EXPORT)
@GetMapping("/export")
public AjaxResult export(${ClassName} ${className})
{
List<${ClassName}> list = ${className}Service.select${ClassName}List(${className});
ExcelUtil<${ClassName}> util = new ExcelUtil<${ClassName}>(${ClassName}.class);
return util.exportExcel(list, "${businessName}");
}
/**
* 获取${functionName}详细信息
*/
@PreAuthorize("@ss.hasPermi('${permissionPrefix}:query')")
@GetMapping(value = "/{${pkColumn.javaField}}")
public AjaxResult getInfo(@PathVariable("${pkColumn.javaField}") ${pkColumn.javaType} ${pkColumn.javaField})
{
return AjaxResult.success(${className}Service.select${ClassName}ById(${pkColumn.javaField}));
}
/**
* 新增${functionName}
*/
@PreAuthorize("@ss.hasPermi('${permissionPrefix}:add')")
@Log(title = "${functionName}", businessType = BusinessType.INSERT)
@PostMapping
public AjaxResult add(@RequestBody ${ClassName} ${className})
{
return toAjax(${className}Service.insert${ClassName}(${className}));
}
/**
* 修改${functionName}
*/
@PreAuthorize("@ss.hasPermi('${permissionPrefix}:edit')")
@Log(title = "${functionName}", businessType = BusinessType.UPDATE)
@PutMapping
public AjaxResult edit(@RequestBody ${ClassName} ${className})
{
return toAjax(${className}Service.update${ClassName}(${className}));
}
/**
* 删除${functionName}
*/
@PreAuthorize("@ss.hasPermi('${permissionPrefix}:remove')")
@Log(title = "${functionName}", businessType = BusinessType.DELETE)
@DeleteMapping("/{${pkColumn.javaField}s}")
public AjaxResult remove(@PathVariable ${pkColumn.javaType}[] ${pkColumn.javaField}s)
{
return toAjax(${className}Service.delete${ClassName}ByIds(${pkColumn.javaField}s));
}
}

View File

@ -1,22 +0,0 @@
-- 菜单 SQL
insert into sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, update_by, update_time, remark)
values('${functionName}', '${parentMenuId}', '1', '${businessName}', '${moduleName}/${businessName}/index', 1, 0, 'C', '0', '0', '${permissionPrefix}:list', '#', 'admin', sysdate(), '', null, '${functionName}菜单');
-- 按钮父菜单ID
SELECT @parentId := LAST_INSERT_ID();
-- 按钮 SQL
insert into sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, update_by, update_time, remark)
values('${functionName}查询', @parentId, '1', '#', '', 1, 0, 'F', '0', '0', '${permissionPrefix}:query', '#', 'admin', sysdate(), '', null, '');
insert into sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, update_by, update_time, remark)
values('${functionName}新增', @parentId, '2', '#', '', 1, 0, 'F', '0', '0', '${permissionPrefix}:add', '#', 'admin', sysdate(), '', null, '');
insert into sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, update_by, update_time, remark)
values('${functionName}修改', @parentId, '3', '#', '', 1, 0, 'F', '0', '0', '${permissionPrefix}:edit', '#', 'admin', sysdate(), '', null, '');
insert into sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, update_by, update_time, remark)
values('${functionName}删除', @parentId, '4', '#', '', 1, 0, 'F', '0', '0', '${permissionPrefix}:remove', '#', 'admin', sysdate(), '', null, '');
insert into sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, update_by, update_time, remark)
values('${functionName}导出', @parentId, '5', '#', '', 1, 0, 'F', '0', '0', '${permissionPrefix}:export', '#', 'admin', sysdate(), '', null, '');

View File

@ -33,7 +33,7 @@ public class SysUserSessionDO extends BaseDO {
/** /**
* 用户编号 * 用户编号
* *
* 外键 {@link SysUserDO#getId()} * 关联 {@link SysUserDO#getId()}
*/ */
private Long userId; private Long userId;
/** /**

View File

@ -29,7 +29,7 @@ public class SysDeptDO extends BaseDO {
/** /**
* 父部门ID * 父部门ID
* *
* 外键 {@link #id} * 关联 {@link #id}
*/ */
private Long parentId; private Long parentId;
/** /**

View File

@ -29,7 +29,7 @@ public class ToolCodegenColumnDO extends BaseDO {
/** /**
* 表编号 * 表编号
* *
* 外键 {@link ToolCodegenTableDO#getId()} * 关联 {@link ToolCodegenTableDO#getId()}
*/ */
private Long tableId; private Long tableId;

View File

@ -1,6 +1,7 @@
package cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen; package cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen;
import cn.iocoder.dashboard.framework.mybatis.core.dataobject.BaseDO; import cn.iocoder.dashboard.framework.mybatis.core.dataobject.BaseDO;
import cn.iocoder.dashboard.modules.system.dal.dataobject.permission.SysMenuDO;
import cn.iocoder.dashboard.modules.tool.enums.codegen.ToolCodegenTemplateTypeEnum; import cn.iocoder.dashboard.modules.tool.enums.codegen.ToolCodegenTemplateTypeEnum;
import com.baomidou.mybatisplus.annotation.TableName; import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Builder; import lombok.Builder;
@ -86,4 +87,11 @@ public class ToolCodegenTableDO extends BaseDO {
// ========== 菜单相关字段 ========== // ========== 菜单相关字段 ==========
/**
* 父菜单编号
*
* 关联 {@link SysMenuDO#getId()}
*/
private Long parentMenuId;
} }

View File

@ -12,6 +12,8 @@ import cn.iocoder.dashboard.common.pojo.PageResult;
import cn.iocoder.dashboard.framework.codegen.config.CodegenProperties; import cn.iocoder.dashboard.framework.codegen.config.CodegenProperties;
import cn.iocoder.dashboard.framework.excel.core.annotations.DictFormat; import cn.iocoder.dashboard.framework.excel.core.annotations.DictFormat;
import cn.iocoder.dashboard.framework.excel.core.util.ExcelUtils; import cn.iocoder.dashboard.framework.excel.core.util.ExcelUtils;
import cn.iocoder.dashboard.framework.logger.operatelog.core.annotations.OperateLog;
import cn.iocoder.dashboard.framework.logger.operatelog.core.enums.OperateTypeEnum;
import cn.iocoder.dashboard.framework.mybatis.core.dataobject.BaseDO; import cn.iocoder.dashboard.framework.mybatis.core.dataobject.BaseDO;
import cn.iocoder.dashboard.framework.mybatis.core.mapper.BaseMapperX; import cn.iocoder.dashboard.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.dashboard.framework.mybatis.core.query.QueryWrapperX; import cn.iocoder.dashboard.framework.mybatis.core.query.QueryWrapperX;
@ -84,6 +86,7 @@ public class ToolCodegenEngine {
.put(vueTemplatePath("api/api.js"), .put(vueTemplatePath("api/api.js"),
vueFilePath("api/${table.moduleName}/${classNameVar}.js")) vueFilePath("api/${table.moduleName}/${classNameVar}.js"))
// SQL // SQL
.put("codegen/sql/sql.vm", "sql/sql.sql")
.build(); .build();
@Resource @Resource
@ -124,10 +127,12 @@ public class ToolCodegenEngine {
globalBindingMap.put("BaseDOClassName", BaseDO.class.getName()); globalBindingMap.put("BaseDOClassName", BaseDO.class.getName());
globalBindingMap.put("QueryWrapperClassName", QueryWrapperX.class.getName()); globalBindingMap.put("QueryWrapperClassName", QueryWrapperX.class.getName());
globalBindingMap.put("BaseMapperClassName", BaseMapperX.class.getName()); globalBindingMap.put("BaseMapperClassName", BaseMapperX.class.getName());
// Util 了诶 // Util 工具类
globalBindingMap.put("ServiceExceptionUtilClassName", ServiceExceptionUtil.class.getName()); globalBindingMap.put("ServiceExceptionUtilClassName", ServiceExceptionUtil.class.getName());
globalBindingMap.put("DateUtilsClassName", DateUtils.class.getName()); globalBindingMap.put("DateUtilsClassName", DateUtils.class.getName());
globalBindingMap.put("ExcelUtilsClassName", ExcelUtils.class.getName()); globalBindingMap.put("ExcelUtilsClassName", ExcelUtils.class.getName());
globalBindingMap.put("OperateLogClassName", OperateLog.class.getName());
globalBindingMap.put("OperateTypeEnumClassName", OperateTypeEnum.class.getName());
} }
public Map<String, String> execute(ToolCodegenTableDO table, List<ToolCodegenColumnDO> columns) { public Map<String, String> execute(ToolCodegenTableDO table, List<ToolCodegenColumnDO> columns) {
@ -146,7 +151,10 @@ public class ToolCodegenEngine {
bindingMap.put("simpleClassName", simpleClassName); bindingMap.put("simpleClassName", simpleClassName);
bindingMap.put("simpleClassName_underlineCase", toUnderlineCase(simpleClassName)); // DictType 转换成 dict_type bindingMap.put("simpleClassName_underlineCase", toUnderlineCase(simpleClassName)); // DictType 转换成 dict_type
bindingMap.put("classNameVar", lowerFirst(simpleClassName)); // DictType 转换成 dictType用于变量 bindingMap.put("classNameVar", lowerFirst(simpleClassName)); // DictType 转换成 dictType用于变量
bindingMap.put("simpleClassName_strikeCase", toSymbolCase(simpleClassName, '-')); // DictType 转换成 dict-type String simpleClassNameStrikeCase = toSymbolCase(simpleClassName, '-'); // DictType 转换成 dict-type
bindingMap.put("simpleClassName_strikeCase", simpleClassNameStrikeCase);
// permission 前缀
bindingMap.put("permissionPrefix", table.getModuleName() + ":" + simpleClassNameStrikeCase);
// 执行生成 // 执行生成
final Map<String, String> result = Maps.newLinkedHashMapWithExpectedSize(TEMPLATES.size()); // 有序 final Map<String, String> result = Maps.newLinkedHashMapWithExpectedSize(TEMPLATES.size()); // 有序

View File

@ -3,6 +3,7 @@ package ${basePackage}.${table.moduleName}.controller.${table.businessName};
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource; import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*; import io.swagger.annotations.*;
@ -18,6 +19,9 @@ import static ${CommonResultClassName}.success;
import ${ExcelUtilsClassName}; import ${ExcelUtilsClassName};
import ${OperateLogClassName};
import static ${OperateTypeEnumClassName}.*;
import ${basePackage}.${table.moduleName}.controller.${table.businessName}.vo.*; import ${basePackage}.${table.moduleName}.controller.${table.businessName}.vo.*;
import ${basePackage}.${table.moduleName}.dal.dataobject.${table.businessName}.${table.className}DO; import ${basePackage}.${table.moduleName}.dal.dataobject.${table.businessName}.${table.className}DO;
import ${basePackage}.${table.moduleName}.convert.${table.businessName}.${table.className}Convert; import ${basePackage}.${table.moduleName}.convert.${table.businessName}.${table.className}Convert;
@ -33,22 +37,25 @@ public class ${table.className}Controller {
@Resource @Resource
private ${table.className}Service ${classNameVar}Service; private ${table.className}Service ${classNameVar}Service;
@ApiOperation("创建${table.classComment}")
@PostMapping("/create") @PostMapping("/create")
@ApiOperation("创建${table.classComment}")
@PreAuthorize("@ss.hasPermi('${permissionPrefix}:create')")
public CommonResult<${primaryColumn.javaType}> create${simpleClassName}(@Valid ${table.className}CreateReqVO createReqVO) { public CommonResult<${primaryColumn.javaType}> create${simpleClassName}(@Valid ${table.className}CreateReqVO createReqVO) {
return success(${classNameVar}Service.create${simpleClassName}(createReqVO)); return success(${classNameVar}Service.create${simpleClassName}(createReqVO));
} }
@ApiOperation("更新${table.classComment}")
@PutMapping("/update") @PutMapping("/update")
@ApiOperation("更新${table.classComment}")
@PreAuthorize("@ss.hasPermi('${permissionPrefix}:update')")
public CommonResult<Boolean> update${simpleClassName}(@Valid ${table.className}UpdateReqVO updateReqVO) { public CommonResult<Boolean> update${simpleClassName}(@Valid ${table.className}UpdateReqVO updateReqVO) {
${classNameVar}Service.update${simpleClassName}(updateReqVO); ${classNameVar}Service.update${simpleClassName}(updateReqVO);
return success(true); return success(true);
} }
@DeleteMapping("/delete")
@ApiOperation("删除${table.classComment}") @ApiOperation("删除${table.classComment}")
@DeleteMapping("/delete")
@ApiImplicitParam(name = "id", value = "编号", required = true) @ApiImplicitParam(name = "id", value = "编号", required = true)
@PreAuthorize("@ss.hasPermi('${permissionPrefix}:delete')")
public CommonResult<Boolean> delete${simpleClassName}(@RequestParam("id") ${primaryColumn.javaType} id) { public CommonResult<Boolean> delete${simpleClassName}(@RequestParam("id") ${primaryColumn.javaType} id) {
${classNameVar}Service.delete${simpleClassName}(id); ${classNameVar}Service.delete${simpleClassName}(id);
return success(true); return success(true);
@ -57,6 +64,7 @@ public class ${table.className}Controller {
@GetMapping("/get") @GetMapping("/get")
@ApiOperation("获得${table.classComment}") @ApiOperation("获得${table.classComment}")
@ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = ${primaryColumn.javaType}.class) @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = ${primaryColumn.javaType}.class)
@PreAuthorize("@ss.hasPermi('${permissionPrefix}:query')")
public CommonResult<${table.className}RespVO> get${simpleClassName}(@RequestParam("id") ${primaryColumn.javaType} id) { public CommonResult<${table.className}RespVO> get${simpleClassName}(@RequestParam("id") ${primaryColumn.javaType} id) {
${table.className}DO ${classNameVar} = ${classNameVar}Service.get${simpleClassName}(id); ${table.className}DO ${classNameVar} = ${classNameVar}Service.get${simpleClassName}(id);
return success(${table.className}Convert.INSTANCE.convert(${classNameVar})); return success(${table.className}Convert.INSTANCE.convert(${classNameVar}));
@ -65,13 +73,15 @@ public class ${table.className}Controller {
@GetMapping("/list") @GetMapping("/list")
@ApiOperation("获得${table.classComment}列表") @ApiOperation("获得${table.classComment}列表")
@ApiImplicitParam(name = "ids", value = "编号列表", required = true, dataTypeClass = List.class) @ApiImplicitParam(name = "ids", value = "编号列表", required = true, dataTypeClass = List.class)
@PreAuthorize("@ss.hasPermi('${permissionPrefix}:query')")
public CommonResult<List<${table.className}RespVO>> get${simpleClassName}List(@RequestParam("ids") Collection<${primaryColumn.javaType}> ids) { public CommonResult<List<${table.className}RespVO>> get${simpleClassName}List(@RequestParam("ids") Collection<${primaryColumn.javaType}> ids) {
List<${table.className}DO> list = ${classNameVar}Service.get${simpleClassName}List(ids); List<${table.className}DO> list = ${classNameVar}Service.get${simpleClassName}List(ids);
return success(${table.className}Convert.INSTANCE.convertList(list)); return success(${table.className}Convert.INSTANCE.convertList(list));
} }
@ApiOperation("获得${table.classComment}分页")
@GetMapping("/page") @GetMapping("/page")
@ApiOperation("获得${table.classComment}分页")
@PreAuthorize("@ss.hasPermi('${permissionPrefix}:query')")
public CommonResult<PageResult<${table.className}RespVO>> get${simpleClassName}Page(@Valid ${table.className}PageReqVO pageVO) { public CommonResult<PageResult<${table.className}RespVO>> get${simpleClassName}Page(@Valid ${table.className}PageReqVO pageVO) {
PageResult<${table.className}DO> pageResult = ${classNameVar}Service.get${simpleClassName}Page(pageVO); PageResult<${table.className}DO> pageResult = ${classNameVar}Service.get${simpleClassName}Page(pageVO);
return success(${table.className}Convert.INSTANCE.convertPage(pageResult)); return success(${table.className}Convert.INSTANCE.convertPage(pageResult));
@ -79,6 +89,8 @@ public class ${table.className}Controller {
@GetMapping("/export-excel") @GetMapping("/export-excel")
@ApiOperation("导出${table.classComment} Excel") @ApiOperation("导出${table.classComment} Excel")
@PreAuthorize("@ss.hasPermi('${permissionPrefix}:export')")
@OperateLog(type = EXPORT)
public void export${simpleClassName}Excel(@Valid ${table.className}ExportReqVO exportReqVO, public void export${simpleClassName}Excel(@Valid ${table.className}ExportReqVO exportReqVO,
HttpServletResponse response) throws IOException { HttpServletResponse response) throws IOException {
List<${table.className}DO> list = ${classNameVar}Service.get${simpleClassName}List(exportReqVO); List<${table.className}DO> list = ${classNameVar}Service.get${simpleClassName}List(exportReqVO);

View File

@ -0,0 +1,26 @@
-- 菜单 SQL
INSERT INTO `sys_menu`(
`name`, `permission`, `menu_type`, `sort`, `parent_id`,
`path`, `icon`, `component`, `status`
)
VALUES (
'${table.tableComment}管理', '${permissionPrefix}:query', 2, 0, ${table.parentMenuId},
'${simpleClassName_strikeCase}', '', '${table.moduleName}/${table.businessName}/index', 1
);
-- 按钮父菜单ID
SELECT @parentId := LAST_INSERT_ID();
-- 按钮 SQL
#set ($functionNames = ['创建', '更新', '删除', '导出'])
#set ($functionOps = ['create', 'update', 'delete', 'export'])
#foreach ($functionName in $functionNames)
INSERT INTO `sys_menu`(
`name`, `permission`, `menu_type`, `sort`, `parent_id`,
`path`, `icon`, `component`, `status`
)
VALUES (
'${table.tableComment}${functionName}', '${permissionPrefix}:${functionOps[$velocityCount]}', 3, 0, @parentId,
'', '', '', 1
);
#end

View File

@ -48,11 +48,11 @@
<el-row :gutter="10" class="mb8"> <el-row :gutter="10" class="mb8">
<el-col :span="1.5"> <el-col :span="1.5">
<el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
v-hasPermi="['${moduleName}:${businessName}:add']">新增</el-button> v-hasPermi="['${permissionPrefix}:create']">新增</el-button>
</el-col> </el-col>
<el-col :span="1.5"> <el-col :span="1.5">
<el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
v-hasPermi="['${moduleName}:${businessName}:export']">导出</el-button> v-hasPermi="['${permissionPrefix}:export']">导出</el-button>
</el-col> </el-col>
<right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar> <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
</el-row> </el-row>
@ -85,9 +85,9 @@
<el-table-column label="操作" align="center" class-name="small-padding fixed-width"> <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
<template slot-scope="scope"> <template slot-scope="scope">
<el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
v-hasPermi="['${moduleName}:${businessName}:edit']">修改</el-button> v-hasPermi="['${permissionPrefix}:update']">修改</el-button>
<el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
v-hasPermi="['${moduleName}:${businessName}:remove']">删除</el-button> v-hasPermi="['${permissionPrefix}:delete']">删除</el-button>
</template> </template>
</el-table-column> </el-table-column>
</el-table> </el-table>
@ -211,7 +211,7 @@ export default {
showSearch: true, showSearch: true,
// 总条数 // 总条数
total: 0, total: 0,
// ${table.tableComment}表格数据 // ${table.classComment}列表
list: [], list: [],
// 弹出层标题 // 弹出层标题
title: "", title: "",
@ -305,7 +305,7 @@ export default {
handleAdd() { handleAdd() {
this.reset(); this.reset();
this.open = true; this.open = true;
this.title = "添加${table.tableComment}"; this.title = "添加${table.classComment}";
}, },
/** 修改按钮操作 */ /** 修改按钮操作 */
handleUpdate(row) { handleUpdate(row) {
@ -319,7 +319,7 @@ export default {
#end #end
#end #end
this.open = true; this.open = true;
this.title = "修改${table.tableComment}"; this.title = "修改${table.classComment}";
}); });
}, },
/** 提交按钮 */ /** 提交按钮 */
@ -353,7 +353,7 @@ export default {
/** 删除按钮操作 */ /** 删除按钮操作 */
handleDelete(row) { handleDelete(row) {
const ${primaryColumn.javaField} = row.${primaryColumn.javaField}; const ${primaryColumn.javaField} = row.${primaryColumn.javaField};
this.$confirm('是否确认删除${table.tableComment}编号为"' + ${primaryColumn.javaField} + '"的数据项?', "警告", { this.$confirm('是否确认删除${table.classComment}编号为"' + ${primaryColumn.javaField} + '"的数据项?', "警告", {
confirmButtonText: "确定", confirmButtonText: "确定",
cancelButtonText: "取消", cancelButtonText: "取消",
type: "warning" type: "warning"
@ -366,7 +366,7 @@ export default {
}, },
/** 导出按钮操作 */ /** 导出按钮操作 */
handleExport() { handleExport() {
this.$confirm('是否确认导出所有${table.tableComment}数据项?', "警告", { this.$confirm('是否确认导出所有${table.classComment}数据项?', "警告", {
confirmButtonText: "确定", confirmButtonText: "确定",
cancelButtonText: "取消", cancelButtonText: "取消",
type: "warning" type: "warning"