代码生成模板支持主子表
This commit is contained in:
@ -63,10 +63,12 @@ public class GenController extends BaseController
|
||||
public AjaxResult getInfo(@PathVariable Long talbleId)
|
||||
{
|
||||
GenTable table = genTableService.selectGenTableById(talbleId);
|
||||
List<GenTable> tables = genTableService.selectGenTableAll();
|
||||
List<GenTableColumn> list = genTableColumnService.selectGenTableColumnListByTableId(talbleId);
|
||||
Map<String, Object> map = new HashMap<String, Object>();
|
||||
map.put("info", table);
|
||||
map.put("rows", list);
|
||||
map.put("tables", tables);
|
||||
return AjaxResult.success(map);
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
|
||||
/** 生成包路径 */
|
||||
@ -64,6 +70,9 @@ public class GenTable extends BaseEntity
|
||||
/** 主键信息 */
|
||||
private GenTableColumn pkColumn;
|
||||
|
||||
/** 子表信息 */
|
||||
private GenTable subTable;
|
||||
|
||||
/** 表列信息 */
|
||||
@Valid
|
||||
private List<GenTableColumn> columns;
|
||||
@ -116,6 +125,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;
|
||||
@ -216,6 +245,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;
|
||||
@ -286,6 +325,16 @@ public class GenTable extends BaseEntity
|
||||
this.parentMenuName = parentMenuName;
|
||||
}
|
||||
|
||||
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);
|
||||
|
@ -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;
|
||||
|
@ -34,6 +34,13 @@ public interface GenTableMapper
|
||||
*/
|
||||
public List<GenTable> selectDbTableListByNames(String[] tableNames);
|
||||
|
||||
/**
|
||||
* 查询所有表信息
|
||||
*
|
||||
* @return 表信息集合
|
||||
*/
|
||||
public List<GenTable> selectGenTableAll();
|
||||
|
||||
/**
|
||||
* 查询表ID业务信息
|
||||
*
|
||||
|
@ -102,6 +102,17 @@ public class GenTableServiceImpl implements IGenTableService
|
||||
return genTableMapper.selectDbTableListByNames(tableNames);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询所有表信息
|
||||
*
|
||||
* @return 表信息集合
|
||||
*/
|
||||
@Override
|
||||
public List<GenTable> selectGenTableAll()
|
||||
{
|
||||
return genTableMapper.selectGenTableAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改业务
|
||||
*
|
||||
@ -185,9 +196,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);
|
||||
@ -231,9 +243,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();
|
||||
|
||||
@ -276,6 +289,10 @@ public class GenTableServiceImpl implements IGenTableService
|
||||
List<String> tableColumnNames = tableColumns.stream().map(GenTableColumn::getColumnName).collect(Collectors.toList());
|
||||
|
||||
List<GenTableColumn> dbTableColumns = genTableColumnMapper.selectDbTableColumnsByName(tableName);
|
||||
if (StringUtils.isEmpty(dbTableColumns))
|
||||
{
|
||||
throw new CustomException("同步数据失败,原表结构不存在");
|
||||
}
|
||||
List<String> dbTableColumnNames = dbTableColumns.stream().map(GenTableColumn::getColumnName).collect(Collectors.toList());
|
||||
|
||||
dbTableColumns.forEach(column -> {
|
||||
@ -319,9 +336,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();
|
||||
|
||||
@ -375,6 +393,17 @@ public class GenTableServiceImpl implements IGenTableService
|
||||
{
|
||||
throw new CustomException("树名称字段不能为空");
|
||||
}
|
||||
else if (GenConstants.TPL_SUB.equals(genTable.getTplCategory()))
|
||||
{
|
||||
if (StringUtils.isEmpty(genTable.getSubTableName()))
|
||||
{
|
||||
throw new CustomException("关联子表的表名不能为空");
|
||||
}
|
||||
else if (StringUtils.isEmpty(genTable.getSubTableFkName()))
|
||||
{
|
||||
throw new CustomException("子表关联的外键名不能为空");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -382,11 +411,10 @@ public class GenTableServiceImpl implements IGenTableService
|
||||
* 设置主键列信息
|
||||
*
|
||||
* @param table 业务表信息
|
||||
* @param columns 业务字段列表
|
||||
*/
|
||||
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())
|
||||
{
|
||||
@ -396,7 +424,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));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -35,6 +35,13 @@ public interface IGenTableService
|
||||
*/
|
||||
public List<GenTable> selectDbTableListByNames(String[] tableNames);
|
||||
|
||||
/**
|
||||
* 查询所有表信息
|
||||
*
|
||||
* @return 表信息集合
|
||||
*/
|
||||
public List<GenTable> selectGenTableAll();
|
||||
|
||||
/**
|
||||
* 查询业务信息
|
||||
*
|
||||
|
@ -7,7 +7,7 @@ import com.ruoyi.common.constant.Constants;
|
||||
/**
|
||||
* VelocityEngine工厂
|
||||
*
|
||||
* @author RuoYi
|
||||
* @author ruoyi
|
||||
*/
|
||||
public class VelocityInitializer
|
||||
{
|
||||
|
@ -54,7 +54,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);
|
||||
@ -63,6 +63,10 @@ public class VelocityUtils
|
||||
{
|
||||
setTreeVelocityContext(velocityContext, genTable);
|
||||
}
|
||||
if (GenConstants.TPL_SUB.equals(tplCategory))
|
||||
{
|
||||
setSubVelocityContext(velocityContext, genTable);
|
||||
}
|
||||
return velocityContext;
|
||||
}
|
||||
|
||||
@ -96,6 +100,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()));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取模板信息
|
||||
*
|
||||
@ -120,6 +142,11 @@ public class VelocityUtils
|
||||
{
|
||||
templates.add("vm/vue/index-tree.vue.vm");
|
||||
}
|
||||
else if (GenConstants.TPL_SUB.equals(tplCategory))
|
||||
{
|
||||
templates.add("vm/vue/index.vue.vm");
|
||||
templates.add("vm/java/sub-domain.java.vm");
|
||||
}
|
||||
return templates;
|
||||
}
|
||||
|
||||
@ -147,6 +174,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);
|
||||
@ -201,13 +232,19 @@ public class VelocityUtils
|
||||
|
||||
/**
|
||||
* 根据列类型获取导入包
|
||||
*
|
||||
* @param columns 列集合
|
||||
*
|
||||
* @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()))
|
||||
|
Reference in New Issue
Block a user