From 14453f2c10be9788fbd6bb9dd67841e9d65bc6e8 Mon Sep 17 00:00:00 2001 From: "20932067@zju.edu.cn" Date: Fri, 26 Feb 2021 17:21:05 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=9A=E5=8F=98=E5=85=B3=E8=81=94=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hchyun-ui/src/views/tool/gen/editTable.vue | 4 +- hchyun-ui/src/views/tool/gen/genInfoForm.vue | 120 ++++++------ hchyun-ui/src/views/tool/gen/index.vue | 6 +- .../com/hchyun/generator/entity/RelTable.java | 78 +++++--- .../service/impl/GenTableServiceImpl.java | 98 ++++++++-- .../generator/utils/GenStringUtils.java | 4 + .../hchyun/generator/utils/VelocityUtils.java | 62 ++++++- .../utils/constant/GenConstants.java | 4 + .../resources/mapper/generator/RelMapper.xml | 27 +-- .../main/resources/vm/java/controller.java.vm | 4 +- .../src/main/resources/vm/java/entity.java.vm | 59 +++--- ...{ass-entity.java.vm => rel-entity.java.vm} | 11 +- .../src/main/resources/vm/vue/index.vue.vm | 113 +++++++----- .../src/main/resources/vm/xml/mapper.xml.vm | 71 ++++--- .../test/controller/RelAController.java | 174 ++++++++++++++++++ .../java/com/hchyun/test/dao/RelADao.java | 61 ++++++ .../java/com/hchyun/test/entity/RelA.java | 77 ++++++++ .../java/com/hchyun/test/entity/RelB.java | 48 +++++ .../java/com/hchyun/test/entity/RelC.java | 48 +++++ .../com/hchyun/test/service/RelAService.java | 63 +++++++ .../test/service/impl/RelAServiceImpl.java | 153 +++++++++++++++ .../main/resources/mapper/test/RelAMapper.xml | 71 +++++++ 22 files changed, 1125 insertions(+), 231 deletions(-) rename hchyun/hchyun-generator/src/main/resources/vm/java/{ass-entity.java.vm => rel-entity.java.vm} (84%) create mode 100644 hchyun/hchyun-test/src/main/java/com/hchyun/test/controller/RelAController.java create mode 100644 hchyun/hchyun-test/src/main/java/com/hchyun/test/dao/RelADao.java create mode 100644 hchyun/hchyun-test/src/main/java/com/hchyun/test/entity/RelA.java create mode 100644 hchyun/hchyun-test/src/main/java/com/hchyun/test/entity/RelB.java create mode 100644 hchyun/hchyun-test/src/main/java/com/hchyun/test/entity/RelC.java create mode 100644 hchyun/hchyun-test/src/main/java/com/hchyun/test/service/RelAService.java create mode 100644 hchyun/hchyun-test/src/main/java/com/hchyun/test/service/impl/RelAServiceImpl.java create mode 100644 hchyun/hchyun-test/src/main/resources/mapper/test/RelAMapper.xml diff --git a/hchyun-ui/src/views/tool/gen/editTable.vue b/hchyun-ui/src/views/tool/gen/editTable.vue index f53a9c3..f9bca9a 100644 --- a/hchyun-ui/src/views/tool/gen/editTable.vue +++ b/hchyun-ui/src/views/tool/gen/editTable.vue @@ -210,7 +210,7 @@ export default { const relTables = this.$refs.genInfo.relTables; console.log(relTables) for (let i=0;i { diff --git a/hchyun-ui/src/views/tool/gen/genInfoForm.vue b/hchyun-ui/src/views/tool/gen/genInfoForm.vue index 286d71c..c9b8656 100644 --- a/hchyun-ui/src/views/tool/gen/genInfoForm.vue +++ b/hchyun-ui/src/views/tool/gen/genInfoForm.vue @@ -396,13 +396,13 @@ - @@ -636,10 +636,43 @@ export default { }; }, created() { + this.relTables = [] this.tableId = this.$route.params && this.$route.params.tableId; getTableInfos().then(res => { this.tableInfo = res.data }) + getRelColumnInfos(this.tableId).then(res => { + if (res.data.length === 0) { + getRelId().then(res => { + let relTable = { + id: res.data, + tableId: this.tableId, + tableName:null, + relName: null, + relComment: null, + relColumn: null, + tableColumn: null, + queryType: 'left', + sort: 1, + tableColumns: [], + relColumns: [], + } + this.relTables.push(relTable); + }) + } else { + let data = res.data + for (let i=0;i { + data[i].tableColumns = res.data + }) + this.tableList.push(table) + } + this.relTables=data + } + }) }, watch: { 'info.subTableName': function (val) { @@ -651,60 +684,20 @@ export default { this.assCloumns = res.data }) } else if (val.tplCategory === 'rel') { - this.tableList = [] let table = { - tableName: val.tableName, - unionId: this.tableId, - isMain: 1, + tableName: val.tableName } - this.tableList.push(table) - getRelColumnInfos(val.tableId).then(res => { - this.relTables = [] - if (res.data.length === 0) { - getRelId().then(res => { - let relTable = { - id: res.data, - unionId: null, - tableId: this.tableId, - isMain: 2, - relName: null, - relComment: null, - relColumn: null, - tableColumn: null, - queryType: 'left', - sort: 1, - tableColumns: [], - relColumns: [], - } - this.relTables.push(relTable); - }) - } else { - let data = res.data - for (let i=0;i { this.relTables[index].tableColumns = res.data }) - console.log(this.relTables) }, /** 删除关子联表 */ removeRelTable(index) { @@ -716,8 +709,6 @@ export default { getRelColumns(relName, this.relTables[index].id).then(res => { let table = { tableName: relName, - unionId: this.relTables[index].id, - isMain: 2, } this.tableList.push(table) for (let i = 0; i < this.tableInfo.length; i++) { @@ -734,10 +725,8 @@ export default { getRelId().then(res => { let relTable = { id: res.data, - unionId: this.tableId, tableId: this.tableId, - isMain: 1, - tableIndex: null, + tableName:null, relName: null, relComment: null, relColumn: null, @@ -773,9 +762,34 @@ export default { }, /** 选择生成模板触发 */ tplSelectChange(value) { - if (value !== 'sub') { + if (value === 'rel'){ + this.tableList = [] + let table = { + tableName: this.info.tableName, + } + this.tableList.push(table) + getRelId().then(res => { + let relTable = { + id: res.data, + tableId: this.tableId, + tableName:null, + relName: null, + relComment: null, + relColumn: null, + tableColumn: null, + queryType: 'left', + sort: 1, + tableColumns: [], + relColumns: [], + } + this.relTables.push(relTable); + }) + }else if (value !== 'sub') { this.info.subTableName = ''; this.info.subTableFkName = ''; + this.relTables=[] + }else { + this.relTables=[] } }, /** 设置关联外键 */ diff --git a/hchyun-ui/src/views/tool/gen/index.vue b/hchyun-ui/src/views/tool/gen/index.vue index b4f051f..1edb9ab 100644 --- a/hchyun-ui/src/views/tool/gen/index.vue +++ b/hchyun-ui/src/views/tool/gen/index.vue @@ -165,8 +165,8 @@
@@ -304,7 +304,7 @@ export default { }, /** 高亮显示 */ highlightedCode(code, key) { - const vmName = key.substring(key.lastIndexOf("/") + 1, key.indexOf(".vm")); + const vmName = key; var language = vmName.substring(vmName.indexOf(".") + 1, vmName.length); const result = hljs.highlight(language, code || "", true); return result.value || ' '; diff --git a/hchyun/hchyun-generator/src/main/java/com/hchyun/generator/entity/RelTable.java b/hchyun/hchyun-generator/src/main/java/com/hchyun/generator/entity/RelTable.java index 0f89736..50343cc 100644 --- a/hchyun/hchyun-generator/src/main/java/com/hchyun/generator/entity/RelTable.java +++ b/hchyun/hchyun-generator/src/main/java/com/hchyun/generator/entity/RelTable.java @@ -1,7 +1,5 @@ package com.hchyun.generator.entity; -import javax.validation.Valid; -import javax.validation.constraints.NotBlank; import java.util.List; public class RelTable { @@ -9,14 +7,16 @@ public class RelTable { //表id private long id; - //unionid - private long unionId; //主表id private long tableId; - //是否是一级关联查询 - private Integer isMain; //关联子表表名 private String relName; + //子表表名称简写 + private String relAs; + //父表名称 + private String tableName; + //父表名称简写 + private String tableAs; //关联子表描述 private String relComment; //关联子表的字段 @@ -25,6 +25,8 @@ public class RelTable { private String tableColumn; //实体类名称(子表) private String relClass; + //实体小写类名称(子表) + private String relclass; //查询方式 private String queryType; //排序 @@ -34,14 +36,6 @@ public class RelTable { private List relColumns; - public List getRelColumns() { - return relColumns; - } - - public void setRelColumns(List relColumns) { - this.relColumns = relColumns; - } - public long getId() { return id; } @@ -50,14 +44,6 @@ public class RelTable { this.id = id; } - public long getUnionId() { - return unionId; - } - - public void setUnionId(long unionId) { - this.unionId = unionId; - } - public long getTableId() { return tableId; } @@ -66,14 +52,6 @@ public class RelTable { this.tableId = tableId; } - public Integer getIsMain() { - return isMain; - } - - public void setIsMain(Integer isMain) { - this.isMain = isMain; - } - public String getRelName() { return relName; } @@ -82,6 +60,30 @@ public class RelTable { this.relName = relName; } + public String getRelAs() { + return relAs; + } + + public void setRelAs(String relAs) { + this.relAs = relAs; + } + + public String getTableName() { + return tableName; + } + + public void setTableName(String tableName) { + this.tableName = tableName; + } + + public String getTableAs() { + return tableAs; + } + + public void setTableAs(String tableAs) { + this.tableAs = tableAs; + } + public String getRelComment() { return relComment; } @@ -114,6 +116,14 @@ public class RelTable { this.relClass = relClass; } + public String getRelclass() { + return relclass; + } + + public void setRelclass(String relclass) { + this.relclass = relclass; + } + public String getQueryType() { return queryType; } @@ -137,4 +147,12 @@ public class RelTable { public void setCreateBy(long createBy) { this.createBy = createBy; } + + public List getRelColumns() { + return relColumns; + } + + public void setRelColumns(List relColumns) { + this.relColumns = relColumns; + } } diff --git a/hchyun/hchyun-generator/src/main/java/com/hchyun/generator/service/impl/GenTableServiceImpl.java b/hchyun/hchyun-generator/src/main/java/com/hchyun/generator/service/impl/GenTableServiceImpl.java index c64eef5..510c0b5 100644 --- a/hchyun/hchyun-generator/src/main/java/com/hchyun/generator/service/impl/GenTableServiceImpl.java +++ b/hchyun/hchyun-generator/src/main/java/com/hchyun/generator/service/impl/GenTableServiceImpl.java @@ -4,6 +4,7 @@ import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.io.StringWriter; +import java.util.Comparator; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -13,8 +14,7 @@ import java.util.zip.ZipOutputStream; import com.hchyun.generator.dao.AssociatedDao; import com.hchyun.generator.dao.RelDao; -import com.hchyun.generator.entity.AssColumn; -import com.hchyun.generator.entity.RelTable; +import com.hchyun.generator.entity.*; import com.hchyun.generator.service.GenTableService; import com.hchyun.generator.utils.*; import com.hchyun.generator.utils.constant.Constants; @@ -33,8 +33,6 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; -import com.hchyun.generator.entity.GenTable; -import com.hchyun.generator.entity.GenTableColumn; import com.hchyun.generator.dao.GenTableColumnDao; import com.hchyun.generator.dao.GenTableDao; import org.springframework.transaction.interceptor.TransactionAspectSupport; @@ -170,7 +168,11 @@ public class GenTableServiceImpl implements GenTableService { relTables = genTable.getRelTables(); for (int i = 0; i < relTables.size(); i++) { RelTable relTable = relTables.get(i); + relTable.setTableAs(GenStringUtils.sqlFormat(relTable.getTableName())); + relTable.setRelAs(GenStringUtils.sqlFormat(relTable.getRelName())); relTable.setRelClass(GenUtils.convertClassName(relTable.getRelName())); + relTable.setCreateBy(GenSecurityUtils.getUserId()); + relTable.setRelclass(GenStringUtils.uncapitalize(relTable.getRelClass())); relTables.set(i,relTable); } if (relTables != null) { @@ -200,6 +202,15 @@ public class GenTableServiceImpl implements GenTableService { try { genTableDao.deleteGenTableByIds(tableIds); genTableColumnDao.deleteGenTableColumnByIds(tableIds); + for (Long tableId : tableIds) { + List relTables = relDao.relTableByTableId(tableId); + if (relTables.size()>0){ + relDao.deleteRelTableByTableId(tableId); + for (RelTable relTable : relTables) { + relDao.deleteRelColumnByRelId(relTable.getId()); + } + } + } return new GenServerResult<>(true); } catch (RuntimeException e) { TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); @@ -252,8 +263,10 @@ public class GenTableServiceImpl implements GenTableService { // 设置主子表信息 setSubTable(table); } - if (table.getTplCategory().equals(GenConstants.TPL_ASS)) { - table.setAssColumns(associatedDao.selectTableColumnByTableId(tableId)); + if (table.getTplCategory().equals(GenConstants.TPL_REL)) { + List relTables = relDao.relTableByTableId(tableId); + relTables = relTables.stream().sorted(Comparator.comparing(RelTable::getSort)).collect(Collectors.toList()); + table.setRelTables(relTables); } // 设置主键列信息 setPkColumn(table); @@ -265,10 +278,24 @@ public class GenTableServiceImpl implements GenTableService { List templates = VelocityUtils.getTemplateList(table.getTplCategory()); for (String template : templates) { // 渲染模板 + if (template.equals("vm/java/rel-entity.java.vm")){ + List relTables = table.getRelTables(); + for (RelTable relTable : relTables) { + List relColumns = relTable.getRelColumns(); + relColumns = relColumns.stream().sorted(Comparator.comparing(RelColumn::getSort)).collect(Collectors.toList()); + relTable.setRelColumns(relColumns); + context.put("relTable",relTable); + StringWriter stringWriter = new StringWriter(); + Template temp = Velocity.getTemplate(template,Constants.UTF8); + temp.merge(context,stringWriter); + dataMap.put(VelocityUtils.getPreviewName(template,relTable.getRelClass()),stringWriter.toString()); + } + continue; + } StringWriter sw = new StringWriter(); Template tpl = Velocity.getTemplate(template, Constants.UTF8); tpl.merge(context, sw); - dataMap.put(template, sw.toString()); + dataMap.put(VelocityUtils.getPreviewName(template,table.getClassName()), sw.toString()); } return dataMap; } catch (RuntimeException e) { @@ -322,6 +349,26 @@ public class GenTableServiceImpl implements GenTableService { for (String template : templates) { if (!GenStringUtils.containsAny(template, "table.sql.vm", "api.js.vm", "index.vue.vm", "index-tree.vue.vm")) { // 渲染模板 + if (template.equals("vm/java/rel-entity.java.vm")){ + List relTables = table.getRelTables(); + for (RelTable relTable : relTables) { + List relColumns = relTable.getRelColumns(); + relColumns = relColumns.stream().sorted(Comparator.comparing(RelColumn::getSort)).collect(Collectors.toList()); + relTable.setRelColumns(relColumns); + context.put("relTable",relTable); + StringWriter stringWriter = new StringWriter(); + Template temp = Velocity.getTemplate(template,Constants.UTF8); + temp.merge(context,stringWriter); + try { + String path = getGenPath(table, template); + FileUtils.writeStringToFile(new File(path), stringWriter.toString(), CharsetKit.UTF_8); + } catch (IOException e) { + logger.error("渲染模板失败,表名:" + table.getTableName()); + return false; + } + } + continue; + } StringWriter sw = new StringWriter(); Template tpl = Velocity.getTemplate(template, Constants.UTF8); tpl.merge(context, sw); @@ -431,22 +478,17 @@ public class GenTableServiceImpl implements GenTableService { * todo 查询表信息并生成代码 */ private ZipOutputStream generatorCode(String tableName, ZipOutputStream zip) { - try { - - } catch (RuntimeException e) { - logger.error(e.getMessage()); - throw new GenCustomException(GenReturnConstants.OP_ERROR); - } // 查询表信息 GenTable table = genTableDao.selectGenTableByName(tableName); // 设置主子表信息 setSubTable(table); // 设置主键列信息 setPkColumn(table); - if (table.getTplCategory().equals(GenConstants.TPL_ASS)) { - table.setAssColumns(associatedDao.selectTableColumnByTableId(table.getTableId())); + if (table.getTplCategory().equals(GenConstants.TPL_REL)) { + List relTables = relDao.relTableByTableId(table.getTableId()); + relTables = relTables.stream().sorted(Comparator.comparing(RelTable::getSort)).collect(Collectors.toList()); + table.setRelTables(relTables); } - VelocityInitializer.initVelocity(); VelocityContext context = VelocityUtils.prepareContext(table); @@ -454,7 +496,31 @@ public class GenTableServiceImpl implements GenTableService { // 获取模板列表 List templates = VelocityUtils.getTemplateList(table.getTplCategory()); for (String template : templates) { + // 渲染模板 + if (template.equals("vm/java/rel-entity.java.vm")){ + List relTables = table.getRelTables(); + for (RelTable relTable : relTables) { + List relColumns = relTable.getRelColumns(); + relColumns = relColumns.stream().sorted(Comparator.comparing(RelColumn::getSort)).collect(Collectors.toList()); + relTable.setRelColumns(relColumns); + context.put("relTable",relTable); + StringWriter stringWriter = new StringWriter(); + Template temp = Velocity.getTemplate(template,Constants.UTF8); + temp.merge(context,stringWriter); + try { + // 添加到zip + zip.putNextEntry(new ZipEntry(VelocityUtils.getRelFileName(template, table,relTable))); + IOUtils.write(stringWriter.toString(), zip, Constants.UTF8); + IOUtils.closeQuietly(stringWriter); + zip.flush(); + zip.closeEntry(); + } catch (IOException e) { + logger.error("渲染模板失败,表名:" + table.getTableName(), e); + } + } + continue; + } StringWriter sw = new StringWriter(); Template tpl = Velocity.getTemplate(template, Constants.UTF8); tpl.merge(context, sw); diff --git a/hchyun/hchyun-generator/src/main/java/com/hchyun/generator/utils/GenStringUtils.java b/hchyun/hchyun-generator/src/main/java/com/hchyun/generator/utils/GenStringUtils.java index f45954f..095c2b2 100644 --- a/hchyun/hchyun-generator/src/main/java/com/hchyun/generator/utils/GenStringUtils.java +++ b/hchyun/hchyun-generator/src/main/java/com/hchyun/generator/utils/GenStringUtils.java @@ -403,6 +403,10 @@ public class GenStringUtils extends org.apache.commons.lang3.StringUtils { return sqlStr; } + public static String toLoad(String str){ + return str.substring(0,1).toLowerCase()+str.substring(1); + } + @SuppressWarnings("unchecked") public static T cast(Object obj) { return (T) obj; diff --git a/hchyun/hchyun-generator/src/main/java/com/hchyun/generator/utils/VelocityUtils.java b/hchyun/hchyun-generator/src/main/java/com/hchyun/generator/utils/VelocityUtils.java index a6644c8..8b74713 100644 --- a/hchyun/hchyun-generator/src/main/java/com/hchyun/generator/utils/VelocityUtils.java +++ b/hchyun/hchyun-generator/src/main/java/com/hchyun/generator/utils/VelocityUtils.java @@ -4,6 +4,7 @@ import java.util.ArrayList; import java.util.HashSet; import java.util.List; +import com.hchyun.generator.entity.RelTable; import com.hchyun.generator.utils.constant.GenConstants; import org.apache.velocity.VelocityContext; import com.alibaba.fastjson.JSONObject; @@ -69,18 +70,15 @@ public class VelocityUtils { if (GenConstants.TPL_SUB.equals(tplCategory)) { setSubVelocityContext(velocityContext, genTable); } - if (GenConstants.TPL_ASS.equals(tplCategory)) { - setAssVelocityContext(velocityContext, genTable); + if (GenConstants.TPL_REL.equals(tplCategory)) { + setRelVelocityContext(velocityContext, genTable); } return velocityContext; } - public static void setAssVelocityContext(VelocityContext context, GenTable genTable) { - String assClassName = GenUtils.convertClassName(genTable.getAssTableName()); - context.put("assClassName", assClassName); - context.put("assclassName", GenStringUtils.uncapitalize(assClassName)); + public static void setRelVelocityContext(VelocityContext context, GenTable genTable) { + context.put("relTables", genTable.getRelTables()); context.put("tableMapping", GenStringUtils.sqlFormat(genTable.getTableName())); - context.put("assMapping", GenStringUtils.sqlFormat(genTable.getAssTableName())); } public static void setMenuVelocityContext(VelocityContext context, GenTable genTable) { @@ -150,12 +148,56 @@ public class VelocityUtils { } else if (GenConstants.TPL_SUB.equals(tplCategory)) { templates.add("vm/vue/index.vue.vm"); templates.add("vm/java/sub-entity.java.vm"); - } else if (GenConstants.TPL_ASS.equals(tplCategory)) { + } else if (GenConstants.TPL_REL.equals(tplCategory)) { templates.add("vm/vue/index.vue.vm"); - templates.add("vm/java/ass-entity.java.vm"); + templates.add("vm/java/rel-entity.java.vm"); } return templates; } + public static String getRelFileName(String template, GenTable genTable, RelTable relTable){ + + // 文件名称 + String fileName = ""; + // 包路径 + String packageName = genTable.getPackageName(); + // 模块名 + String moduleName = genTable.getModuleName(); + + String javaPath = PROJECT_PATH + "/" + GenStringUtils.replace(packageName, ".", "/"); + fileName = GenStringUtils.format("{}/entity/{}.java", javaPath, relTable.getRelClass()); + return fileName; + } + + public static String getPreviewName(String template, String ClassName) { + String fileName = ""; + if (template.contains("entity.java.vm")) { + fileName = GenStringUtils.format("{}.java", ClassName); + } + if (template.contains("sub-entity.java.vm") && GenStringUtils.equals(GenConstants.TPL_SUB, ClassName)) { + fileName = GenStringUtils.format("{}.java", ClassName); + } else if (template.contains("rel-entity.java.vm")) { + fileName = GenStringUtils.format("{}.java", ClassName); + } else if (template.contains("dao.java.vm")) { + fileName = GenStringUtils.format("{}Dao.java", ClassName); + } else if (template.contains("service.java.vm")) { + fileName = GenStringUtils.format("{}Service.java", ClassName); + } else if (template.contains("serviceImpl.java.vm")) { + fileName = GenStringUtils.format("{}ServiceImpl.java", ClassName); + } else if (template.contains("controller.java.vm")) { + fileName = GenStringUtils.format("{}Controller.java",ClassName); + } else if (template.contains("mapper.xml.vm")) { + fileName = GenStringUtils.format("{}Mapper.xml",ClassName); + } else if (template.contains("table.sql.vm")) { + fileName = ClassName + "menu.sql"; + } else if (template.contains("api.js.vm")) { + fileName = GenStringUtils.format("{}.js", GenStringUtils.toLoad(ClassName)); + } else if (template.contains("index.vue.vm")) { + fileName = "index.vue"; + } else if (template.contains("index-tree.vue.vm")) { + fileName = "index.vue"; + } + return fileName; + } /** * 获取文件名 @@ -194,7 +236,7 @@ public class VelocityUtils { } else if (template.contains("mapper.xml.vm")) { fileName = GenStringUtils.format("{}/{}Mapper.xml", mybatisPath, className); } else if (template.contains("table.sql.vm")) { - fileName = businessName + "GenMenu.sql"; + fileName = businessName + "menu.sql"; } else if (template.contains("api.js.vm")) { fileName = GenStringUtils.format("{}/api/{}/{}.js", vuePath, moduleName, businessName); } else if (template.contains("index.vue.vm")) { diff --git a/hchyun/hchyun-generator/src/main/java/com/hchyun/generator/utils/constant/GenConstants.java b/hchyun/hchyun-generator/src/main/java/com/hchyun/generator/utils/constant/GenConstants.java index 5539c1b..6bd847e 100644 --- a/hchyun/hchyun-generator/src/main/java/com/hchyun/generator/utils/constant/GenConstants.java +++ b/hchyun/hchyun-generator/src/main/java/com/hchyun/generator/utils/constant/GenConstants.java @@ -25,6 +25,10 @@ public class GenConstants { * 关联查询(增删改查) */ public static final String TPL_ASS = "ass"; + /** + * 多表关联查询(增删改查) + */ + public static final String TPL_REL = "rel"; /** * 树编码字段 diff --git a/hchyun/hchyun-generator/src/main/resources/mapper/generator/RelMapper.xml b/hchyun/hchyun-generator/src/main/resources/mapper/generator/RelMapper.xml index e054de3..e7607fa 100644 --- a/hchyun/hchyun-generator/src/main/resources/mapper/generator/RelMapper.xml +++ b/hchyun/hchyun-generator/src/main/resources/mapper/generator/RelMapper.xml @@ -6,14 +6,16 @@ - - + + + + @@ -30,10 +32,10 @@ - + - + @@ -55,17 +57,17 @@ from information_schema.columns where table_schema = (select database()) and table_name = (#{tableName}) - order by sort + order by sort desc - insert into gen_rel_table(id, union_id, table_id, is_main, rel_name, rel_comment, - rel_column, table_column, rel_class, query_type, sort, create_by) + insert into gen_rel_table(id, table_id, rel_name,rel_as,table_name,table_as, rel_comment, + rel_column, table_column, rel_class, relclass, query_type, sort, create_by) values - (#{item.id},#{item.unionId},#{item.tableId},#{item.isMain}, - #{item.relName},#{item.relComment},#{item.relColumn},#{item.tableColumn},#{item.relClass}, - #{item.queryType},#{item.sort},#{item.createBy}) + (#{item.id},#{item.tableId},#{item.relName},#{item.relAs},#{item.tableName}, + #{item.tableAs},#{item.relComment},#{item.relColumn},#{item.tableColumn},#{item.relClass}, + #{item.relclass},#{item.queryType},#{item.sort},#{item.createBy}) @@ -89,11 +91,12 @@ diff --git a/hchyun/hchyun-generator/src/main/resources/vm/java/controller.java.vm b/hchyun/hchyun-generator/src/main/resources/vm/java/controller.java.vm index 5472106..49b81e6 100644 --- a/hchyun/hchyun-generator/src/main/resources/vm/java/controller.java.vm +++ b/hchyun/hchyun-generator/src/main/resources/vm/java/controller.java.vm @@ -32,7 +32,7 @@ import com.hchyun.common.utils.poi.ExcelUtil; #if($table.crud || $table.sub) import com.hchyun.common.core.page.TableDataInfo; #elseif($table.tree) -#elseif($table.tplCategory.equals("ass")) +#elseif($table.tplCategory.equals("rel")) import org.springframework.validation.annotation.Validated; #end @@ -63,7 +63,7 @@ public class ${ClassName}Controller extends HcyBaseController { startPage(${className}.getParams()); ServerResult> genServerResult = ${className}Service.select${ClassName}List(${className}); if (genServerResult.isStart()) { -#if($table.crud || $table.sub || $table.tplCategory.equals("ass")) +#if($table.crud || $table.sub || $tplCategory.equals("rel")) return getDataTable(genServerResult.getData()); #elseif($table.tree) return AjaxResult.success(genServerResult.getData()); diff --git a/hchyun/hchyun-generator/src/main/resources/vm/java/entity.java.vm b/hchyun/hchyun-generator/src/main/resources/vm/java/entity.java.vm index 3da4dd9..e573f65 100644 --- a/hchyun/hchyun-generator/src/main/resources/vm/java/entity.java.vm +++ b/hchyun/hchyun-generator/src/main/resources/vm/java/entity.java.vm @@ -3,8 +3,7 @@ package ${packageName}.entity; #foreach ($import in $importList) import ${import}; #end -import com.hchyun.common.annotation.Excel; -#if($table.crud || $table.sub) +#if($table.crud || $table.sub || $tplCategory.equals("rel")) #set($Entity="BaseEntity") #elseif($table.tree) #set($Entity="TreeEntity") @@ -17,10 +16,10 @@ import ${packageName}.entity.${assClassName}; import ${packageName}.entity.${subClassName}; #end import com.hchyun.common.core.entity.${Entity}; +import com.hchyun.common.annotation.Excel; +import com.hchyun.common.annotation.Excels; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; -import org.apache.commons.lang3.builder.ToStringBuilder; -import org.apache.commons.lang3.builder.ToStringStyle; /** * ${functionName}对象 ${tableName} @@ -34,12 +33,10 @@ import org.apache.commons.lang3.builder.ToStringStyle; #set($Entity="TreeEntity") #end @ApiModel("${functionName}") -public class ${ClassName} extends ${Entity} -{ - private static final long serialVersionUID = 1L; - +public class ${ClassName} extends ${Entity}{ #foreach ($column in $columns) #if(!$table.isSuperColumn($column.javaField)) + /** $column.columnComment */ #if($column.list) #set($parentheseIndex=$column.columnComment.indexOf("(")) @@ -67,17 +64,17 @@ public class ${ClassName} extends ${Entity} @ApiModelProperty("${table.subTable.functionName}信息") private List<${subClassName}> ${subclassName}List; -#elseif($table.tplCategory.equals("ass")) - #set($count = 0) -#foreach($column in $table.assColumns) -#if($column.isList == "1") - #set($count = $count + 1) +#elseif($tplCategory.equals("rel")) +#foreach($table in $relTables) +#foreach($column in $table.relColumns) +#if($column.isList.equals("1")) +#set($count = $count + 1) #end #end #set($targetName = "") #set($indexs = 0) -#foreach($column in $table.assColumns) -#if($column.isList == "1") +#foreach($column in $table.relColumns) +#if($column.isList.equals("1")) #set($parentheseIndex=$column.columnComment.indexOf("(")) #if($parentheseIndex != -1) #set($comment=$column.columnComment.substring(0, $parentheseIndex)) @@ -87,12 +84,16 @@ public class ${ClassName} extends ${Entity} #set($indexs = $indexs + 1) #set($targetName = $targetName + '@Excel(name = "'+$comment+'",targetAttr = "'+$column.javaField+'")') #if($indexs != $count) - #set($targetName = $targetName + ",") +#set($targetName = $targetName + ",") #end #end #end - @Excels({${targetName}})) - private ${assClassName} ${assclassName}; + + /** ${table.relComment}信息 */ + @Excels({${targetName}}) + private ${table.relClass} ${table.relclass}; +#end + #end #foreach ($column in $columns) @@ -120,16 +121,16 @@ public class ${ClassName} extends ${Entity} public void set${subClassName}List(List<${subClassName}> ${subclassName}List){ this.${subclassName}List = ${subclassName}List; } +#elseif($table.tplCategory.equals("rel")) +#foreach($table in $relTables) + public ${table.relClass} get${table.relClass}(){ + return ${table.relclass}; + } + + public void set${table.relClass}(${table.relClass} ${table.relclass}){ + this.${table.relclass} = ${table.relclass}; + } #end -#if($table.tplCategory.equals("ass")) - public ${assClassName} get${assClassName}(){ - return ${assclassName}; - } - - public void set${assClassName}(${assClassName} ${assclassName}){ - this.${assclassName} = ${assclassName}; - } - #end @Override public String toString() { @@ -144,6 +145,10 @@ public class ${ClassName} extends ${Entity} #end #if($table.sub) .append("${subclassName}List", get${subClassName}List()) +#elseif($tplCategory.equals("rel")) +#foreach($table in $relTables) + .append("${table.relclass}",get${table.relClass}()) +#end #end .toString(); } diff --git a/hchyun/hchyun-generator/src/main/resources/vm/java/ass-entity.java.vm b/hchyun/hchyun-generator/src/main/resources/vm/java/rel-entity.java.vm similarity index 84% rename from hchyun/hchyun-generator/src/main/resources/vm/java/ass-entity.java.vm rename to hchyun/hchyun-generator/src/main/resources/vm/java/rel-entity.java.vm index 621a182..5530860 100644 --- a/hchyun/hchyun-generator/src/main/resources/vm/java/ass-entity.java.vm +++ b/hchyun/hchyun-generator/src/main/resources/vm/java/rel-entity.java.vm @@ -1,10 +1,9 @@ package ${packageName}.entity; -#foreach ($import in $subImportList) -import ${import}; -#end import com.hchyun.common.annotation.Excel; import com.hchyun.common.core.entity.BaseEntity; +import org.apache.commons.lang3.builder.ToStringBuilder; +import org.apache.commons.lang3.builder.ToStringStyle; /** * @@ -12,9 +11,9 @@ import com.hchyun.common.core.entity.BaseEntity; * @date ${datetime} */ -public class ${assClassName} extends BaseEntity{ +public class ${relTable.relClass} extends BaseEntity{ -#foreach($column in $table.assColumns) +#foreach($column in $relTable.relColumns) #if(!$table.isSuperColumn($column.javaField)) /** $column.columnComment */ #set($parentheseIndex=$column.columnComment.indexOf("(")) @@ -36,7 +35,7 @@ public class ${assClassName} extends BaseEntity{ #end #end -#foreach ($column in $table.assColumns) +#foreach ($column in $relTable.relColumns) #if(!$table.isSuperColumn($column.javaField)) #if($column.javaField.length() > 2 && $column.javaField.substring(1,2).matches("[A-Z]")) #set($AttrName=$column.javaField) diff --git a/hchyun/hchyun-generator/src/main/resources/vm/vue/index.vue.vm b/hchyun/hchyun-generator/src/main/resources/vm/vue/index.vue.vm index 4a97025..b3fadb0 100644 --- a/hchyun/hchyun-generator/src/main/resources/vm/vue/index.vue.vm +++ b/hchyun/hchyun-generator/src/main/resources/vm/vue/index.vue.vm @@ -63,7 +63,9 @@ #end #end #end -#foreach($column in $table.assColumns) +#if($tplCategory.equals("rel")) +#foreach($table in $relTables) +#foreach($column in $table.relColumns) #if($column.isQuery == '1') #set($dictType=$column.dictType) #set($AttrName=$column.javaField.substring(0,1).toUpperCase() + ${column.javaField.substring(1)}) @@ -76,7 +78,7 @@ #if($column.htmlType == "input") #elseif(($column.htmlType == "select" || $column.htmlType == "radio") && "" != $dictType) - + #elseif(($column.htmlType == "select" || $column.htmlType == "radio") && $dictType) - + #elseif($column.htmlType == "datetime" && $column.queryType != "BETWEEN") @@ -112,7 +114,7 @@ #elseif($column.htmlType == "datetime" && $column.queryType == "BETWEEN") #end #end +#end +#end #end 搜索 @@ -188,22 +192,22 @@ #set($comment=$column.columnComment) #end #if($column.pk) - + #elseif($column.list && $column.htmlType == "datetime") - - - + + + #elseif($column.list && "" != $column.dictType) - + #elseif($column.list && "" != $javaField) - + #end #end - -#if($table.tplCategory.equals("ass")) -#foreach($column in $table.assColumns) +#if($tplCategory.equals("rel")) +#foreach($table in $relTables) +#foreach($column in $table.relColumns) #set($javaField=$column.javaField) #set($parentheseIndex=$column.columnComment.indexOf("(")) #if($parentheseIndex != -1) @@ -212,19 +216,19 @@ #set($comment=$column.columnComment) #end #if($column.isList == "1" && $column.htmlType == "datetime") - - - + + + #elseif($column.isList == "1" && "" != $column.dictType) - + #elseif($column.isList == "1" && "" != $javaField) - + +#end #end #end #end -