From 9c74df5d3701fc4ee080470286e78624cfb62e7c Mon Sep 17 00:00:00 2001 From: keyleaf Date: Tue, 25 May 2021 15:44:37 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E8=81=94=E8=A1=A8=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ruoyi/common/constant/GenConstants.java | 5 +- .../com/ruoyi/generator/domain/GenTable.java | 75 ++++++++++- .../service/GenTableServiceImpl.java | 35 ++++- .../ruoyi/generator/util/VelocityUtils.java | 110 +++++++++++++++- .../mapper/generator/GenTableMapper.xml | 21 +-- .../src/main/resources/vm/java/vo.java.vm | 18 +++ .../src/main/resources/vm/vue/index.vue.vm | 82 +++++++++++- .../src/main/resources/vm/xml/mapper.xml.vm | 52 +++++++- ruoyi-ui/src/views/tool/gen/editTable.vue | 2 +- ruoyi-ui/src/views/tool/gen/genInfoForm.vue | 120 ++++++++++++++++++ sql/ry_20210523.sql | 1 + 11 files changed, 495 insertions(+), 26 deletions(-) create mode 100644 sql/ry_20210523.sql diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/constant/GenConstants.java b/ruoyi-common/src/main/java/com/ruoyi/common/constant/GenConstants.java index 1fa328e45..60598a64b 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/constant/GenConstants.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/constant/GenConstants.java @@ -2,7 +2,7 @@ package com.ruoyi.common.constant; /** * 代码生成通用常量 - * + * * @author ruoyi */ public class GenConstants @@ -16,6 +16,9 @@ public class GenConstants /** 主子表(增删改查) */ public static final String TPL_SUB = "sub"; + /** 关联表(增删改查) */ + public static final String TPL_JOIN = "join"; + /** 树编码字段 */ public static final String TREE_CODE = "treeCode"; diff --git a/ruoyi-generator/src/main/java/com/ruoyi/generator/domain/GenTable.java b/ruoyi-generator/src/main/java/com/ruoyi/generator/domain/GenTable.java index 5d5c6a483..31757fdc0 100644 --- a/ruoyi-generator/src/main/java/com/ruoyi/generator/domain/GenTable.java +++ b/ruoyi-generator/src/main/java/com/ruoyi/generator/domain/GenTable.java @@ -1,7 +1,10 @@ package com.ruoyi.generator.domain; import cn.hutool.core.util.StrUtil; +import com.alibaba.fastjson.JSON; import com.baomidou.mybatisplus.annotation.*; +import com.baomidou.mybatisplus.extension.handlers.AbstractJsonTypeHandler; +import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler; import com.fasterxml.jackson.annotation.JsonFormat; import com.ruoyi.common.constant.GenConstants; import lombok.*; @@ -11,10 +14,7 @@ import org.apache.commons.lang3.ArrayUtils; import javax.validation.Valid; import javax.validation.constraints.NotBlank; import java.io.Serializable; -import java.util.Date; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; /** * 业务表 gen_table @@ -114,6 +114,18 @@ public class GenTable implements Serializable { @TableField(exist = false) private GenTableColumn pkColumn; + /** + * 关联表配置信息 + */ + @TableField(typeHandler = JacksonTypeHandler.class) + private List joinInfos; + + /** + * 关联表信息 + */ + @TableField(exist = false) + private Map joinTableMap = new HashMap<>(); + /** * 子表信息 */ @@ -234,4 +246,57 @@ public class GenTable implements Serializable { } return StrUtil.equalsAnyIgnoreCase(javaField, GenConstants.BASE_ENTITY); } -} \ No newline at end of file + + /** + * 关联表信息 + * + * 举例 + * select A.xxx, B.xxx(showFields) from A left join B(joinTable) on A.fk_id(tableFkName) = B.id(joinField) + */ + @Data + public static class JoinInfo { + + /** + * 关联表的名称 + */ + private String joinTable; + + /** + * 主表的关联字段 + */ + private String tableFkName; + + /** + * 关联表的关联字段 + */ + private String joinField; + + /** + * 关联表中要展示的字段 + */ + private LinkedHashSet showFields; + + /** + * 查询列表要展示的字段 + */ + private LinkedHashSet queryFields; + + } + + /** + * 自定义类型转换器 + * 服务于GenTableMapper.xml + */ + public static class JoinInfoTypeHandler extends AbstractJsonTypeHandler> { + + @Override + protected List parse(String json) { + return JSON.parseArray(json, JoinInfo.class); + } + + @Override + protected String toJson(List obj) { + return JSON.toJSONString(obj); + } + } +} diff --git a/ruoyi-generator/src/main/java/com/ruoyi/generator/service/GenTableServiceImpl.java b/ruoyi-generator/src/main/java/com/ruoyi/generator/service/GenTableServiceImpl.java index e71e88962..7f205ec79 100644 --- a/ruoyi-generator/src/main/java/com/ruoyi/generator/service/GenTableServiceImpl.java +++ b/ruoyi-generator/src/main/java/com/ruoyi/generator/service/GenTableServiceImpl.java @@ -24,12 +24,14 @@ import com.ruoyi.generator.util.VelocityInitializer; import com.ruoyi.generator.util.VelocityUtils; import lombok.extern.slf4j.Slf4j; import org.apache.commons.io.IOUtils; +import org.apache.commons.lang3.StringUtils; import org.apache.velocity.Template; import org.apache.velocity.VelocityContext; import org.apache.velocity.app.Velocity; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import org.springframework.util.CollectionUtils; import java.io.ByteArrayOutputStream; import java.io.File; @@ -203,6 +205,8 @@ public class GenTableServiceImpl extends ServiceImpl i GenTable table = baseMapper.selectGenTableById(tableId); // 设置主子表信息 setSubTable(table); + // 设置关联表信息 + setJoinTable(table); // 设置主键列信息 setPkColumn(table); VelocityInitializer.initVelocity(); @@ -247,6 +251,8 @@ public class GenTableServiceImpl extends ServiceImpl i GenTable table = baseMapper.selectGenTableByName(tableName); // 设置主子表信息 setSubTable(table); + // 设置关联表信息 + setJoinTable(table); // 设置主键列信息 setPkColumn(table); @@ -329,6 +335,8 @@ public class GenTableServiceImpl extends ServiceImpl i GenTable table = baseMapper.selectGenTableByName(tableName); // 设置主子表信息 setSubTable(table); + // 设置关联表信息 + setJoinTable(table); // 设置主键列信息 setPkColumn(table); @@ -378,7 +386,11 @@ public class GenTableServiceImpl extends ServiceImpl i } else if (Validator.isEmpty(genTable.getSubTableFkName())) { throw new CustomException("子表关联的外键名不能为空"); } - } + } else if (GenConstants.TPL_JOIN.equals(genTable.getTplCategory())) { + if (CollectionUtils.isEmpty(genTable.getJoinInfos())) { + throw new CustomException("关联表的相关配置不能为空"); + } + } } } @@ -422,6 +434,25 @@ public class GenTableServiceImpl extends ServiceImpl i } } + /** + * 设置关联表的信息 + * @param table + */ + public void setJoinTable(GenTable table) { + Map joinTableMap = table.getJoinTableMap(); + List joinInfos = table.getJoinInfos(); + if (!CollectionUtils.isEmpty(joinInfos)) { + for (GenTable.JoinInfo joinInfo: joinInfos) { + String joinTable = joinInfo.getJoinTable(); + if (StringUtils.isNotBlank(joinTable)) { + GenTable joinGenTable = baseMapper.selectGenTableByName(joinTable); + setPkColumn(joinGenTable); + joinTableMap.put(joinTable, joinGenTable); + } + } + } + } + /** * 设置代码生成其他选项值 * @@ -458,4 +489,4 @@ public class GenTableServiceImpl extends ServiceImpl i } return genPath + File.separator + VelocityUtils.getFileName(template, table); } -} \ No newline at end of file +} diff --git a/ruoyi-generator/src/main/java/com/ruoyi/generator/util/VelocityUtils.java b/ruoyi-generator/src/main/java/com/ruoyi/generator/util/VelocityUtils.java index 69421cc01..81ce0556d 100644 --- a/ruoyi-generator/src/main/java/com/ruoyi/generator/util/VelocityUtils.java +++ b/ruoyi-generator/src/main/java/com/ruoyi/generator/util/VelocityUtils.java @@ -1,5 +1,6 @@ package com.ruoyi.generator.util; +import cn.hutool.core.collection.CollUtil; import cn.hutool.core.lang.Validator; import cn.hutool.core.util.StrUtil; import com.alibaba.fastjson.JSONObject; @@ -7,15 +8,15 @@ import com.ruoyi.common.constant.GenConstants; import com.ruoyi.common.utils.DateUtils; import com.ruoyi.generator.domain.GenTable; import com.ruoyi.generator.domain.GenTableColumn; +import org.apache.commons.lang3.StringUtils; import org.apache.velocity.VelocityContext; +import org.springframework.util.CollectionUtils; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; +import java.util.*; /** * 模板处理工具类 - * + * * @author ruoyi */ public class VelocityUtils @@ -69,6 +70,10 @@ public class VelocityUtils { setSubVelocityContext(velocityContext, genTable); } + if (GenConstants.TPL_JOIN.equals(tplCategory)) + { + setJoinVelocityContext(velocityContext, genTable); + } return velocityContext; } @@ -120,6 +125,101 @@ public class VelocityUtils context.put("subImportList", getImportList(genTable.getSubTable())); } + /** + * 关联表类型的相关参数封装 + * @param context + * @param genTable + */ + public static void setJoinVelocityContext(VelocityContext context, GenTable genTable) { + List joinInfos = genTable.getJoinInfos(); + Map joinTableMap = genTable.getJoinTableMap(); + List genTableColumns = genTable.getColumns(); + Map genTableColumnMap = CollUtil.fieldValueMap(genTableColumns, "columnName"); + List> joinInfoList = new ArrayList<>(); + context.put("joinInfos", joinInfoList); + if (!CollectionUtils.isEmpty(joinInfos)) { + for (GenTable.JoinInfo joinInfo : joinInfos) { + Map joinInfoMap = new HashMap<>(); + joinInfoList.add(joinInfoMap); + + String joinTable = joinInfo.getJoinTable(); + String joinTableClassName = StrUtil.toCamelCase(joinTable); + String joinTableLowerClassName = StrUtil.lowerFirst(joinTableClassName); + String joinTableUpperClassName = StrUtil.upperFirst(joinTableClassName); + + joinInfoMap.put("joinGenTable", joinTableMap.get(joinTable)); + joinInfoMap.put("joinGenTableBusinessName", StrUtil.upperFirst(joinTableMap.get(joinTable).getBusinessName())); + joinInfoMap.put("joinGenTableModuleName", StrUtil.upperFirst(joinTableMap.get(joinTable).getModuleName())); + joinInfoMap.put("joinTable", joinTable); + joinInfoMap.put("joinTableClassName", joinTableClassName); + joinInfoMap.put("joinTableLowerClassName", joinTableLowerClassName); + joinInfoMap.put("joinTableUpperClassName", joinTableUpperClassName); + + String tableFkName = joinInfo.getTableFkName(); + joinInfoMap.put("tableFkName", tableFkName); + GenTableColumn genTableColumn = genTableColumnMap.get(tableFkName); + // 主表关联的字段(tableFkName),如果本身就在列表中展示,就会在各个vm中生成拼接,如果不需要在列表中展示,关联查询却仍需要这个字段,为避免重复生成增加该flag进行判断 + joinInfoMap.put("tableFkGenerateFlag", !genTableColumn.isList()); + joinInfoMap.put("tableFkColumn", genTableColumn); + + String joinField = joinInfo.getJoinField(); + joinInfoMap.put("joinField", joinField); + + + LinkedHashSet showFields = joinInfo.getShowFields(); + List> showFieldList = new ArrayList<>(); + joinInfoMap.put("showFields", showFieldList); + if (!CollectionUtils.isEmpty(showFields)) { + GenTable joinGenTable = joinTableMap.get(joinTable); + List columns = joinGenTable.getColumns(); + Map columnMap = CollUtil.fieldValueMap(columns, "columnName"); + + for (String showField : showFields) { + Map showFieldMap = new HashMap<>(); + showFieldList.add(showFieldMap); + String showFiledName = StrUtil.toCamelCase(showField); + String showFiledUpperName = StrUtil.upperFirst(showFiledName); + showFieldMap.put("showField", showField); + showFieldMap.put("showFiledName", showFiledName); + showFieldMap.put("showFiledUpperName", showFiledUpperName); + GenTableColumn column = columnMap.get(showField); + showFieldMap.put("showFieldJavaType", column.getJavaType()); + showFieldMap.put("showFieldDictType", column.getDictType()); + if (StringUtils.isNotBlank(column.getColumnComment())) { + showFieldMap.put("showFieldComment", column.getColumnComment()); + } else { + showFieldMap.put("showFieldComment", joinTableLowerClassName + showFiledUpperName); + } + } + } + LinkedHashSet queryFields = joinInfo.getQueryFields(); + List> queryFieldList = new ArrayList<>(); + joinInfoMap.put("queryFields", queryFieldList); + if (!CollectionUtils.isEmpty(queryFields)) { + GenTable joinGenTable = joinTableMap.get(joinTable); + List columns = joinGenTable.getColumns(); + Map columnMap = CollUtil.fieldValueMap(columns, "columnName"); + for (String queryField : queryFields) { + Map queryFieldMap = new HashMap<>(); + queryFieldList.add(queryFieldMap); + String queryFiledName = StrUtil.toCamelCase(queryField); + String queryFiledUpperName = StrUtil.upperFirst(queryFiledName); + queryFieldMap.put("queryField", queryField); + queryFieldMap.put("queryFiledName", queryFiledName); + queryFieldMap.put("queryFiledUpperName", queryFiledUpperName); + + GenTableColumn column = columnMap.get(queryField); + if (StringUtils.isNotBlank(column.getColumnComment())) { + queryFieldMap.put("queryFieldComment", column.getColumnComment()); + } else { + queryFieldMap.put("queryFieldComment", joinTableLowerClassName + queryFiledUpperName); + } + } + } + } + } + } + /** * 获取模板信息 * @@ -140,7 +240,7 @@ public class VelocityUtils templates.add("vm/xml/mapper.xml.vm"); templates.add("vm/sql/sql.vm"); templates.add("vm/js/api.js.vm"); - if (GenConstants.TPL_CRUD.equals(tplCategory)) + if (GenConstants.TPL_CRUD.equals(tplCategory) || GenConstants.TPL_JOIN.equals(tplCategory)) { templates.add("vm/vue/index.vue.vm"); } diff --git a/ruoyi-generator/src/main/resources/mapper/generator/GenTableMapper.xml b/ruoyi-generator/src/main/resources/mapper/generator/GenTableMapper.xml index c752c5cfb..89a8a61fb 100644 --- a/ruoyi-generator/src/main/resources/mapper/generator/GenTableMapper.xml +++ b/ruoyi-generator/src/main/resources/mapper/generator/GenTableMapper.xml @@ -10,6 +10,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" + @@ -27,7 +28,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" - + @@ -52,7 +53,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" - + select table_id, table_name, table_comment, sub_table_name, sub_table_fk_name, class_name, tpl_category, package_name, module_name, business_name, function_name, function_author, gen_type, gen_path, options, create_by, create_time, update_by, update_time, remark from gen_table @@ -131,32 +132,32 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" AND date_format(create_time,'%y%m%d') <= date_format(#{params.endTime},'%y%m%d') - + - + - + - + - \ No newline at end of file + diff --git a/ruoyi-generator/src/main/resources/vm/java/vo.java.vm b/ruoyi-generator/src/main/resources/vm/java/vo.java.vm index 0746f0cb2..f14b50eed 100644 --- a/ruoyi-generator/src/main/resources/vm/java/vo.java.vm +++ b/ruoyi-generator/src/main/resources/vm/java/vo.java.vm @@ -49,4 +49,22 @@ public class ${ClassName}Vo { #end #end +#foreach ($joinInfo in $joinInfos) +#if(${joinInfo.tableFkGenerateFlag}) + // 补充主表字段 + private ${joinInfo.tableFkColumn.javaType} ${joinInfo.tableFkColumn.javaField}; +#end + // 关联表 ${joinInfo.joinTableLowerClassName} 字段 +#foreach ($showField in $joinInfo.showFields) +#if($showField.showFieldJavaType == 'Date') + @Excel(name = "${showField.showFieldComment}" , width = 30, dateFormat = "yyyy-MM-dd") + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") +#else + @Excel(name = "${showField.showFieldComment}") +#end + private ${showField.showFieldJavaType} ${joinInfo.joinTableLowerClassName}${showField.showFiledUpperName}; +#end + +#end + } diff --git a/ruoyi-generator/src/main/resources/vm/vue/index.vue.vm b/ruoyi-generator/src/main/resources/vm/vue/index.vue.vm index c6b6573e3..8ae24f9a2 100644 --- a/ruoyi-generator/src/main/resources/vm/vue/index.vue.vm +++ b/ruoyi-generator/src/main/resources/vm/vue/index.vue.vm @@ -138,6 +138,16 @@ #elseif($column.list && "" != $javaField) #end +#end +#foreach($joinInfo in $joinInfos) + +#foreach ($showField in $joinInfo.showFields) +#if($showField.showFieldDictType && "" != $showField.showFieldDictType) + +#else + +#end +#end #end - + + +#foreach($joinInfo in $joinInfos) + + + + + + +#end + #foreach($column in $columns) #set($field=$column.javaField) #if($column.insert && !$column.pk) @@ -309,6 +342,10 @@