From 60acd607a999c23b79863543c39a6ff20fbd930e Mon Sep 17 00:00:00 2001 From: luozuanshi Date: Fri, 23 Apr 2021 18:08:27 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=90=8C=E6=AD=A5?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=BA=93=E6=8E=A5=E5=8F=A3=20=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E5=AD=97=E6=AE=B5=E6=B3=A8=E9=87=8A=E4=BF=A1=E6=81=AF?= =?UTF-8?q?,=E7=B1=BB=E5=9E=8B,=E6=8E=92=E5=BA=8F=E5=90=8C=E6=AD=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../gen/service/GenTableServiceImpl.java | 76 +++++++++++++------ .../mapper/generator/GenTableColumnMapper.xml | 21 ++--- 2 files changed, 64 insertions(+), 33 deletions(-) diff --git a/ruoyi-modules/ruoyi-gen/src/main/java/com/ruoyi/gen/service/GenTableServiceImpl.java b/ruoyi-modules/ruoyi-gen/src/main/java/com/ruoyi/gen/service/GenTableServiceImpl.java index 1e656200..e5c904b4 100644 --- a/ruoyi-modules/ruoyi-gen/src/main/java/com/ruoyi/gen/service/GenTableServiceImpl.java +++ b/ruoyi-modules/ruoyi-gen/src/main/java/com/ruoyi/gen/service/GenTableServiceImpl.java @@ -7,6 +7,7 @@ import java.io.StringWriter; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.stream.Collectors; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; @@ -38,7 +39,7 @@ import com.ruoyi.gen.util.VelocityUtils; /** * 业务 服务层实现 - * + * * @author ruoyi */ @Service @@ -54,7 +55,7 @@ public class GenTableServiceImpl implements IGenTableService /** * 查询业务信息 - * + * * @param id 业务ID * @return 业务信息 */ @@ -68,7 +69,7 @@ public class GenTableServiceImpl implements IGenTableService /** * 查询业务列表 - * + * * @param genTable 业务信息 * @return 业务集合 */ @@ -80,7 +81,7 @@ public class GenTableServiceImpl implements IGenTableService /** * 查询据库列表 - * + * * @param genTable 业务信息 * @return 数据库表集合 */ @@ -92,7 +93,7 @@ public class GenTableServiceImpl implements IGenTableService /** * 查询据库列表 - * + * * @param tableNames 表名称组 * @return 数据库表集合 */ @@ -104,7 +105,7 @@ public class GenTableServiceImpl implements IGenTableService /** * 查询所有表信息 - * + * * @return 表信息集合 */ @Override @@ -115,7 +116,7 @@ public class GenTableServiceImpl implements IGenTableService /** * 修改业务 - * + * * @param genTable 业务信息 * @return 结果 */ @@ -137,7 +138,7 @@ public class GenTableServiceImpl implements IGenTableService /** * 删除业务对象 - * + * * @param tableIds 需要删除的数据ID * @return 结果 */ @@ -151,7 +152,7 @@ public class GenTableServiceImpl implements IGenTableService /** * 导入表结构 - * + * * @param tableList 导入表列表 */ @Override @@ -186,7 +187,7 @@ public class GenTableServiceImpl implements IGenTableService /** * 预览代码 - * + * * @param tableId 表编号 * @return 预览数据列表 */ @@ -219,7 +220,7 @@ public class GenTableServiceImpl implements IGenTableService /** * 生成代码(下载方式) - * + * * @param tableName 表名称 * @return 数据 */ @@ -235,7 +236,7 @@ public class GenTableServiceImpl implements IGenTableService /** * 生成代码(自定义路径) - * + * * @param tableName 表名称 */ @Override @@ -277,7 +278,7 @@ public class GenTableServiceImpl implements IGenTableService /** * 同步数据库 - * + * * @param tableName 表名称 */ @Override @@ -286,7 +287,6 @@ public class GenTableServiceImpl implements IGenTableService { GenTable table = genTableMapper.selectGenTableByName(tableName); List tableColumns = table.getColumns(); - List tableColumnNames = tableColumns.stream().map(GenTableColumn::getColumnName).collect(Collectors.toList()); List dbTableColumns = genTableColumnMapper.selectDbTableColumnsByName(tableName); if (StringUtils.isEmpty(dbTableColumns)) @@ -295,11 +295,20 @@ public class GenTableServiceImpl implements IGenTableService } List dbTableColumnNames = dbTableColumns.stream().map(GenTableColumn::getColumnName).collect(Collectors.toList()); + Map tableColumnsMap = tableColumns.stream().collect(Collectors.toMap(GenTableColumn::getColumnName, GenTableColumn->GenTableColumn, (key1,key2)->key2)); + dbTableColumns.forEach(column -> { - if (!tableColumnNames.contains(column.getColumnName())) + if (!equalColumInfo(tableColumnsMap,column)) { GenUtils.initColumnField(column, table); - genTableColumnMapper.insertGenTableColumn(column); + + GenTableColumn tableColumn = tableColumnsMap.get(column.getColumnName()); + if (tableColumn!=null) { + column.setColumnId(tableColumn.getColumnId()); + genTableColumnMapper.updateGenTableColumn(column); + }else { + genTableColumnMapper.insertGenTableColumn(column); + } } }); @@ -308,11 +317,32 @@ public class GenTableServiceImpl implements IGenTableService { genTableColumnMapper.deleteGenTableColumns(delColumns); } + + } + + /** + * 比较数据库列信息 + * @param tableColumnsMap + * @param column + * @return ture 数据库字段信息与information_schema 的字段内容一致 false 与 information_schema 的字段内容不一致 + */ + private boolean equalColumInfo(Map tableColumnsMap, GenTableColumn column) { + String columnName = column.getColumnName(); + GenTableColumn tableColumns = tableColumnsMap.get(columnName); + if (tableColumns==null) { + return false; + } + return Objects.equals(column.getColumnName(), tableColumns.getColumnName()) && + Objects.equals(column.getColumnComment(), tableColumns.getColumnComment()) && + Objects.equals(column.getColumnType(), tableColumns.getColumnType()) && + Objects.equals(column.getSort(), tableColumns.getSort()) && + Objects.equals(column.getIsPk(), tableColumns.getIsPk()) && + Objects.equals(column.getIsIncrement(), tableColumns.getIsIncrement()); } /** * 批量生成代码(下载方式) - * + * * @param tableNames 表数组 * @return 数据 */ @@ -371,7 +401,7 @@ public class GenTableServiceImpl implements IGenTableService /** * 修改保存参数校验 - * + * * @param genTable 业务信息 */ @Override @@ -409,7 +439,7 @@ public class GenTableServiceImpl implements IGenTableService /** * 设置主键列信息 - * + * * @param table 业务表信息 */ public void setPkColumn(GenTable table) @@ -444,7 +474,7 @@ public class GenTableServiceImpl implements IGenTableService } /** * 设置主子表信息 - * + * * @param table 业务表信息 */ public void setSubTable(GenTable table) @@ -458,7 +488,7 @@ public class GenTableServiceImpl implements IGenTableService /** * 设置代码生成其他选项值 - * + * * @param genTable 设置后的生成对象 */ public void setTableFromOptions(GenTable genTable) @@ -471,7 +501,7 @@ public class GenTableServiceImpl implements IGenTableService String treeName = paramsObj.getString(GenConstants.TREE_NAME); String parentMenuId = paramsObj.getString(GenConstants.PARENT_MENU_ID); String parentMenuName = paramsObj.getString(GenConstants.PARENT_MENU_NAME); - + genTable.setTreeCode(treeCode); genTable.setTreeParentCode(treeParentCode); genTable.setTreeName(treeName); @@ -482,7 +512,7 @@ public class GenTableServiceImpl implements IGenTableService /** * 获取代码生成地址 - * + * * @param table 业务表信息 * @param template 模板文件路径 * @return 生成地址 diff --git a/ruoyi-modules/ruoyi-gen/src/main/resources/mapper/generator/GenTableColumnMapper.xml b/ruoyi-modules/ruoyi-gen/src/main/resources/mapper/generator/GenTableColumnMapper.xml index 83565467..47738ba7 100644 --- a/ruoyi-modules/ruoyi-gen/src/main/resources/mapper/generator/GenTableColumnMapper.xml +++ b/ruoyi-modules/ruoyi-gen/src/main/resources/mapper/generator/GenTableColumnMapper.xml @@ -3,7 +3,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> - + @@ -28,23 +28,23 @@ 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 - + - + - + insert into gen_table_column ( table_id, @@ -88,11 +88,12 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" sysdate() ) - + update gen_table_column column_comment = #{columnComment}, + column_type = #{columnType}, java_type = #{javaType}, java_field = #{javaField}, is_insert = #{isInsert}, @@ -111,17 +112,17 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" - delete from gen_table_column where table_id in + delete from gen_table_column where table_id in #{tableId} - + - delete from gen_table_column where column_id in + delete from gen_table_column where column_id in #{item.columnId} - \ No newline at end of file + From bdb9339d9723c39014d3b63a9e2c579ae09c5a0d Mon Sep 17 00:00:00 2001 From: luozuanshi <59036386@qq.com> Date: Sun, 25 Apr 2021 15:49:01 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20=E5=AE=8C=E5=85=A8?= =?UTF-8?q?=E6=9B=B4=E6=96=B0sql=20=E4=B8=BAnull=E7=9A=84=E5=AD=97?= =?UTF-8?q?=E6=AE=B5=E4=B9=9F=E6=9B=B4=E6=96=B0=E6=9B=BF=E6=8D=A2=E6=8E=89?= =?UTF-8?q?=E4=BA=86=E5=8E=9F=E6=9C=89=E7=9A=84""=E5=AD=97=E6=AE=B5,=20?= =?UTF-8?q?=E5=AF=BC=E8=87=B4=E6=9B=B4=E6=96=B0=E8=BF=87=E7=9A=84=E5=AD=97?= =?UTF-8?q?=E6=AE=B5=E9=83=BD=E8=AE=A4=E4=B8=BA=E9=85=8D=E7=BD=AE=E4=BA=86?= =?UTF-8?q?=E5=AD=97=E5=85=B8=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mapper/generator/GenTableColumnMapper.xml | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/ruoyi-modules/ruoyi-gen/src/main/resources/mapper/generator/GenTableColumnMapper.xml b/ruoyi-modules/ruoyi-gen/src/main/resources/mapper/generator/GenTableColumnMapper.xml index 47738ba7..9dd4612f 100644 --- a/ruoyi-modules/ruoyi-gen/src/main/resources/mapper/generator/GenTableColumnMapper.xml +++ b/ruoyi-modules/ruoyi-gen/src/main/resources/mapper/generator/GenTableColumnMapper.xml @@ -92,20 +92,20 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" update gen_table_column - column_comment = #{columnComment}, - column_type = #{columnType}, - java_type = #{javaType}, - java_field = #{javaField}, - is_insert = #{isInsert}, - is_edit = #{isEdit}, - is_list = #{isList}, - is_query = #{isQuery}, - is_required = #{isRequired}, - query_type = #{queryType}, - html_type = #{htmlType}, - dict_type = #{dictType}, - sort = #{sort}, - update_by = #{updateBy}, + column_type = #{columnType}, + column_type = #{columnType}, + java_type = #{javaType}, + java_field = #{javaField}, + is_insert = #{isInsert}, + is_edit = #{isEdit}, + is_list = #{isList}, + is_query = #{isQuery}, + is_required = #{isRequired}, + query_type = #{queryType}, + html_type = #{htmlType}, + dict_type = #{dictType}, + sort = #{sort}, + update_by = #{updateBy}, update_time = sysdate() where column_id = #{columnId} From 96bc882cfdd099da6069089a7506cfe7ff72d704 Mon Sep 17 00:00:00 2001 From: luozuanshi <59036386@qq.com> Date: Sun, 25 Apr 2021 15:49:01 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20=E5=AE=8C=E5=85=A8?= =?UTF-8?q?=E6=9B=B4=E6=96=B0sql=20=E4=B8=BAnull=E7=9A=84=E5=AD=97?= =?UTF-8?q?=E6=AE=B5=E4=B9=9F=E6=9B=B4=E6=96=B0=E6=9B=BF=E6=8D=A2=E6=8E=89?= =?UTF-8?q?=E4=BA=86=E5=8E=9F=E6=9C=89=E7=9A=84""=E5=AD=97=E6=AE=B5,=20?= =?UTF-8?q?=E5=AF=BC=E8=87=B4=E6=9B=B4=E6=96=B0=E8=BF=87=E7=9A=84=E5=AD=97?= =?UTF-8?q?=E6=AE=B5=E9=83=BD=E8=AE=A4=E4=B8=BA=E9=85=8D=E7=BD=AE=E4=BA=86?= =?UTF-8?q?=E5=AD=97=E5=85=B8=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mapper/generator/GenTableColumnMapper.xml | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/ruoyi-modules/ruoyi-gen/src/main/resources/mapper/generator/GenTableColumnMapper.xml b/ruoyi-modules/ruoyi-gen/src/main/resources/mapper/generator/GenTableColumnMapper.xml index 47738ba7..f021bb04 100644 --- a/ruoyi-modules/ruoyi-gen/src/main/resources/mapper/generator/GenTableColumnMapper.xml +++ b/ruoyi-modules/ruoyi-gen/src/main/resources/mapper/generator/GenTableColumnMapper.xml @@ -92,20 +92,20 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" update gen_table_column - column_comment = #{columnComment}, - column_type = #{columnType}, - java_type = #{javaType}, - java_field = #{javaField}, - is_insert = #{isInsert}, - is_edit = #{isEdit}, - is_list = #{isList}, - is_query = #{isQuery}, - is_required = #{isRequired}, - query_type = #{queryType}, - html_type = #{htmlType}, - dict_type = #{dictType}, - sort = #{sort}, - update_by = #{updateBy}, + column_comment = #{columnComment}, + column_type = #{columnType}, + java_type = #{javaType}, + java_field = #{javaField}, + is_insert = #{isInsert}, + is_edit = #{isEdit}, + is_list = #{isList}, + is_query = #{isQuery}, + is_required = #{isRequired}, + query_type = #{queryType}, + html_type = #{htmlType}, + dict_type = #{dictType}, + sort = #{sort}, + update_by = #{updateBy}, update_time = sysdate() where column_id = #{columnId} From 6875a4e1a69da5124523f42eb6968698c11dbccf Mon Sep 17 00:00:00 2001 From: luozuanshi <59036386@qq.com> Date: Wed, 28 Apr 2021 10:17:24 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20=E7=BC=96=E8=BE=91?= =?UTF-8?q?=E4=BF=9D=E5=AD=98=20=E6=8F=92=E5=85=A5=20=E7=BC=96=E8=BE=91=20?= =?UTF-8?q?=E5=88=97=E8=A1=A8=20=E6=9F=A5=E8=AF=A2=20=E5=8F=96=E6=B6=88?= =?UTF-8?q?=E5=8B=BE=E9=80=89=E4=BF=9D=E5=AD=98=E6=B2=A1=E7=94=9F=E6=95=88?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../resources/mapper/generator/GenTableColumnMapper.xml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ruoyi-modules/ruoyi-gen/src/main/resources/mapper/generator/GenTableColumnMapper.xml b/ruoyi-modules/ruoyi-gen/src/main/resources/mapper/generator/GenTableColumnMapper.xml index f021bb04..c7202643 100644 --- a/ruoyi-modules/ruoyi-gen/src/main/resources/mapper/generator/GenTableColumnMapper.xml +++ b/ruoyi-modules/ruoyi-gen/src/main/resources/mapper/generator/GenTableColumnMapper.xml @@ -96,10 +96,10 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" column_type = #{columnType}, java_type = #{javaType}, java_field = #{javaField}, - is_insert = #{isInsert}, - is_edit = #{isEdit}, - is_list = #{isList}, - is_query = #{isQuery}, + is_insert = #{isInsert}, + is_edit = #{isEdit}, + is_list = #{isList}, + is_query = #{isQuery}, is_required = #{isRequired}, query_type = #{queryType}, html_type = #{htmlType},