代码生成模板支持主子表

This commit is contained in:
RuoYi
2020-06-17 09:43:15 +08:00
parent 338138a0bc
commit 052ab8866d
25 changed files with 802 additions and 124 deletions

View File

@ -1,6 +1,7 @@
package com.ruoyi.generator.controller;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
@ -15,12 +16,15 @@ import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSON;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.CxSelect;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.security.PermissionUtils;
import com.ruoyi.generator.domain.GenTable;
import com.ruoyi.generator.domain.GenTableColumn;
@ -126,7 +130,24 @@ public class GenController extends BaseController
public String edit(@PathVariable("tableId") Long tableId, ModelMap mmap)
{
GenTable table = genTableService.selectGenTableById(tableId);
List<GenTable> genTables = genTableService.selectGenTableAll();
List<CxSelect> cxSelect = new ArrayList<CxSelect>();
for (GenTable genTable : genTables)
{
if (!StringUtils.equals(table.getTableName(), genTable.getTableName()))
{
CxSelect cxTable = new CxSelect(genTable.getTableName(), genTable.getTableName() + '' + genTable.getTableComment());
List<CxSelect> cxColumns = new ArrayList<CxSelect>();
for (GenTableColumn tableColumn : genTable.getColumns())
{
cxColumns.add(new CxSelect(tableColumn.getColumnName(), tableColumn.getColumnName() + '' + tableColumn.getColumnComment()));
}
cxTable.setS(cxColumns);
cxSelect.add(cxTable);
}
}
mmap.put("table", table);
mmap.put("data", JSON.toJSON(cxSelect));
return prefix + "/edit";
}

View File

@ -28,11 +28,17 @@ public class GenTable extends BaseEntity
@NotBlank(message = "表描述不能为空")
private String tableComment;
/** 关联父表的表名 */
private String subTableName;
/** 本表关联父表的外键名 */
private String subTableFkName;
/** 实体类名称(首字母大写) */
@NotBlank(message = "实体类名称不能为空")
private String className;
/** 使用的模板crud单表操作 tree树表操作 */
/** 使用的模板crud单表操作 tree树表操作 sub主子表操作 */
private String tplCategory;
/** 生成包路径 */
@ -58,6 +64,9 @@ public class GenTable extends BaseEntity
/** 主键信息 */
private GenTableColumn pkColumn;
/** 子表信息 */
private GenTable subTable;
/** 表列信息 */
@Valid
private List<GenTableColumn> columns;
@ -104,6 +113,26 @@ public class GenTable extends BaseEntity
this.tableComment = tableComment;
}
public String getSubTableName()
{
return subTableName;
}
public void setSubTableName(String subTableName)
{
this.subTableName = subTableName;
}
public String getSubTableFkName()
{
return subTableFkName;
}
public void setSubTableFkName(String subTableFkName)
{
this.subTableFkName = subTableFkName;
}
public String getClassName()
{
return className;
@ -184,6 +213,16 @@ public class GenTable extends BaseEntity
this.pkColumn = pkColumn;
}
public GenTable getSubTable()
{
return subTable;
}
public void setSubTable(GenTable subTable)
{
this.subTable = subTable;
}
public List<GenTableColumn> getColumns()
{
return columns;
@ -234,6 +273,16 @@ public class GenTable extends BaseEntity
this.treeName = treeName;
}
public boolean isSub()
{
return isSub(this.tplCategory);
}
public static boolean isSub(String tplCategory)
{
return tplCategory != null && StringUtils.equals(GenConstants.TPL_SUB, tplCategory);
}
public boolean isTree()
{
return isTree(this.tplCategory);

View File

@ -138,6 +138,11 @@ public class GenTableColumn extends BaseEntity
return javaField;
}
public String getCapJavaField()
{
return StringUtils.capitalize(javaField);
}
public void setIsPk(String isPk)
{
this.isPk = isPk;
@ -326,9 +331,9 @@ public class GenTableColumn extends BaseEntity
public static boolean isSuperColumn(String javaField)
{
return StringUtils.equalsAnyIgnoreCase(javaField,
//BaseEntity
// BaseEntity
"createBy", "createTime", "updateBy", "updateTime", "remark",
//TreeEntity
// TreeEntity
"parentName", "parentId", "orderNum", "ancestors");
}
@ -339,8 +344,8 @@ public class GenTableColumn extends BaseEntity
public static boolean isUsableColumn(String javaField)
{
//isSuperColumn()中的名单用于避免生成多余Domain属性若某些属性在生成页面时需要用到不能忽略则放在此处白名单
return StringUtils.equalsAnyIgnoreCase(javaField, "parentId" , "orderNum");
// isSuperColumn()中的名单用于避免生成多余Domain属性若某些属性在生成页面时需要用到不能忽略则放在此处白名单
return StringUtils.equalsAnyIgnoreCase(javaField, "parentId", "orderNum");
}
public String readConverterExp()

View File

@ -34,6 +34,13 @@ public interface GenTableMapper
*/
public List<GenTable> selectDbTableListByNames(String[] tableNames);
/**
* 查询所有表信息
*
* @return 表信息集合
*/
public List<GenTable> selectGenTableAll();
/**
* 查询表ID业务信息
*

View File

@ -35,6 +35,13 @@ public interface IGenTableService
*/
public List<GenTable> selectDbTableListByNames(String[] tableNames);
/**
* 查询所有表信息
*
* @return 表信息集合
*/
public List<GenTable> selectGenTableAll();
/**
* 查询业务信息
*

View File

@ -99,6 +99,16 @@ public class GenTableServiceImpl implements IGenTableService
return genTableMapper.selectDbTableListByNames(tableNames);
}
/**
* 查询所有表信息
*
* @return 表信息集合
*/
public List<GenTable> selectGenTableAll()
{
return genTableMapper.selectGenTableAll();
}
/**
* 修改业务
*
@ -182,9 +192,10 @@ public class GenTableServiceImpl implements IGenTableService
Map<String, String> dataMap = new LinkedHashMap<>();
// 查询表信息
GenTable table = genTableMapper.selectGenTableById(tableId);
// 查询列信息
List<GenTableColumn> columns = table.getColumns();
setPkColumn(table, columns);
// 设置主子表信息
setSubTable(table);
// 设置主键列信息
setPkColumn(table);
VelocityInitializer.initVelocity();
VelocityContext context = VelocityUtils.prepareContext(table);
@ -244,9 +255,10 @@ public class GenTableServiceImpl implements IGenTableService
{
// 查询表信息
GenTable table = genTableMapper.selectGenTableByName(tableName);
// 查询列信息
List<GenTableColumn> columns = table.getColumns();
setPkColumn(table, columns);
// 设置主子表信息
setSubTable(table);
// 设置主键列信息
setPkColumn(table);
VelocityInitializer.initVelocity();
@ -301,17 +313,28 @@ public class GenTableServiceImpl implements IGenTableService
throw new BusinessException("树名称字段不能为空");
}
}
else if (GenConstants.TPL_SUB.equals(genTable.getTplCategory()))
{
if (StringUtils.isEmpty(genTable.getSubTableName()))
{
throw new BusinessException("关联子表的表名不能为空");
}
else if (StringUtils.isEmpty(genTable.getSubTableFkName()))
{
throw new BusinessException("子表关联的外键名不能为空");
}
}
}
/**
* 设置主键列信息
*
* @param genTable 业务表信息
* @param columns 业务字段列表
* @param table 业务表信息
*/
public void setPkColumn(GenTable table, List<GenTableColumn> columns)
public void setPkColumn(GenTable table)
{
for (GenTableColumn column : columns)
for (GenTableColumn column : table.getColumns())
{
if (column.isPk())
{
@ -321,7 +344,36 @@ public class GenTableServiceImpl implements IGenTableService
}
if (StringUtils.isNull(table.getPkColumn()))
{
table.setPkColumn(columns.get(0));
table.setPkColumn(table.getColumns().get(0));
}
if (GenConstants.TPL_SUB.equals(table.getTplCategory()))
{
for (GenTableColumn column : table.getSubTable().getColumns())
{
if (column.isPk())
{
table.getSubTable().setPkColumn(column);
break;
}
}
if (StringUtils.isNull(table.getSubTable().getPkColumn()))
{
table.getSubTable().setPkColumn(table.getSubTable().getColumns().get(0));
}
}
}
/**
* 设置主子表信息
*
* @param table 业务表信息
*/
public void setSubTable(GenTable table)
{
String subTableName = table.getSubTableName();
if (StringUtils.isNotEmpty(subTableName))
{
table.setSubTable(genTableMapper.selectGenTableByName(subTableName));
}
}

View File

@ -49,7 +49,7 @@ public class VelocityUtils
velocityContext.put("author", genTable.getFunctionAuthor());
velocityContext.put("datetime", DateUtils.getDate());
velocityContext.put("pkColumn", genTable.getPkColumn());
velocityContext.put("importList", getImportList(genTable.getColumns()));
velocityContext.put("importList", getImportList(genTable));
velocityContext.put("permissionPrefix", getPermissionPrefix(moduleName, businessName));
velocityContext.put("columns", genTable.getColumns());
velocityContext.put("table", genTable);
@ -57,6 +57,10 @@ public class VelocityUtils
{
setTreeVelocityContext(velocityContext, genTable);
}
if (GenConstants.TPL_SUB.equals(tplCategory))
{
setSubVelocityContext(velocityContext, genTable);
}
return velocityContext;
}
@ -82,6 +86,24 @@ public class VelocityUtils
}
}
public static void setSubVelocityContext(VelocityContext context, GenTable genTable)
{
GenTable subTable = genTable.getSubTable();
String subTableName = genTable.getSubTableName();
String subTableFkName = genTable.getSubTableFkName();
String subClassName = genTable.getSubTable().getClassName();
String subTableFkClassName = StringUtils.convertToCamelCase(subTableFkName);
context.put("subTable", subTable);
context.put("subTableName", subTableName);
context.put("subTableFkName", subTableFkName);
context.put("subTableFkClassName", subTableFkClassName);
context.put("subTableFkclassName", StringUtils.uncapitalize(subTableFkClassName));
context.put("subClassName", subClassName);
context.put("subclassName", StringUtils.uncapitalize(subClassName));
context.put("subImportList", getImportList(genTable.getSubTable()));
}
/**
* 获取模板信息
*
@ -105,6 +127,11 @@ public class VelocityUtils
templates.add("vm/html/tree.html.vm");
templates.add("vm/html/list-tree.html.vm");
}
else if (GenConstants.TPL_SUB.equals(tplCategory))
{
templates.add("vm/html/list.html.vm");
templates.add("vm/java/sub-domain.java.vm");
}
templates.add("vm/html/add.html.vm");
templates.add("vm/html/edit.html.vm");
templates.add("vm/sql/sql.vm");
@ -135,6 +162,10 @@ public class VelocityUtils
{
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);
@ -213,12 +244,18 @@ public class VelocityUtils
/**
* 根据列类型获取导入包
*
* @param column 列集合
* @param genTable 业务表对象
* @return 返回需要导入的包列表
*/
public static HashSet<String> getImportList(List<GenTableColumn> columns)
public static HashSet<String> getImportList(GenTable genTable)
{
List<GenTableColumn> columns = genTable.getColumns();
GenTable subGenTable = genTable.getSubTable();
HashSet<String> importList = new HashSet<String>();
if (StringUtils.isNotNull(subGenTable))
{
importList.add("java.util.List");
}
for (GenTableColumn column : columns)
{
if (!column.isSuperColumn() && GenConstants.TYPE_DATE.equals(column.getJavaType()))