优化代码生成

This commit is contained in:
AlanScipio
2024-02-18 16:58:39 +08:00
parent 09b96b5cfa
commit 7a257eb264
39 changed files with 3614 additions and 436 deletions

View File

@@ -0,0 +1,60 @@
package com.ruoyi.gen.config;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.reflect.ReflectUtils;
import com.ruoyi.common.core.web.domain.ExtBaseEntity;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;
/**
* 共通字段设定
*
* @author Alan Scipio
* created on 2024/2/18
*/
public class CommonFieldsSet {
private static final List<String> COMMON_FIELDS = new ArrayList<>();
static {
Field[] fields = ReflectUtils.getFieldsDeep(ExtBaseEntity.class);
for (Field field : fields) {
//跳过静态字段
if (Modifier.isStatic(field.getModifiers())) {
continue;
}
//将驼峰命名转换为下划线命名
String columnName = StringUtils.toUnderScoreCase(field.getName());
COMMON_FIELDS.add(columnName);
}
//从属部门ID也相当于是共通字段
COMMON_FIELDS.add("DEPT_ID");
//备注字段默认都是共通字段
COMMON_FIELDS.add("remark*");
}
public static boolean isCommonField(String columnName) {
for (String commonField : COMMON_FIELDS) {
//如果字段名相同则是共通字段,不区分大小写
if (commonField.equalsIgnoreCase(columnName)) {
return true;
} else if (commonField.endsWith("*")) {
//如果字段名以*结尾则是通配符,不区分大小写
String prefix = commonField.substring(0, commonField.length() - 1);
if (columnName.toLowerCase().startsWith(prefix.toLowerCase())) {
return true;
}
}
}
return false;
}
public static List<String> getCommonFields() {
return COMMON_FIELDS;
}
}

View File

@@ -12,96 +12,145 @@ import java.util.List;
/**
* 业务表 gen_table
*
*
* @author ruoyi
*/
public class GenTable extends BaseEntity
{
public class GenTable extends BaseEntity {
@Serial
private static final long serialVersionUID = 1L;
/** 编号 */
/**
* 编号
*/
private Long tableId;
/** 表名称 */
/**
* 表名称
*/
@NotBlank(message = "表名称不能为空")
private String tableName;
/** 表描述 */
/**
* 表描述
*/
@NotBlank(message = "表描述不能为空")
private String tableComment;
/** 关联父表的表名 */
/**
* 关联父表的表名
*/
private String subTableName;
/** 本表关联父表的外键名 */
/**
* 本表关联父表的外键名
*/
private String subTableFkName;
/** 实体类名称(首字母大写) */
/**
* 实体类名称(首字母大写)
*/
@NotBlank(message = "实体类名称不能为空")
private String className;
/** 使用的模板crud单表操作 tree树表操作 sub主子表操作 */
/**
* 使用的模板crud单表操作 tree树表操作 sub主子表操作
*/
private String tplCategory;
/** 前端类型element-ui模版 element-plus模版 */
/**
* 前端类型element-ui模版 element-plus模版
*/
private String tplWebType;
/** 后端类型MyBaitsDynamicSQL模板常规模板 */
/**
* 后端类型MyBaitsDynamicSQL模板常规模板
*/
private String tplBackendType;
/** 生成包路径 */
/**
* 生成包路径
*/
@NotBlank(message = "生成包路径不能为空")
private String packageName;
/** 生成模块名 */
/**
* 生成模块名
*/
@NotBlank(message = "生成模块名不能为空")
private String moduleName;
/** 生成业务名 */
/**
* 生成业务名
*/
@NotBlank(message = "生成业务名不能为空")
private String businessName;
/** 生成功能名 */
/**
* 生成功能名
*/
@NotBlank(message = "生成功能名不能为空")
private String functionName;
/** 生成作者 */
/**
* 生成作者
*/
@NotBlank(message = "作者不能为空")
private String functionAuthor;
/** 生成代码方式0zip压缩包 1自定义路径 */
/**
* 生成代码方式0zip压缩包 1自定义路径
*/
private String genType;
/** 生成路径(不填默认项目路径) */
/**
* 生成路径(不填默认项目路径)
*/
private String genPath;
/** 主键信息 */
/**
* 主键信息
*/
private GenTableColumn pkColumn;
/** 子表信息 */
/**
* 子表信息
*/
private GenTable subTable;
/** 表列信息 */
/**
* 表列信息
*/
@Valid
private List<GenTableColumn> columns;
/** 其它生成选项 */
/**
* 其它生成选项
*/
private String options;
/** 树编码字段 */
/**
* 树编码字段
*/
private String treeCode;
/** 树父编码字段 */
/**
* 树父编码字段
*/
private String treeParentCode;
/** 树名称字段 */
/**
* 树名称字段
*/
private String treeName;
/** 上级菜单ID字段 */
/**
* 上级菜单ID字段
*/
private String parentMenuId;
/** 上级菜单名称字段 */
/**
* 上级菜单名称字段
*/
private String parentMenuName;
public String getTplBackendType() {
@@ -112,283 +161,228 @@ public class GenTable extends BaseEntity
this.tplBackendType = tplBackendType;
}
public Long getTableId()
{
public Long getTableId() {
return tableId;
}
public void setTableId(Long tableId)
{
public void setTableId(Long tableId) {
this.tableId = tableId;
}
public String getTableName()
{
public String getTableName() {
return tableName;
}
public void setTableName(String tableName)
{
public void setTableName(String tableName) {
this.tableName = tableName;
}
public String getTableComment()
{
public String getTableComment() {
return tableComment;
}
public void setTableComment(String tableComment)
{
public void setTableComment(String tableComment) {
this.tableComment = tableComment;
}
public String getSubTableName()
{
public String getSubTableName() {
return subTableName;
}
public void setSubTableName(String subTableName)
{
public void setSubTableName(String subTableName) {
this.subTableName = subTableName;
}
public String getSubTableFkName()
{
public String getSubTableFkName() {
return subTableFkName;
}
public void setSubTableFkName(String subTableFkName)
{
public void setSubTableFkName(String subTableFkName) {
this.subTableFkName = subTableFkName;
}
public String getClassName()
{
public String getClassName() {
return className;
}
public void setClassName(String className)
{
public void setClassName(String className) {
this.className = className;
}
public String getTplCategory()
{
public String getTplCategory() {
return tplCategory;
}
public void setTplCategory(String tplCategory)
{
public void setTplCategory(String tplCategory) {
this.tplCategory = tplCategory;
}
public String getTplWebType()
{
public String getTplWebType() {
return tplWebType;
}
public void setTplWebType(String tplWebType)
{
public void setTplWebType(String tplWebType) {
this.tplWebType = tplWebType;
}
public String getPackageName()
{
public String getPackageName() {
return packageName;
}
public void setPackageName(String packageName)
{
public void setPackageName(String packageName) {
this.packageName = packageName;
}
public String getModuleName()
{
public String getModuleName() {
return moduleName;
}
public void setModuleName(String moduleName)
{
public void setModuleName(String moduleName) {
this.moduleName = moduleName;
}
public String getBusinessName()
{
public String getBusinessName() {
return businessName;
}
public void setBusinessName(String businessName)
{
public void setBusinessName(String businessName) {
this.businessName = businessName;
}
public String getFunctionName()
{
public String getFunctionName() {
return functionName;
}
public void setFunctionName(String functionName)
{
public void setFunctionName(String functionName) {
this.functionName = functionName;
}
public String getFunctionAuthor()
{
public String getFunctionAuthor() {
return functionAuthor;
}
public void setFunctionAuthor(String functionAuthor)
{
public void setFunctionAuthor(String functionAuthor) {
this.functionAuthor = functionAuthor;
}
public String getGenType()
{
public String getGenType() {
return genType;
}
public void setGenType(String genType)
{
public void setGenType(String genType) {
this.genType = genType;
}
public String getGenPath()
{
public String getGenPath() {
return genPath;
}
public void setGenPath(String genPath)
{
public void setGenPath(String genPath) {
this.genPath = genPath;
}
public GenTableColumn getPkColumn()
{
public GenTableColumn getPkColumn() {
return pkColumn;
}
public void setPkColumn(GenTableColumn pkColumn)
{
public void setPkColumn(GenTableColumn pkColumn) {
this.pkColumn = pkColumn;
}
public GenTable getSubTable()
{
public GenTable getSubTable() {
return subTable;
}
public void setSubTable(GenTable subTable)
{
public void setSubTable(GenTable subTable) {
this.subTable = subTable;
}
public List<GenTableColumn> getColumns()
{
public List<GenTableColumn> getColumns() {
return columns;
}
public void setColumns(List<GenTableColumn> columns)
{
public void setColumns(List<GenTableColumn> columns) {
this.columns = columns;
}
public String getOptions()
{
public String getOptions() {
return options;
}
public void setOptions(String options)
{
public void setOptions(String options) {
this.options = options;
}
public String getTreeCode()
{
public String getTreeCode() {
return treeCode;
}
public void setTreeCode(String treeCode)
{
public void setTreeCode(String treeCode) {
this.treeCode = treeCode;
}
public String getTreeParentCode()
{
public String getTreeParentCode() {
return treeParentCode;
}
public void setTreeParentCode(String treeParentCode)
{
public void setTreeParentCode(String treeParentCode) {
this.treeParentCode = treeParentCode;
}
public String getTreeName()
{
public String getTreeName() {
return treeName;
}
public void setTreeName(String treeName)
{
public void setTreeName(String treeName) {
this.treeName = treeName;
}
public String getParentMenuId()
{
public String getParentMenuId() {
return parentMenuId;
}
public void setParentMenuId(String parentMenuId)
{
public void setParentMenuId(String parentMenuId) {
this.parentMenuId = parentMenuId;
}
public String getParentMenuName()
{
public String getParentMenuName() {
return parentMenuName;
}
public void setParentMenuName(String parentMenuName)
{
public void setParentMenuName(String parentMenuName) {
this.parentMenuName = parentMenuName;
}
public boolean isSub()
{
public boolean isSub() {
return isSub(this.tplCategory);
}
public static boolean isSub(String tplCategory)
{
public static boolean isSub(String tplCategory) {
return tplCategory != null && StringUtils.equals(GenConstants.TPL_SUB, tplCategory);
}
public boolean isTree()
{
public boolean isTree() {
return isTree(this.tplCategory);
}
public static boolean isTree(String tplCategory)
{
public static boolean isTree(String tplCategory) {
return tplCategory != null && StringUtils.equals(GenConstants.TPL_TREE, tplCategory);
}
public boolean isCrud()
{
public boolean isCrud() {
return isCrud(this.tplCategory);
}
public static boolean isCrud(String tplCategory)
{
public static boolean isCrud(String tplCategory) {
return tplCategory != null && StringUtils.equals(GenConstants.TPL_CRUD, tplCategory);
}
public boolean isSuperColumn(String javaField)
{
public boolean isSuperColumn(String javaField) {
return isSuperColumn(this.tplCategory, javaField);
}
public static boolean isSuperColumn(String tplCategory, String javaField)
{
if (isTree(tplCategory))
{
public static boolean isSuperColumn(String tplCategory, String javaField) {
if (isTree(tplCategory)) {
return StringUtils.equalsAnyIgnoreCase(javaField,
ArrayUtils.addAll(GenConstants.TREE_ENTITY, GenConstants.BASE_ENTITY));
}

View File

@@ -3,7 +3,7 @@ package com.ruoyi.gen.service;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.gen.domain.GenTableColumn;
import com.ruoyi.gen.mapper.GenTableColumnMapper;
import org.springframework.beans.factory.annotation.Autowired;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import java.util.List;
@@ -15,7 +15,7 @@ import java.util.List;
*/
@Service
public class GenTableColumnServiceImpl implements IGenTableColumnService {
@Autowired
@Resource
private GenTableColumnMapper genTableColumnMapper;
/**

View File

@@ -15,6 +15,7 @@ import com.ruoyi.gen.mapper.GenTableMapper;
import com.ruoyi.gen.util.GenUtils;
import com.ruoyi.gen.util.VelocityInitializer;
import com.ruoyi.gen.util.VelocityUtils;
import jakarta.annotation.Resource;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.velocity.Template;
@@ -22,7 +23,6 @@ import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@@ -47,10 +47,10 @@ import java.util.zip.ZipOutputStream;
public class GenTableServiceImpl implements IGenTableService {
private static final Logger log = LoggerFactory.getLogger(GenTableServiceImpl.class);
@Autowired
@Resource
private GenTableMapper genTableMapper;
@Autowired
@Resource
private GenTableColumnMapper genTableColumnMapper;
/**

View File

@@ -2,6 +2,7 @@ package com.ruoyi.gen.util;
import com.ruoyi.common.core.constant.GenConstants;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.gen.config.CommonFieldsSet;
import com.ruoyi.gen.config.GenConfig;
import com.ruoyi.gen.domain.GenTable;
import com.ruoyi.gen.domain.GenTableColumn;
@@ -68,19 +69,21 @@ public class GenUtils {
}
}
// 插入字段(默认所有字段都需要插入)
column.setIsInsert(GenConstants.REQUIRE);
// 插入字段(默认所有字段都需要插入,除了共通字段
if (!CommonFieldsSet.isCommonField(columnName)) {
column.setIsInsert(GenConstants.REQUIRE);
}
// 编辑字段
if (!arraysContains(GenConstants.COLUMNNAME_NOT_EDIT, columnName) && !column.isPk()) {
if (!arraysContains(GenConstants.COLUMNNAME_NOT_EDIT, columnName) && !column.isPk() && !CommonFieldsSet.isCommonField(columnName)) {
column.setIsEdit(GenConstants.REQUIRE);
}
// 列表字段
if (!arraysContains(GenConstants.COLUMNNAME_NOT_LIST, columnName) && !column.isPk()) {
if (!arraysContains(GenConstants.COLUMNNAME_NOT_LIST, columnName) && !column.isPk() && !CommonFieldsSet.isCommonField(columnName)) {
column.setIsList(GenConstants.REQUIRE);
}
// 查询字段
if (!arraysContains(GenConstants.COLUMNNAME_NOT_QUERY, columnName) && !column.isPk()) {
if (!arraysContains(GenConstants.COLUMNNAME_NOT_QUERY, columnName) && !column.isPk() && !CommonFieldsSet.isCommonField(columnName)) {
column.setIsQuery(GenConstants.REQUIRE);
}

View File

@@ -4,7 +4,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ruoyi.gen.mapper.GenTableColumnMapper">
<resultMap type="GenTableColumn" id="GenTableColumnResult">
<resultMap type="com.ruoyi.gen.domain.GenTableColumn" id="GenTableColumnResult">
<id property="columnId" column="column_id" />
<result property="tableId" column="table_id" />
<result property="columnName" column="column_name" />
@@ -33,7 +33,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
select column_id, table_id, column_name, column_comment, column_type, java_type, java_field, is_pk, is_increment, is_required, is_insert, is_edit, is_list, is_query, query_type, html_type, dict_type, sort, create_by, create_time, update_by, update_time from gen_table_column
</sql>
<select id="selectGenTableColumnListByTableId" parameterType="GenTableColumn" resultMap="GenTableColumnResult">
<select id="selectGenTableColumnListByTableId" parameterType="com.ruoyi.gen.domain.GenTableColumn" resultMap="GenTableColumnResult">
<include refid="selectGenTableColumnVo"/>
where table_id = #{tableId}
order by sort
@@ -45,7 +45,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
order by ordinal_position
</select>
<insert id="insertGenTableColumn" parameterType="GenTableColumn" useGeneratedKeys="true" keyProperty="columnId">
<insert id="insertGenTableColumn" parameterType="com.ruoyi.gen.domain.GenTableColumn" useGeneratedKeys="true" keyProperty="columnId">
insert into gen_table_column (
<if test="tableId != null and tableId != ''">table_id,</if>
<if test="columnName != null and columnName != ''">column_name,</if>
@@ -89,7 +89,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
)
</insert>
<update id="updateGenTableColumn" parameterType="GenTableColumn">
<update id="updateGenTableColumn" parameterType="com.ruoyi.gen.domain.GenTableColumn">
update gen_table_column
<set>
<if test="columnComment != null">column_comment = #{columnComment},</if>

View File

@@ -22,7 +22,7 @@ import org.springframework.transaction.annotation.Transactional;
import ${packageName}.domain.${subClassName};
#end
import com.ruoyi.common.core.web.domain.ExtBaseEntity;
import com.ruoyi.common.security.utils.SecurityUtilsExt
import com.ruoyi.common.security.utils.SecurityUtilsExt;
import ${packageName}.mapper.${ClassName}Mapper;
import ${packageName}.domain.${ClassName};
import ${packageName}.mapper.${ClassName}DynamicSqlSupport;
@@ -91,7 +91,7 @@ public class ${ClassName}ServiceImpl implements I${ClassName}Service
#end
## 对like条件的特殊处理
#if ($column.queryType == "LIKE")
.and(${ClassName}DynamicSqlSupport.$column.javaField, SqlBuilder.${conditionFuncName}(${className}.get${column.javaFieldUpper}() == null ? null : "%" + ${className}.get${column.javaFieldUpper}() + "%")
.and(${ClassName}DynamicSqlSupport.$column.javaField, SqlBuilder.${conditionFuncName}(${className}.get${column.javaFieldUpper}() == null ? null : "%" + ${className}.get${column.javaFieldUpper}() + "%"))
#else
.and(${ClassName}DynamicSqlSupport.$column.javaField, SqlBuilder.${conditionFuncName}(${className}.get${column.javaFieldUpper}()))
#end
@@ -114,11 +114,11 @@ public class ${ClassName}ServiceImpl implements I${ClassName}Service
@Override
public int insert${ClassName}(${ClassName} ${className})
{
#foreach ($column in $columns)
#if($column.javaField == 'createTime')
${className}.setCreateTime(DateUtils.getNowDate());
#end
#end
###foreach ($column in $columns)
###if($column.javaField == 'createTime')
## ${className}.setCreateTime(DateUtils.getNowDate());
###end
###end
#if($table.sub)
int rows = ${className}Mapper.insertSelective(${className});
insert${subClassName}(${className});
@@ -140,11 +140,11 @@ public class ${ClassName}ServiceImpl implements I${ClassName}Service
@Override
public int update${ClassName}(${ClassName} ${className})
{
#foreach ($column in $columns)
#if($column.javaField == 'updateTime')
${className}.setUpdateTime(DateUtils.getNowDate());
#end
#end
###foreach ($column in $columns)
###if($column.javaField == 'updateTime')
## ${className}.setUpdateTime(DateUtils.getNowDate());
###end
###end
#if($table.sub)
${className}Mapper.delete${subClassName}By${subTableFkClassName}(${className}.get${pkColumn.capJavaField}());
insert${subClassName}(${className});
@@ -169,11 +169,11 @@ public class ${ClassName}ServiceImpl implements I${ClassName}Service
#end
## return ${className}Mapper.delete(dsl -> dsl.where(${ClassName}DynamicSqlSupport.${pkColumn.javaField}, SqlBuilder.isIn(${pkColumn.javaField}s)));
String userId = SecurityUtilsExt.getUserIdStr();
UpdateStatementProvider provider = SqlBuilder.update(UnitInfoDynamicSqlSupport.${className})
UpdateStatementProvider provider = SqlBuilder.update(${ClassName}DynamicSqlSupport.${className})
.set(${ClassName}DynamicSqlSupport.deleteFlag).equalTo(ExtBaseEntity.DELETED)
.set(${ClassName}DynamicSqlSupport.updateTime).equalTo(DateUtils.getNowDate())
.set(${ClassName}DynamicSqlSupport.updateBy).equalTo(userId)
.where(${ClassName}DynamicSqlSupport.unitCode, SqlBuilder.isIn(${pkColumn.javaField}s))
.where(${ClassName}DynamicSqlSupport.${pkColumn.javaField}, SqlBuilder.isIn(${pkColumn.javaField}s))
.build()
.render(RenderingStrategies.MYBATIS3);
return ${className}Mapper.update(provider);

View File

@@ -108,7 +108,7 @@
<right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
</el-row>
<el-table v-loading="loading" :data="${businessName}List" @selection-change="handleSelectionChange">
<el-table v-loading="loading" :data="${businessName}List" @selection-change="handleSelectionChange" show-overflow-tooltip="true">
<el-table-column type="selection" width="55" align="center" />
#foreach($column in $columns)
#set($javaField=$column.javaField)