代码生成 - 结构同步

This commit is contained in:
TinyAnts 2022-06-14 18:58:50 +08:00
parent 61790c7444
commit 731cf9044e
5 changed files with 202 additions and 126 deletions

View File

@ -106,8 +106,10 @@ public class GenController {
* @author fzr
* @return Object
*/
@PostMapping("/syncTable")
public Object syncTable(@Validated @IDMust() @RequestParam("id") Integer id) {
return null;
iGenerateService.syncTable(id);
return AjaxResult.success();
}
/**

View File

@ -73,4 +73,16 @@ public interface GenTableMapper extends IBaseMapper<GenTable> {
"</script>"})
List<GenTableColumn> selectDbTableColumnsByName(String tableName);
@Select({"<script>",
"SELECT " +
"t.id, t.author_name, t.table_name, t.table_comment, t.sub_table_name, t.sub_table_fk, t.entity_name, " +
"t.module_name, t.package_name, t.business_name, t.function_name, t.gen_tpl, t.gen_type, t.gen_path, t.remarks, " +
"c.id as column_id, c.column_name, c.column_comment, c.column_type, c.java_type, c.java_field, c.is_pk, " +
"c.is_increment, c.is_required, c.is_insert, c.is_edit, c.is_list, c.is_query, c.html_type, c.dict_type, c.sort",
"FROM ls_gen_table t",
"LEFT JOIN ls_gen_table_column c ON t.id = c.table_id",
"WHERE t.id = #{id} ORDER BY c.sort",
"</script>"})
Map<String, String> selectGenTableByName(Integer id);
}

View File

@ -9,8 +9,10 @@ import com.hxkj.common.constant.GenConstants;
import com.hxkj.common.core.PageResult;
import com.hxkj.common.exception.OperateException;
import com.hxkj.common.utils.ArrayUtil;
import com.hxkj.common.utils.StringUtil;
import com.hxkj.common.utils.TimeUtil;
import com.hxkj.common.utils.ToolsUtil;
import com.hxkj.generator.config.GenConfig;
import com.hxkj.generator.entity.GenTable;
import com.hxkj.generator.entity.GenTableColumn;
@ -34,6 +36,8 @@ import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.io.StringWriter;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* 代码生成器服务实现类
@ -171,109 +175,18 @@ public class GenerateServiceImpl implements IGenerateService {
public void importTable(String[] tableNames) {
try {
List<Map<String, String>> tables = genTableMapper.selectDbTableListByNames(tableNames);
for (Map<String, String> map : tables) {
// 取基本数据
String tableName = map.get("table_name");
String tableDesc = map.get("table_comment");
// 生成表信息
GenTable table = new GenTable();
table.setTableName(tableName);
table.setTableComment(tableDesc);
table.setAuthorName(GenConfig.authorName);
table.setEntityName(GenUtil.toClassName(tableName));
table.setModuleName(GenUtil.toModuleName(GenConfig.packageName));
table.setPackageName(GenConfig.packageName);
table.setBusinessName(GenUtil.toBusinessName(tableName));
table.setFunctionName(GenUtil.replaceText(tableDesc));
table.setCreateTime(System.currentTimeMillis() / 1000);
table.setUpdateTime(System.currentTimeMillis() / 1000);
GenUtil.initTable(table, map);
int row = genTableMapper.insert(table);
// 生成列信息
if (row > 0) {
String tableName = map.get("table_name");
List<GenTableColumn> genTableColumns = genTableMapper.selectDbTableColumnsByName(tableName);
for (GenTableColumn column : genTableColumns) {
String columnName = column.getColumnName();
String columnType = GenUtil.getDbType(column.getColumnType());
column.setTableId(table.getId());
column.setJavaField(StringUtil.toCamelCase(columnName));
column.setJavaType(GenConstants.TYPE_STRING);
column.setQueryType(GenConstants.QUERY_EQ);
column.setIsInsert(GenConstants.REQUIRE);
column.setUpdateTime(table.getUpdateTime());
column.setCreateTime(table.getCreateTime());
// 文本域组
if (GenUtil.isArraysContains(GenConstants.COLUMN_TYPE_STR, columnType) ||
GenUtil.isArraysContains(GenConstants.COLUMN_TYPE_TEXT, columnType)) {
Integer columnLength = GenUtil.getColumnLength(column.getColumnType());
String htmlType = columnLength >= 500 || GenUtil.isArraysContains(GenConstants.COLUMN_TYPE_TEXT, columnType)
? GenConstants.HTML_TEXTAREA
: GenConstants.HTML_INPUT;
column.setHtmlType(htmlType);
}
// 日期组件
else if (GenUtil.isArraysContains(GenConstants.COLUMN_TYPE_TIME, columnType)) {
column.setJavaType(GenConstants.TYPE_DATE);
column.setHtmlType(GenConstants.HTML_DATETIME);
}
// 数字组件
else if (GenUtil.isArraysContains(GenConstants.COLUMN_TYPE_NUMBER, columnType)) {
column.setHtmlType(GenConstants.HTML_INPUT);
String[] str = StringUtil.split(StringUtil.substringBetween(column.getColumnType(), "(", ")"), ",");
if (str != null && str.length == 2 && Integer.parseInt(str[1]) > 0) {
column.setJavaType(GenConstants.TYPE_BIG_DECIMAL); // 浮点形
} else if (str != null && str.length == 1 && Integer.parseInt(str[0]) <= 10) {
column.setJavaType(GenConstants.TYPE_INTEGER); // 整数形
} else {
column.setJavaType(GenConstants.TYPE_LONG); // 长整形
}
}
// 编辑字段
if (!GenUtil.isArraysContains(GenConstants.COLUMN_NAME_NOT_EDIT, columnName) && column.getIsPk() == 0) {
column.setIsEdit(GenConstants.REQUIRE);
}
// 列表字段
if (!GenUtil.isArraysContains(GenConstants.COLUMN_NAME_NOT_LIST, columnName) && column.getIsPk() == 0) {
column.setIsList(GenConstants.REQUIRE);
}
// 查询字段
if (!GenUtil.isArraysContains(GenConstants.COLUMN_NAME_NOT_QUERY, columnName) && column.getIsPk() == 0) {
column.setIsQuery(GenConstants.REQUIRE);
}
// 查询字段类型
if (StringUtil.endsWithIgnoreCase(columnName, "name")) {
column.setQueryType(GenConstants.QUERY_LIKE);
}
// 根据字段设置
if (StringUtil.endsWithIgnoreCase(columnName, "status")) {
// 状态字段设置单选框
column.setHtmlType(GenConstants.HTML_RADIO);
} else if (StringUtil.endsWithIgnoreCase(columnName, "type") ||
StringUtil.endsWithIgnoreCase(columnName, "sex")) {
// 类型&性别字段设置下拉框
column.setHtmlType(GenConstants.HTML_SELECT);
} else if (StringUtil.endsWithIgnoreCase(columnName, "image")) {
// 图片字段设置图片上传控件
column.setHtmlType(GenConstants.HTML_IMAGE_UPLOAD);
} else if (StringUtil.endsWithIgnoreCase(columnName, "file")) {
// 文件字段设置文件上传控件
column.setHtmlType(GenConstants.HTML_FILE_UPLOAD);
} else if (StringUtil.endsWithIgnoreCase(columnName, "content")) {
// 内容字段的设置富文本控件
column.setHtmlType(GenConstants.HTML_EDITOR);
}
GenUtil.initColumn(column, table);
genTableColumnMapper.insert(column);
}
}
@ -348,10 +261,63 @@ public class GenerateServiceImpl implements IGenerateService {
* 同步数据表
*
* @author fzr
* @param id 主键
*/
@Override
@Transactional
public void syncTable(Integer id) {
// 原表数据
GenTable genTable = genTableMapper.selectById(id);
List<GenTableColumn> genTableColumns = genTableColumnMapper.selectList(
new QueryWrapper<GenTableColumn>()
.eq("table_id", id)
.orderByAsc("sort"));
// 原表转Map
Map<String, GenTableColumn> tableColumnMap = genTableColumns
.stream().collect(Collectors.toMap(GenTableColumn::getColumnName, Function.identity()));
// 新表数据
List<GenTableColumn> columns = genTableMapper.selectDbTableColumnsByName(genTable.getTableName());
if (StringUtil.isNull(columns)) {
throw new OperateException("同步结构失败,原表结构不存在!");
}
// 处理更新字段
columns.forEach(column -> {
GenUtil.initColumn(column, genTable);
if (tableColumnMap.containsKey(column.getColumnName())) {
GenTableColumn prevColumn = tableColumnMap.get(column.getColumnName());
column.setId(prevColumn.getId());
if (column.getIsList() != null && column.getIsList() == 1) {
column.setDictType(prevColumn.getDictType());
column.setQueryType(prevColumn.getQueryType());
}
if (prevColumn.getIsRequired() == 1
&& column.getIsPk() == 0
&& (column.getIsInsert() == 1 || column.getIsEdit() == 1)) {
column.setHtmlType(prevColumn.getHtmlType());
column.setIsRequired(prevColumn.getIsRequired());
}
genTableColumnMapper.updateById(column);
} else {
genTableColumnMapper.insert(column);
}
});
// 删除弃用字段
List<String> dbTableColumnNames = columns.stream().map(GenTableColumn::getColumnName).collect(Collectors.toList());
List<GenTableColumn> delColumns = genTableColumns.stream()
.filter(column -> !dbTableColumnNames.contains(column.getColumnName()))
.collect(Collectors.toList());
if (StringUtil.isNotEmpty(delColumns)) {
for (GenTableColumn item : delColumns) {
genTableColumnMapper.deleteById(item);
}
}
}
/**
@ -362,7 +328,6 @@ public class GenerateServiceImpl implements IGenerateService {
*/
@Override
public Map<String, String> previewCode(Integer id) {
GenTable table = genTableMapper.selectById(id);
// 初始模板

View File

@ -1,12 +1,126 @@
package com.hxkj.generator.util;
import com.hxkj.common.constant.GenConstants;
import com.hxkj.common.utils.StringUtil;
import com.hxkj.generator.config.GenConfig;
import com.hxkj.generator.entity.GenTable;
import com.hxkj.generator.entity.GenTableColumn;
import org.apache.commons.lang3.RegExUtils;
import java.util.Arrays;
import java.util.Map;
public class GenUtil {
/**
* 初始化表
*
* @author fzr
* @param table
* @param map 参数
*/
public static void initTable(GenTable table, Map<String, String> map) {
String tableName = map.get("table_name");
String tableDesc = map.get("table_comment");
table.setTableName(tableName);
table.setTableComment(tableDesc);
table.setAuthorName(GenConfig.authorName);
table.setEntityName(GenUtil.toClassName(tableName));
table.setModuleName(GenUtil.toModuleName(GenConfig.packageName));
table.setPackageName(GenConfig.packageName);
table.setBusinessName(GenUtil.toBusinessName(tableName));
table.setFunctionName(GenUtil.replaceText(tableDesc));
table.setCreateTime(System.currentTimeMillis() / 1000);
table.setUpdateTime(System.currentTimeMillis() / 1000);
}
/**
* 初始化字段列
*
* @author fzr
* @param column
* @param table
*/
public static void initColumn(GenTableColumn column, GenTable table) {
String columnName = column.getColumnName();
String columnType = GenUtil.getDbType(column.getColumnType());
column.setTableId(table.getId());
column.setJavaField(StringUtil.toCamelCase(columnName));
column.setJavaType(GenConstants.TYPE_STRING);
column.setQueryType(GenConstants.QUERY_EQ);
column.setIsInsert(GenConstants.REQUIRE);
column.setUpdateTime(table.getUpdateTime());
column.setCreateTime(table.getCreateTime());
// 文本域组
if (GenUtil.isArraysContains(GenConstants.COLUMN_TYPE_STR, columnType) ||
GenUtil.isArraysContains(GenConstants.COLUMN_TYPE_TEXT, columnType)) {
Integer columnLength = GenUtil.getColumnLength(column.getColumnType());
String htmlType = columnLength >= 500 || GenUtil.isArraysContains(GenConstants.COLUMN_TYPE_TEXT, columnType)
? GenConstants.HTML_TEXTAREA
: GenConstants.HTML_INPUT;
column.setHtmlType(htmlType);
}
// 日期组件
else if (GenUtil.isArraysContains(GenConstants.COLUMN_TYPE_TIME, columnType)) {
column.setJavaType(GenConstants.TYPE_DATE);
column.setHtmlType(GenConstants.HTML_DATETIME);
}
// 数字组件
else if (GenUtil.isArraysContains(GenConstants.COLUMN_TYPE_NUMBER, columnType)) {
column.setHtmlType(GenConstants.HTML_INPUT);
String[] str = StringUtil.split(StringUtil.substringBetween(column.getColumnType(), "(", ")"), ",");
if (str != null && str.length == 2 && Integer.parseInt(str[1]) > 0) {
column.setJavaType(GenConstants.TYPE_BIG_DECIMAL); // 浮点形
} else if (str != null && str.length == 1 && Integer.parseInt(str[0]) <= 10) {
column.setJavaType(GenConstants.TYPE_INTEGER); // 整数形
} else {
column.setJavaType(GenConstants.TYPE_LONG); // 长整形
}
}
// 编辑字段
if (!GenUtil.isArraysContains(GenConstants.COLUMN_NAME_NOT_EDIT, columnName) && column.getIsPk() == 0) {
column.setIsEdit(GenConstants.REQUIRE);
}
// 列表字段
if (!GenUtil.isArraysContains(GenConstants.COLUMN_NAME_NOT_LIST, columnName) && column.getIsPk() == 0) {
column.setIsList(GenConstants.REQUIRE);
}
// 查询字段
if (!GenUtil.isArraysContains(GenConstants.COLUMN_NAME_NOT_QUERY, columnName) && column.getIsPk() == 0) {
column.setIsQuery(GenConstants.REQUIRE);
}
// 查询字段类型
if (StringUtil.endsWithIgnoreCase(columnName, "name")) {
column.setQueryType(GenConstants.QUERY_LIKE);
}
// 根据字段设置
if (StringUtil.endsWithIgnoreCase(columnName, "status")) {
// 状态字段设置单选框
column.setHtmlType(GenConstants.HTML_RADIO);
} else if (StringUtil.endsWithIgnoreCase(columnName, "type") ||
StringUtil.endsWithIgnoreCase(columnName, "sex")) {
// 类型&性别字段设置下拉框
column.setHtmlType(GenConstants.HTML_SELECT);
} else if (StringUtil.endsWithIgnoreCase(columnName, "image")) {
// 图片字段设置图片上传控件
column.setHtmlType(GenConstants.HTML_IMAGE_UPLOAD);
} else if (StringUtil.endsWithIgnoreCase(columnName, "file")) {
// 文件字段设置文件上传控件
column.setHtmlType(GenConstants.HTML_FILE_UPLOAD);
} else if (StringUtil.endsWithIgnoreCase(columnName, "content")) {
// 内容字段的设置富文本控件
column.setHtmlType(GenConstants.HTML_EDITOR);
}
}
/**
* 转模块名
*
@ -14,8 +128,7 @@ public class GenUtil {
* @param packageName 包名
* @return 模块名
*/
public static String toModuleName(String packageName)
{
public static String toModuleName(String packageName) {
int lastIndex = packageName.lastIndexOf(".");
int nameLength = packageName.length();
return StringUtil.substring(packageName, lastIndex + 1, nameLength);
@ -28,8 +141,7 @@ public class GenUtil {
* @param tableName 表名
* @return 业务名
*/
public static String toBusinessName(String tableName)
{
public static String toBusinessName(String tableName) {
int lastIndex = tableName.lastIndexOf("_");
int nameLength = tableName.length();
return StringUtil.substring(tableName, lastIndex + 1, nameLength);
@ -42,8 +154,7 @@ public class GenUtil {
* @param tableName 表名称
* @return 类名
*/
public static String toClassName(String tableName)
{
public static String toClassName(String tableName) {
String tablePrefix = "ls_";
if (StringUtil.isNotEmpty(tablePrefix)) {
String[] searchList = StringUtil.split(tablePrefix, ",");
@ -60,8 +171,7 @@ public class GenUtil {
* @param searchList 替换列表
* @return String
*/
public static String replaceFirst(String replaceVal, String[] searchList)
{
public static String replaceFirst(String replaceVal, String[] searchList) {
String text = replaceVal;
for (String searchString : searchList) {
if (replaceVal.startsWith(searchString)) {
@ -79,8 +189,7 @@ public class GenUtil {
* @param text 需要被替换的名字
* @return 替换后的名字
*/
public static String replaceText(String text)
{
public static String replaceText(String text) {
return RegExUtils.replaceAll(text, "(?:表)", "");
}
@ -90,10 +199,9 @@ public class GenUtil {
* @author fzr
* @param arr 数组
* @param targetValue
* @return 是否包含
* @return Boolean
*/
public static boolean isArraysContains(String[] arr, String targetValue)
{
public static Boolean isArraysContains(String[] arr, String targetValue) {
return Arrays.asList(arr).contains(targetValue);
}
@ -102,10 +210,9 @@ public class GenUtil {
*
* @author fzr
* @param columnType 列类型
* @return 截取后的列类型
* @return String
*/
public static String getDbType(String columnType)
{
public static String getDbType(String columnType) {
if (StringUtil.indexOf(columnType, "(") > 0) {
return StringUtil.substringBefore(columnType, "(");
}
@ -121,8 +228,7 @@ public class GenUtil {
* @param columnType 列类型
* @return 截取后的列类型
*/
public static Integer getColumnLength(String columnType)
{
public static Integer getColumnLength(String columnType) {
if (StringUtil.indexOf(columnType, "(") > 0) {
String length = StringUtil.substringBetween(columnType, "(", ")");
return Integer.valueOf(length);
@ -132,8 +238,4 @@ public class GenUtil {
}
}
public static void setPkColumn() {
}
}

View File

@ -8,8 +8,6 @@ import org.apache.velocity.app.Velocity;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;
import java.sql.Time;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;
@ -19,8 +17,7 @@ public class VelocityUtil {
/**
* 初始化vm方法
*/
public static void initVelocity()
{
public static void initVelocity() {
try {
Properties p = new Properties();
p.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
@ -37,8 +34,7 @@ public class VelocityUtil {
*
* @return 模板列表
*/
public static VelocityContext prepareContext(GenTable genTable)
{
public static VelocityContext prepareContext(GenTable genTable) {
String moduleName = genTable.getModuleName();
String businessName = genTable.getBusinessName();
String packageName = genTable.getPackageName();
@ -64,8 +60,7 @@ public class VelocityUtil {
*
* @return 模板列表
*/
public static List<String> getTemplateList(String genTpl)
{
public static List<String> getTemplateList(String genTpl) {
List<String> templates = new LinkedList<>();
templates.add("java/controller.java.vm");