mirror of
https://gitcode.com/ageerle/ruoyi-ai.git
synced 2026-04-15 04:43:40 +00:00
feat: fix代码生成类型问题
This commit is contained in:
@@ -129,17 +129,17 @@ public class GenTableServiceImpl implements IGenTableService {
|
|||||||
*/
|
*/
|
||||||
private VelocityContext prepareSchemaContext(SchemaVo schema, List<SchemaFieldVo> fields) {
|
private VelocityContext prepareSchemaContext(SchemaVo schema, List<SchemaFieldVo> fields) {
|
||||||
VelocityContext context = new VelocityContext();
|
VelocityContext context = new VelocityContext();
|
||||||
|
|
||||||
// 从配置文件读取基本配置
|
// 从配置文件读取基本配置
|
||||||
String packageName = GenConfig.getPackageName();
|
String packageName = GenConfig.getPackageName();
|
||||||
String author = GenConfig.getAuthor();
|
String author = GenConfig.getAuthor();
|
||||||
String tablePrefix = GenConfig.getTablePrefix();
|
String tablePrefix = GenConfig.getTablePrefix();
|
||||||
boolean autoRemovePre = GenConfig.getAutoRemovePre();
|
boolean autoRemovePre = GenConfig.getAutoRemovePre();
|
||||||
|
|
||||||
// 处理表名和类名
|
// 处理表名和类名
|
||||||
String tableName = schema.getTableName();
|
String tableName = schema.getTableName();
|
||||||
String baseClassName = schema.getTableName();
|
String baseClassName = schema.getTableName();
|
||||||
|
|
||||||
// 自动去除表前缀
|
// 自动去除表前缀
|
||||||
if (autoRemovePre && StrUtil.isNotBlank(tablePrefix)) {
|
if (autoRemovePre && StrUtil.isNotBlank(tablePrefix)) {
|
||||||
String[] prefixes = tablePrefix.split(",");
|
String[] prefixes = tablePrefix.split(",");
|
||||||
@@ -150,12 +150,12 @@ public class GenTableServiceImpl implements IGenTableService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
String className = toCamelCase(baseClassName, true); // 首字母大写的类名,如:SysRole
|
String className = toCamelCase(baseClassName, true); // 首字母大写的类名,如:SysRole
|
||||||
String classname = toCamelCase(baseClassName, false); // 首字母小写的类名,如:sysRole
|
String classname = toCamelCase(baseClassName, false); // 首字母小写的类名,如:sysRole
|
||||||
String businessName = toCamelCase(baseClassName, false);
|
String businessName = toCamelCase(baseClassName, false);
|
||||||
String moduleName = getModuleName(packageName);
|
String moduleName = getModuleName(packageName);
|
||||||
|
|
||||||
// 基本信息
|
// 基本信息
|
||||||
context.put("tableName", tableName);
|
context.put("tableName", tableName);
|
||||||
context.put("tableComment", schema.getComment());
|
context.put("tableComment", schema.getComment());
|
||||||
@@ -169,18 +169,18 @@ public class GenTableServiceImpl implements IGenTableService {
|
|||||||
context.put("packageName", packageName);
|
context.put("packageName", packageName);
|
||||||
context.put("moduleName", moduleName);
|
context.put("moduleName", moduleName);
|
||||||
context.put("businessName", businessName);
|
context.put("businessName", businessName);
|
||||||
|
|
||||||
// 权限相关
|
// 权限相关
|
||||||
context.put("permissionPrefix", moduleName + ":" + businessName);
|
context.put("permissionPrefix", moduleName + ":" + businessName);
|
||||||
context.put("parentMenuId", "2000"); // 默认父菜单ID,可配置
|
context.put("parentMenuId", "2000"); // 默认父菜单ID,可配置
|
||||||
|
|
||||||
// 生成菜单ID
|
// 生成菜单ID
|
||||||
List<Long> menuIds = new ArrayList<>();
|
List<Long> menuIds = new ArrayList<>();
|
||||||
for (int i = 0; i < 6; i++) {
|
for (int i = 0; i < 6; i++) {
|
||||||
menuIds.add(IdUtil.getSnowflakeNextId());
|
menuIds.add(IdUtil.getSnowflakeNextId());
|
||||||
}
|
}
|
||||||
context.put("menuIds", menuIds);
|
context.put("menuIds", menuIds);
|
||||||
|
|
||||||
// 创建table对象,包含menuIds等信息和方法
|
// 创建table对象,包含menuIds等信息和方法
|
||||||
Map<String, Object> table = new HashMap<>();
|
Map<String, Object> table = new HashMap<>();
|
||||||
table.put("menuIds", menuIds);
|
table.put("menuIds", menuIds);
|
||||||
@@ -189,29 +189,19 @@ public class GenTableServiceImpl implements IGenTableService {
|
|||||||
table.put("className", className);
|
table.put("className", className);
|
||||||
table.put("classname", classname);
|
table.put("classname", classname);
|
||||||
table.put("functionName", schema.getName());
|
table.put("functionName", schema.getName());
|
||||||
|
|
||||||
// 添加表类型属性(默认为crud类型)
|
// 添加表类型属性(默认为crud类型)
|
||||||
table.put("crud", true);
|
table.put("crud", true);
|
||||||
table.put("sub", false);
|
table.put("sub", false);
|
||||||
table.put("tree", false);
|
table.put("tree", false);
|
||||||
|
|
||||||
// 添加isSuperColumn方法
|
|
||||||
table.put("isSuperColumn", new Object() {
|
|
||||||
public boolean isSuperColumn(String javaField) {
|
|
||||||
// 定义超类字段(BaseEntity中的字段)
|
|
||||||
return "createBy".equals(javaField) || "createTime".equals(javaField)
|
|
||||||
|| "updateBy".equals(javaField) || "updateTime".equals(javaField)
|
|
||||||
|| "remark".equals(javaField) || "tenantId".equals(javaField);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
context.put("table", table);
|
context.put("table", table);
|
||||||
|
|
||||||
// 处理字段信息
|
// 处理字段信息
|
||||||
List<Map<String, Object>> columns = new ArrayList<>();
|
List<Map<String, Object>> columns = new ArrayList<>();
|
||||||
Map<String, Object> pkColumn = null;
|
Map<String, Object> pkColumn = null;
|
||||||
Set<String> importList = new HashSet<>();
|
Set<String> importList = new HashSet<>();
|
||||||
|
|
||||||
// 添加基础导入
|
// 添加基础导入
|
||||||
importList.add("java.io.Serializable");
|
importList.add("java.io.Serializable");
|
||||||
|
|
||||||
@@ -219,7 +209,7 @@ public class GenTableServiceImpl implements IGenTableService {
|
|||||||
Map<String, Object> column = new HashMap<>();
|
Map<String, Object> column = new HashMap<>();
|
||||||
String javaType = getJavaType(field.getType());
|
String javaType = getJavaType(field.getType());
|
||||||
String javaField = StrUtil.toCamelCase(field.getCode());
|
String javaField = StrUtil.toCamelCase(field.getCode());
|
||||||
|
|
||||||
column.put("columnName", field.getCode());
|
column.put("columnName", field.getCode());
|
||||||
column.put("columnComment", field.getName());
|
column.put("columnComment", field.getName());
|
||||||
column.put("comment", field.getName()); // 添加comment别名
|
column.put("comment", field.getName()); // 添加comment别名
|
||||||
@@ -227,7 +217,7 @@ public class GenTableServiceImpl implements IGenTableService {
|
|||||||
column.put("javaType", javaType);
|
column.put("javaType", javaType);
|
||||||
column.put("javaField", javaField);
|
column.put("javaField", javaField);
|
||||||
column.put("capJavaField", toCamelCase(field.getCode(), true));
|
column.put("capJavaField", toCamelCase(field.getCode(), true));
|
||||||
|
|
||||||
// 布尔值属性(兼容两种格式)
|
// 布尔值属性(兼容两种格式)
|
||||||
boolean isPk = "1".equals(field.getIsPk());
|
boolean isPk = "1".equals(field.getIsPk());
|
||||||
boolean isRequired = "1".equals(field.getIsRequired());
|
boolean isRequired = "1".equals(field.getIsRequired());
|
||||||
@@ -235,7 +225,7 @@ public class GenTableServiceImpl implements IGenTableService {
|
|||||||
boolean isEdit = "1".equals(field.getIsEdit());
|
boolean isEdit = "1".equals(field.getIsEdit());
|
||||||
boolean isList = "1".equals(field.getIsList());
|
boolean isList = "1".equals(field.getIsList());
|
||||||
boolean isQuery = "1".equals(field.getIsQuery());
|
boolean isQuery = "1".equals(field.getIsQuery());
|
||||||
|
|
||||||
column.put("isPk", isPk ? 1 : 0);
|
column.put("isPk", isPk ? 1 : 0);
|
||||||
column.put("pk", isPk); // 添加pk别名
|
column.put("pk", isPk); // 添加pk别名
|
||||||
column.put("isRequired", isRequired);
|
column.put("isRequired", isRequired);
|
||||||
@@ -248,27 +238,27 @@ public class GenTableServiceImpl implements IGenTableService {
|
|||||||
column.put("list", isList); // 添加list别名
|
column.put("list", isList); // 添加list别名
|
||||||
column.put("isQuery", isQuery);
|
column.put("isQuery", isQuery);
|
||||||
column.put("query", isQuery); // 添加query别名
|
column.put("query", isQuery); // 添加query别名
|
||||||
|
|
||||||
column.put("queryType", field.getQueryType());
|
column.put("queryType", field.getQueryType());
|
||||||
column.put("htmlType", field.getHtmlType());
|
column.put("htmlType", field.getHtmlType());
|
||||||
column.put("dictType", field.getDictType());
|
column.put("dictType", field.getDictType());
|
||||||
column.put("sort", field.getSort());
|
column.put("sort", field.getSort());
|
||||||
|
|
||||||
// 添加readConverterExp方法
|
// 添加readConverterExp方法
|
||||||
column.put("readConverterExp", new Object() {
|
column.put("readConverterExp", new Object() {
|
||||||
});
|
});
|
||||||
|
|
||||||
// 根据Java类型添加相应的导入
|
// 根据Java类型添加相应的导入
|
||||||
addImportForJavaType(javaType, importList);
|
addImportForJavaType(javaType, importList);
|
||||||
|
|
||||||
columns.add(column);
|
columns.add(column);
|
||||||
|
|
||||||
// 设置主键列
|
// 设置主键列
|
||||||
if (isPk) {
|
if (isPk) {
|
||||||
pkColumn = column;
|
pkColumn = column;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 如果没有主键,使用第一个字段作为主键
|
// 如果没有主键,使用第一个字段作为主键
|
||||||
if (pkColumn == null && !columns.isEmpty()) {
|
if (pkColumn == null && !columns.isEmpty()) {
|
||||||
pkColumn = columns.get(0);
|
pkColumn = columns.get(0);
|
||||||
@@ -276,27 +266,28 @@ public class GenTableServiceImpl implements IGenTableService {
|
|||||||
pkColumn.put("isPk", 1);
|
pkColumn.put("isPk", 1);
|
||||||
pkColumn.put("pk", true);
|
pkColumn.put("pk", true);
|
||||||
}
|
}
|
||||||
|
|
||||||
context.put("columns", columns);
|
context.put("columns", columns);
|
||||||
context.put("pkColumn", pkColumn);
|
context.put("pkColumn", pkColumn);
|
||||||
context.put("importList", new ArrayList<>(importList));
|
context.put("importList", new ArrayList<>(importList));
|
||||||
|
|
||||||
return context;
|
return context;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据Java类型添加相应的导入
|
* 根据Java类型添加相应的导入
|
||||||
*/
|
*/
|
||||||
private void addImportForJavaType(String javaType, Set<String> importList) {
|
private void addImportForJavaType(String javaType, Set<String> importList) {
|
||||||
switch (javaType) {
|
switch (javaType) {
|
||||||
case "BigDecimal" -> importList.add("java.math.BigDecimal");
|
case "BigDecimal" -> importList.add("java.math.BigDecimal");
|
||||||
case "Date" -> importList.add("java.util.Date");
|
case "Date" -> importList.add("java.util.Date");
|
||||||
case "LocalDateTime" -> importList.add("java.time.LocalDateTime");
|
case "LocalDateTime" -> importList.add("java.time.LocalDateTime");
|
||||||
case "LocalDate" -> importList.add("java.time.LocalDate");
|
case "LocalDate" -> importList.add("java.time.LocalDate");
|
||||||
case "LocalTime" -> importList.add("java.time.LocalTime");
|
case "LocalTime" -> importList.add("java.time.LocalTime");
|
||||||
default -> {}
|
default -> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 从包名中提取模块名
|
* 从包名中提取模块名
|
||||||
@@ -320,10 +311,10 @@ public class GenTableServiceImpl implements IGenTableService {
|
|||||||
String packageName = GenConfig.getPackageName();
|
String packageName = GenConfig.getPackageName();
|
||||||
String tablePrefix = GenConfig.getTablePrefix();
|
String tablePrefix = GenConfig.getTablePrefix();
|
||||||
boolean autoRemovePre = GenConfig.getAutoRemovePre();
|
boolean autoRemovePre = GenConfig.getAutoRemovePre();
|
||||||
|
|
||||||
// 处理类名
|
// 处理类名
|
||||||
String baseClassName = schema.getTableName();
|
String baseClassName = schema.getTableName();
|
||||||
|
|
||||||
// 自动去除表前缀
|
// 自动去除表前缀
|
||||||
if (autoRemovePre && StrUtil.isNotBlank(tablePrefix)) {
|
if (autoRemovePre && StrUtil.isNotBlank(tablePrefix)) {
|
||||||
String[] prefixes = tablePrefix.split(",");
|
String[] prefixes = tablePrefix.split(",");
|
||||||
@@ -334,13 +325,13 @@ public class GenTableServiceImpl implements IGenTableService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
String className = toCamelCase(baseClassName, true); // 首字母大写,如:SysRole
|
String className = toCamelCase(baseClassName, true); // 首字母大写,如:SysRole
|
||||||
// 首字母小写,如:sysRole
|
// 首字母小写,如:sysRole
|
||||||
String moduleName = getModuleName(packageName);
|
String moduleName = getModuleName(packageName);
|
||||||
String javaPath = "src/main/java/";
|
String javaPath = "src/main/java/";
|
||||||
String mybatisPath = "src/main/resources/mapper/";
|
String mybatisPath = "src/main/resources/mapper/";
|
||||||
|
|
||||||
if (template.contains("domain.java.vm")) {
|
if (template.contains("domain.java.vm")) {
|
||||||
return javaPath + packageName.replace(".", "/") + "/domain/" + className + ".java";
|
return javaPath + packageName.replace(".", "/") + "/domain/" + className + ".java";
|
||||||
} else if (template.contains("mapper.java.vm")) {
|
} else if (template.contains("mapper.java.vm")) {
|
||||||
@@ -413,16 +404,17 @@ public class GenTableServiceImpl implements IGenTableService {
|
|||||||
return "String";
|
return "String";
|
||||||
}
|
}
|
||||||
String type = dbType.toLowerCase();
|
String type = dbType.toLowerCase();
|
||||||
if (type.contains("int") || type.contains("tinyint") || type.contains("smallint")) {
|
if (StrUtil.equalsAny(type, "int", "tinyint")) {
|
||||||
return "Integer";
|
return "Integer";
|
||||||
} else if (type.contains("bigint")) {
|
} else if (StrUtil.equalsAny(type, "bigint")) {
|
||||||
return "Long";
|
return "Long";
|
||||||
} else if (type.contains("decimal") || type.contains("numeric") || type.contains("float") || type.contains(
|
} else if (StrUtil.equalsAny(type, "decimal", "numeric", "float", "double")) {
|
||||||
"double")) {
|
|
||||||
return "BigDecimal";
|
return "BigDecimal";
|
||||||
} else if (type.contains("date") || type.contains("time")) {
|
} else if (StrUtil.equalsAny(type, "date")) {
|
||||||
return "Date";
|
return "LocalDate";
|
||||||
} else if (type.contains("bit") || type.contains("boolean")) {
|
} else if (StrUtil.equalsAny(type, "datetime", "timestamp")) {
|
||||||
|
return "LocalDateTime";
|
||||||
|
} else if (StrUtil.equalsAny(type, "bit", "boolean")) {
|
||||||
return "Boolean";
|
return "Boolean";
|
||||||
} else {
|
} else {
|
||||||
return "String";
|
return "String";
|
||||||
|
|||||||
@@ -211,7 +211,6 @@ public class SchemaFieldServiceImpl implements SchemaFieldService {
|
|||||||
result.put("tableName", schema.getTableName());
|
result.put("tableName", schema.getTableName());
|
||||||
result.put("tableComment", schema.getComment());
|
result.put("tableComment", schema.getComment());
|
||||||
result.put("className", toCamelCase(schema.getTableName(), true));
|
result.put("className", toCamelCase(schema.getTableName(), true));
|
||||||
// result.put("className", StrUtil.toCamelCase(schema.getTableName()));
|
|
||||||
result.put("tableCamelName", StrUtil.toCamelCase(schema.getTableName()));
|
result.put("tableCamelName", StrUtil.toCamelCase(schema.getTableName()));
|
||||||
result.put("functionName", schema.getName());
|
result.put("functionName", schema.getName());
|
||||||
result.put("schemaName", schema.getName());
|
result.put("schemaName", schema.getName());
|
||||||
@@ -363,16 +362,15 @@ public class SchemaFieldServiceImpl implements SchemaFieldService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
String type = dbType.toLowerCase();
|
String type = dbType.toLowerCase();
|
||||||
if (type.contains("int") || type.contains("tinyint") || type.contains("smallint")) {
|
if (StrUtil.equalsAny(type, "int", "tinyint", "smallint")) {
|
||||||
return "Integer";
|
return "Integer";
|
||||||
} else if (type.contains("bigint")) {
|
} else if (StrUtil.equalsAny(type, "bigint")) {
|
||||||
return "Long";
|
return "Long";
|
||||||
} else if (type.contains("decimal") || type.contains("numeric") || type.contains("float") || type.contains(
|
} else if (StrUtil.equalsAny(type, "decimal", "numeric", "float", "double")) {
|
||||||
"double")) {
|
|
||||||
return "BigDecimal";
|
return "BigDecimal";
|
||||||
} else if (type.contains("date") || type.contains("time")) {
|
} else if (StrUtil.equalsAny(type, "date", "datetime","timestamp")) {
|
||||||
return "Date";
|
return "Date";
|
||||||
} else if (type.contains("bit") || type.contains("boolean")) {
|
} else if (StrUtil.equalsAny(type, "bit", "boolean")) {
|
||||||
return "Boolean";
|
return "Boolean";
|
||||||
} else {
|
} else {
|
||||||
return "String";
|
return "String";
|
||||||
|
|||||||
@@ -25,11 +25,11 @@ import org.ruoyi.common.core.validate.EditGroup;
|
|||||||
public class ${ClassName}Bo implements Serializable {
|
public class ${ClassName}Bo implements Serializable {
|
||||||
|
|
||||||
#foreach ($column in $columns)
|
#foreach ($column in $columns)
|
||||||
#if(!$table.isSuperColumn($column.javaField) && ($column.isPk || $column.query || $column.insert || $column.edit))
|
#if($column.isPk || $column.query || $column.insert || $column.edit|| $column.required)
|
||||||
/**
|
/**
|
||||||
* $column.columnComment
|
* $column.columnComment
|
||||||
*/
|
*/
|
||||||
#if($column.insert && $column.edit)
|
#if(($column.insert && $column.edit) || $column.required)
|
||||||
#set($Group="AddGroup.class, EditGroup.class")
|
#set($Group="AddGroup.class, EditGroup.class")
|
||||||
#elseif($column.insert)
|
#elseif($column.insert)
|
||||||
#set($Group="AddGroup.class")
|
#set($Group="AddGroup.class")
|
||||||
|
|||||||
@@ -1,22 +1,10 @@
|
|||||||
package ${packageName}.domain;
|
package ${packageName}.domain;
|
||||||
|
|
||||||
#foreach ($column in $columns)
|
|
||||||
#if($column.javaField=='tenantId')
|
|
||||||
#set($IsTenant=1)
|
|
||||||
#end
|
|
||||||
#end
|
|
||||||
#if($IsTenant==1)
|
|
||||||
import core.tenant.common.org.ruoyi.TenantEntity;
|
|
||||||
#else
|
|
||||||
#end
|
|
||||||
import com.baomidou.mybatisplus.annotation.*;
|
import com.baomidou.mybatisplus.annotation.*;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.EqualsAndHashCode;
|
#foreach ($import in $importList)
|
||||||
#foreach ($import in $importList)
|
import ${import};
|
||||||
import ${import};
|
#end
|
||||||
#end
|
|
||||||
|
|
||||||
import org.ruoyi.core.domain.BaseEntity;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ${functionName}对象 ${tableName}
|
* ${functionName}对象 ${tableName}
|
||||||
@@ -24,15 +12,9 @@ import org.ruoyi.core.domain.BaseEntity;
|
|||||||
* @author ${author}
|
* @author ${author}
|
||||||
* @date ${datetime}
|
* @date ${datetime}
|
||||||
*/
|
*/
|
||||||
#if($IsTenant==1)
|
|
||||||
#set($Entity="TenantEntity")
|
|
||||||
#else
|
|
||||||
#set($Entity="BaseEntity")
|
|
||||||
#end
|
|
||||||
@Data
|
@Data
|
||||||
@EqualsAndHashCode(callSuper = true)
|
|
||||||
@TableName("${tableName}")
|
@TableName("${tableName}")
|
||||||
public class ${ClassName} extends ${Entity} {
|
public class ${ClassName} implements Serializable {
|
||||||
|
|
||||||
|
|
||||||
#foreach ($column in $columns)
|
#foreach ($column in $columns)
|
||||||
|
|||||||
Reference in New Issue
Block a user