From 37f6b629752c9b555251ec6d317e0ee6af48670a Mon Sep 17 00:00:00 2001 From: Parker Date: Thu, 19 Nov 2020 19:57:09 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E7=94=9F=E6=88=90=E5=99=A8?= =?UTF-8?q?=E5=AE=8C=E6=88=90=2050%=20=EF=BC=8C=20=E5=8F=AF=E7=94=9F?= =?UTF-8?q?=E6=88=90=E6=95=B0=E6=8D=AE=E5=BA=93=E8=A1=A8=EF=BC=8C=20?= =?UTF-8?q?=E5=8F=AF=E5=AF=BC=E5=85=A5=E6=95=B0=E6=8D=AE=E5=BA=93=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/org/opsli/common/utils/Props.java | 30 +++++ .../opsli/core/creater/msg/CreaterMsg.java | 6 + .../strategy/sync/MySQLSyncBuilder.java | 20 ++- .../service/impl/TableColumnServiceImpl.java | 2 +- .../creater/importable/ImportTableUtil.java | 122 ++++++++++++++++++ .../creater/importable/constants/DbType.java | 19 +++ .../importable/entity/DatabaseColumn.java | 64 +++++++++ .../importable/entity/DatabaseTable.java | 44 +++++++ .../mapper/MySQLDatabaseTableMapper.java | 48 +++++++ .../mapper/xml/MySQLDatabaseTableMapper.xml | 40 ++++++ .../service/DatabaseTableService.java | 60 +++++++++ .../MySQLDatabaseTableServiceImpl.java | 116 +++++++++++++++++ .../modulars/creater/table/api/TableApi.java | 14 ++ .../creater/table/mapper/TableMapper.java | 8 ++ .../creater/table/mapper/xml/TableMapper.xml | 7 + .../creater/table/service/ITableService.java | 14 ++ .../table/service/impl/TableServiceImpl.java | 75 +++++++++-- .../table/web/TableRestController.java | 22 ++++ .../src/main/resources/creater.yaml | 4 + 19 files changed, 699 insertions(+), 16 deletions(-) create mode 100644 opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/modulars/creater/importable/ImportTableUtil.java create mode 100644 opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/modulars/creater/importable/constants/DbType.java create mode 100644 opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/modulars/creater/importable/entity/DatabaseColumn.java create mode 100644 opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/modulars/creater/importable/entity/DatabaseTable.java create mode 100644 opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/modulars/creater/importable/mapper/MySQLDatabaseTableMapper.java create mode 100644 opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/modulars/creater/importable/mapper/xml/MySQLDatabaseTableMapper.xml create mode 100644 opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/modulars/creater/importable/service/DatabaseTableService.java create mode 100644 opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/modulars/creater/importable/service/MySQLDatabaseTableServiceImpl.java diff --git a/opsli-base-support/opsli-common/src/main/java/org/opsli/common/utils/Props.java b/opsli-base-support/opsli-common/src/main/java/org/opsli/common/utils/Props.java index a29b4c1..946e071 100644 --- a/opsli-base-support/opsli-common/src/main/java/org/opsli/common/utils/Props.java +++ b/opsli-base-support/opsli-common/src/main/java/org/opsli/common/utils/Props.java @@ -21,6 +21,7 @@ import org.yaml.snakeyaml.Yaml; import java.io.InputStream; import java.util.LinkedHashMap; +import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -162,6 +163,7 @@ public class Props { Object obj = this.getObject(keys); if(obj != null){ + def = ""; def = Convert.convert(def.getClass(), obj); } return def; @@ -339,6 +341,34 @@ public class Props { return def; } + /** + * 获得 布尔类型对象 + * @return + */ + public List getList(String key){ + return this.getList(key, null); + } + + /** + * 获得 布尔类型对象 + * @return + */ + public List getList(String key, List def){ + if(key == null || "".equals(key) ){ + return def; + } + // key 集合 + String[] keys = key.split("\\."); + + // 获得对象 + Object obj = this.getObject(keys); + + if(obj != null){ + def = Convert.toList(String.class, obj); + } + return def; + } + /** * 获得对象 diff --git a/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/core/creater/msg/CreaterMsg.java b/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/core/creater/msg/CreaterMsg.java index 20fda14..eea080f 100644 --- a/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/core/creater/msg/CreaterMsg.java +++ b/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/core/creater/msg/CreaterMsg.java @@ -42,6 +42,12 @@ public enum CreaterMsg implements BaseMsg { EXCEPTION_SYNC_NULL(50100,"同步表失败,暂无该表"), EXCEPTION_SYNC_CORE(50101,"系统核心关键表不允许同步"), + /** + * 导入 + */ + EXCEPTION_IMPORT_NULL(50120,"未选中表,无法导入"), + EXCEPTION_IMPORT_TABLE_NULL(50121,"暂无{}该表"), + ; private int code; diff --git a/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/core/creater/strategy/sync/MySQLSyncBuilder.java b/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/core/creater/strategy/sync/MySQLSyncBuilder.java index bcb8c1c..a7f29bd 100644 --- a/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/core/creater/strategy/sync/MySQLSyncBuilder.java +++ b/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/core/creater/strategy/sync/MySQLSyncBuilder.java @@ -1,7 +1,9 @@ package org.opsli.core.creater.strategy.sync; import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.builder.ToStringBuilder; import org.opsli.common.exception.ServiceException; +import org.opsli.common.utils.Props; import org.opsli.core.creater.msg.CreaterMsg; import org.opsli.core.creater.strategy.sync.mysql.entity.FieldTypeAttribute; import org.opsli.core.creater.strategy.sync.mysql.enums.MySQLSyncColumnType; @@ -38,6 +40,13 @@ public class MySQLSyncBuilder implements SyncStrategy { private static final char YES = '1'; /** 否 */ private static final char NO = '0'; + /** 排除表 */ + private static final List EXCLUDE_TABLES; + + static { + Props props = new Props("creater.yaml"); + EXCLUDE_TABLES = props.getList("opsli.exclude-tables"); + } @Autowired(required = false) private SQLActuator sqlActuator; @@ -66,9 +75,7 @@ public class MySQLSyncBuilder implements SyncStrategy { } // 排查该表 是否是 在排除外的表, 如果是则不允许同步 - List excludeTables = new ArrayList<>(); - excludeTables.add("creater_table"); - if(excludeTables.contains(currTable.getOldTableName()) || excludeTables.contains(currTable.getTableName())){ + if(EXCLUDE_TABLES.contains(currTable.getOldTableName()) || EXCLUDE_TABLES.contains(currTable.getTableName())){ // 同步表失败 系统核心关键表不允许同步 throw new ServiceException(CreaterMsg.EXCEPTION_SYNC_CORE); } @@ -123,10 +130,13 @@ public class MySQLSyncBuilder implements SyncStrategy { if(fieldAttr != null){ // 字段有长度 if(fieldAttr.isIzLength()){ - str.append("(").append(tmp.getFieldLength()); + Integer len = tmp.getFieldLength(); + str.append("("); // 字段有精度 if(fieldAttr.isIzPrecision()){ - str.append(",").append(tmp.getFieldPrecision()); + str.append( len + tmp.getFieldPrecision() ).append(",").append(tmp.getFieldPrecision()); + } else { + str.append(len); } str.append(")"); } diff --git a/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/modulars/creater/column/service/impl/TableColumnServiceImpl.java b/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/modulars/creater/column/service/impl/TableColumnServiceImpl.java index 37efb86..ef7e6fa 100644 --- a/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/modulars/creater/column/service/impl/TableColumnServiceImpl.java +++ b/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/modulars/creater/column/service/impl/TableColumnServiceImpl.java @@ -160,7 +160,7 @@ public class TableColumnServiceImpl extends CrudServiceImpl queryBuilder = new GenQueryBuilder<>(); QueryWrapper wrapper = queryBuilder.build(); - wrapper.eq("tableId", tableId); + wrapper.eq("table_id", tableId); super.remove(wrapper); } } diff --git a/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/modulars/creater/importable/ImportTableUtil.java b/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/modulars/creater/importable/ImportTableUtil.java new file mode 100644 index 0000000..ac9ebe7 --- /dev/null +++ b/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/modulars/creater/importable/ImportTableUtil.java @@ -0,0 +1,122 @@ +/** + * Copyright 2020 OPSLI 快速开发平台 https://www.opsli.com + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package org.opsli.modulars.creater.importable; + +import cn.hutool.core.util.ClassUtil; +import lombok.extern.slf4j.Slf4j; +import org.opsli.common.utils.Props; +import org.opsli.core.creater.strategy.sync.SyncStrategy; +import org.opsli.core.utils.SpringContextHolder; +import org.opsli.modulars.creater.importable.entity.DatabaseColumn; +import org.opsli.modulars.creater.importable.entity.DatabaseTable; +import org.opsli.modulars.creater.importable.service.DatabaseTableService; +import org.opsli.modulars.creater.table.wrapper.CreaterTableAndColumnModel; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import java.lang.reflect.Modifier; +import java.util.List; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; + +/** + * @BelongsProject: opsli-boot + * @Author: Parker + * @CreateTime: 2020-09-15 14:50 + * @Description: 数据库同步策略 工具类 + * + */ +@Slf4j +@Configuration +public class ImportTableUtil{ + + /** 数据库名 */ + public static final String DB_NAME; + /** 数据库类型 */ + public static final String DB_TYPE; + /** 处理方法集合 */ + private static final ConcurrentMap HANDLER_MAP = new ConcurrentHashMap<>(); + + static { + Props props = new Props("creater.yaml"); + DB_NAME = props.getStr("opsli.db-name","opsli-boot"); + DB_TYPE = props.getStr("opsli.db-type"); + } + + /** + * 获得当前数据库中表 + * @return + */ + public static List findTables() { + DatabaseTableService databaseTableService = HANDLER_MAP.get(DB_TYPE); + if(databaseTableService == null){ + return null; + } + return databaseTableService.findTables(DB_NAME); + } + + /** + * 获得当前数据库中表 + * @param tableName + * @return + */ + public static List findTables(String tableName) { + DatabaseTableService databaseTableService = HANDLER_MAP.get(DB_TYPE); + if(databaseTableService == null){ + return null; + } + return databaseTableService.findTables(DB_NAME, tableName); + } + + /** + * 获得表字段 + * @param tableName + * @return + */ + public static List findColumns(String tableName) { + DatabaseTableService databaseTableService = HANDLER_MAP.get(DB_TYPE); + if(databaseTableService == null){ + return null; + } + return databaseTableService.findColumns(DB_NAME, tableName); + } + + + // ==================================== + + + @Bean + public void initImportTable(){ + + // 拿到state包下 实现了 SystemEventState 接口的,所有子类 + Set> clazzSet = ClassUtil.scanPackageBySuper(DatabaseTableService.class.getPackage().getName() + , DatabaseTableService.class + ); + + for (Class aClass : clazzSet) { + // 位运算 去除抽象类 + if((aClass.getModifiers() & Modifier.ABSTRACT) != 0){ + continue; + } + + DatabaseTableService handler = (DatabaseTableService) SpringContextHolder.getBean(aClass); + // 加入集合 + HANDLER_MAP.put(handler.getType(),handler); + } + } + +} diff --git a/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/modulars/creater/importable/constants/DbType.java b/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/modulars/creater/importable/constants/DbType.java new file mode 100644 index 0000000..a877d04 --- /dev/null +++ b/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/modulars/creater/importable/constants/DbType.java @@ -0,0 +1,19 @@ +package org.opsli.modulars.creater.importable.constants; + +/** + * @BelongsProject: opsli-boot + * @BelongsPackage: org.opsli.modulars.creater.importable.constants + * @Author: Parker + * @CreateTime: 2020-11-19 15:27 + * @Description: 数据库类型 + */ +public interface DbType { + + String DB_MYSQL = "mysql"; + + String DB_ORACLE = "oracle"; + + String DB_DB2 = "db2"; + + String DB_SQLSERVER = "sqlserver"; +} diff --git a/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/modulars/creater/importable/entity/DatabaseColumn.java b/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/modulars/creater/importable/entity/DatabaseColumn.java new file mode 100644 index 0000000..3b5bfff --- /dev/null +++ b/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/modulars/creater/importable/entity/DatabaseColumn.java @@ -0,0 +1,64 @@ +/** + * Copyright 2020 OPSLI 快速开发平台 https://www.opsli.com + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package org.opsli.modulars.creater.importable.entity; + +import lombok.Data; +import lombok.EqualsAndHashCode; + +/** + * @BelongsProject: opsli-boot + * @Author: Parker + * @CreateTime: 2020-11-15 17:33 + * @Description: 代码生成器 - 数据库表字段 + */ +@Data +@EqualsAndHashCode(callSuper = false) +public class DatabaseColumn { + + + /** 数据库 */ + private String dbName; + + /** 表名称 */ + private String tableName; + + /** 字段名称 */ + private String columnName; + + /** 字段类型 */ + private String columnType; + + /** 字段长度 */ + private Integer columnLength; + + /** 字段精度 = 位数 */ + private Integer columnPrecision; + + /** 字段位数 = 精度 */ + private Integer columnScale; + + /** 字段描述 */ + private String columnComment; + + /** 是否主键 */ + private Character izPk; + + /** 是否可为空 */ + private Character izNull; + + // ======================================== + +} diff --git a/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/modulars/creater/importable/entity/DatabaseTable.java b/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/modulars/creater/importable/entity/DatabaseTable.java new file mode 100644 index 0000000..4eac933 --- /dev/null +++ b/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/modulars/creater/importable/entity/DatabaseTable.java @@ -0,0 +1,44 @@ +/** + * Copyright 2020 OPSLI 快速开发平台 https://www.opsli.com + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package org.opsli.modulars.creater.importable.entity; + +import lombok.Data; +import lombok.EqualsAndHashCode; + +/** + * @BelongsProject: opsli-boot + * @Author: Parker + * @CreateTime: 2020-11-15 17:33 + * @Description: 代码生成器 - 数据库表 + */ +@Data +@EqualsAndHashCode(callSuper = false) +public class DatabaseTable { + + + /** 数据库 */ + private String dbName; + + /** 表名称 */ + private String tableName; + + /** 描述 */ + private String tableComments; + + + // ======================================== + +} diff --git a/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/modulars/creater/importable/mapper/MySQLDatabaseTableMapper.java b/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/modulars/creater/importable/mapper/MySQLDatabaseTableMapper.java new file mode 100644 index 0000000..b2182bb --- /dev/null +++ b/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/modulars/creater/importable/mapper/MySQLDatabaseTableMapper.java @@ -0,0 +1,48 @@ +/** + * Copyright 2020 OPSLI 快速开发平台 https://www.opsli.com + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package org.opsli.modulars.creater.importable.mapper; + +import org.apache.ibatis.annotations.Mapper; +import org.opsli.modulars.creater.importable.entity.DatabaseColumn; +import org.opsli.modulars.creater.importable.entity.DatabaseTable; + +import java.util.List; + + +/** + * @BelongsProject: opsli-boot + * @Author: Parker + * @CreateTime: 2020-09-17 13:01 + * @Description: 代码生成器 - 数据库表 Mapper + */ +@Mapper +public interface MySQLDatabaseTableMapper { + + /** + * 获得当前库中 所有表 + * @param table + * @return + */ + List findTables(DatabaseTable table); + + /** + * 获得当前表中 所有字段 + * @param column + * @return + */ + List findColumns(DatabaseColumn column); + +} diff --git a/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/modulars/creater/importable/mapper/xml/MySQLDatabaseTableMapper.xml b/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/modulars/creater/importable/mapper/xml/MySQLDatabaseTableMapper.xml new file mode 100644 index 0000000..46690ff --- /dev/null +++ b/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/modulars/creater/importable/mapper/xml/MySQLDatabaseTableMapper.xml @@ -0,0 +1,40 @@ + + + + + + + + + + diff --git a/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/modulars/creater/importable/service/DatabaseTableService.java b/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/modulars/creater/importable/service/DatabaseTableService.java new file mode 100644 index 0000000..c1d8062 --- /dev/null +++ b/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/modulars/creater/importable/service/DatabaseTableService.java @@ -0,0 +1,60 @@ +/** + * Copyright 2020 OPSLI 快速开发平台 https://www.opsli.com + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package org.opsli.modulars.creater.importable.service; + +import org.opsli.modulars.creater.importable.entity.DatabaseColumn; +import org.opsli.modulars.creater.importable.entity.DatabaseTable; + +import java.util.List; + + +/** + * @BelongsProject: opsli-boot + * @Author: Parker + * @CreateTime: 2020-09-17 13:07 + * @Description: 代码生成器 - 数据库表 + */ +public interface DatabaseTableService { + + /** + * 获得类型 + * @return + */ + String getType(); + + /** + * 获得当前库中 所有表 + * @param dbName + * @return + */ + List findTables(String dbName); + + /** + * 获得当前库中 所有表 + * @param dbName + * @return + */ + List findTables(String dbName, String tableName); + + /** + * 获得当前表中 所有字段 + * @param dbName + * @param tableName + * @return + */ + List findColumns(String dbName, String tableName); + +} diff --git a/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/modulars/creater/importable/service/MySQLDatabaseTableServiceImpl.java b/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/modulars/creater/importable/service/MySQLDatabaseTableServiceImpl.java new file mode 100644 index 0000000..491adc2 --- /dev/null +++ b/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/modulars/creater/importable/service/MySQLDatabaseTableServiceImpl.java @@ -0,0 +1,116 @@ +/** + * Copyright 2020 OPSLI 快速开发平台 https://www.opsli.com + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package org.opsli.modulars.creater.importable.service; + +import org.apache.commons.lang3.StringUtils; +import org.opsli.common.utils.Props; +import org.opsli.modulars.creater.importable.constants.DbType; +import org.opsli.modulars.creater.importable.entity.DatabaseColumn; +import org.opsli.modulars.creater.importable.entity.DatabaseTable; +import org.opsli.modulars.creater.importable.mapper.MySQLDatabaseTableMapper; +import org.opsli.modulars.creater.table.service.ITableService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; + + +/** + * @BelongsProject: opsli-boot + * @Author: Parker + * @CreateTime: 2020-09-16 17:34 + * @Description: 代码生成器 - 表 接口实现类 + */ +@Service +public class MySQLDatabaseTableServiceImpl implements DatabaseTableService { + + /** 排除表 */ + private static final List EXCLUDE_TABLES; + + static { + Props props = new Props("creater.yaml"); + EXCLUDE_TABLES = props.getList("opsli.exclude-tables"); + } + + @Autowired(required = false) + private MySQLDatabaseTableMapper mapper; + + @Autowired + private ITableService iTableService; + + @Override + public String getType() { + return DbType.DB_MYSQL; + } + + @Override + public List findTables(String dbName) { + return this.findTables(dbName, null); + } + + @Override + public List findTables(String dbName, String tableName) { + DatabaseTable table = new DatabaseTable(); + table.setDbName(dbName); + + if(StringUtils.isNotEmpty(tableName)){ + table.setTableName(tableName); + } + + List tables = mapper.findTables(table); + + // 表去重复 + List currTableNames = iTableService.findAllByTableName(); + if(currTableNames != null && !currTableNames.isEmpty()){ + //遍历删除 + tables.removeIf(tmp -> currTableNames.contains(tmp.getTableName())); + //遍历删除 + tables.removeIf(tmp -> EXCLUDE_TABLES.contains(tmp.getTableName())); + } + + return tables; + } + + + @Override + public List findColumns(String dbName, String tableName) { + DatabaseColumn entity = new DatabaseColumn(); + entity.setDbName(dbName); + entity.setTableName(tableName); + + List columns = mapper.findColumns(entity); + + // 设置字段长度 + for (DatabaseColumn column : columns) { + // MySQL 中 这两个 有一个会代表为当前字段长度 + Integer len1 = column.getColumnLength(); + Integer len2 = column.getColumnPrecision(); + if(len1 == null && len2 != null){ + column.setColumnLength(len2); + } + // 如果小数位不为空 则需要 减掉小数位置 + if(column.getColumnScale() != null){ + column.setColumnLength( + column.getColumnLength() - column.getColumnScale() + ); + } + } + + return columns; + } +} + + diff --git a/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/modulars/creater/table/api/TableApi.java b/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/modulars/creater/table/api/TableApi.java index b29fba1..227c6f7 100644 --- a/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/modulars/creater/table/api/TableApi.java +++ b/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/modulars/creater/table/api/TableApi.java @@ -110,4 +110,18 @@ public interface TableApi { @PostMapping("/sync") ResultVo sync(String id); + /** + * 获得当前数据库 + * @return ResultVo + */ + @GetMapping("/getTables") + ResultVo getTables(); + + /** + * 导入选中表 + * @return ResultVo + */ + @PostMapping("/importTables") + ResultVo importTables(String[] tableNames); + } diff --git a/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/modulars/creater/table/mapper/TableMapper.java b/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/modulars/creater/table/mapper/TableMapper.java index bef38bb..5956a56 100644 --- a/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/modulars/creater/table/mapper/TableMapper.java +++ b/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/modulars/creater/table/mapper/TableMapper.java @@ -19,6 +19,8 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper; import org.apache.ibatis.annotations.Mapper; import org.opsli.modulars.creater.table.entity.CreaterTable; +import java.util.List; + /** * @BelongsProject: opsli-boot @@ -42,4 +44,10 @@ public interface TableMapper extends BaseMapper { */ void renewSyncState(String id); + /** + * 获得当前 生成器中所有表名 + * @return + */ + List findAllByTableName(); + } diff --git a/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/modulars/creater/table/mapper/xml/TableMapper.xml b/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/modulars/creater/table/mapper/xml/TableMapper.xml index f94f8dc..b3bb5ae 100644 --- a/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/modulars/creater/table/mapper/xml/TableMapper.xml +++ b/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/modulars/creater/table/mapper/xml/TableMapper.xml @@ -16,6 +16,13 @@ + + update creater_table set iz_sync = '1' diff --git a/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/modulars/creater/table/service/ITableService.java b/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/modulars/creater/table/service/ITableService.java index da085a3..206adf4 100644 --- a/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/modulars/creater/table/service/ITableService.java +++ b/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/modulars/creater/table/service/ITableService.java @@ -20,6 +20,8 @@ import org.opsli.modulars.creater.table.entity.CreaterTable; import org.opsli.modulars.creater.table.wrapper.CreaterTableAndColumnModel; import org.opsli.modulars.creater.table.wrapper.CreaterTableModel; +import java.util.List; + /** * @BelongsProject: opsli-boot @@ -63,4 +65,16 @@ public interface ITableService extends CrudServiceInterface findAllByTableName(); + + /** + * 导入数据库表 + * @param tableNames + */ + void importTables(String[] tableNames); + } diff --git a/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/modulars/creater/table/service/impl/TableServiceImpl.java b/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/modulars/creater/table/service/impl/TableServiceImpl.java index 53f4011..51a0a06 100644 --- a/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/modulars/creater/table/service/impl/TableServiceImpl.java +++ b/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/modulars/creater/table/service/impl/TableServiceImpl.java @@ -15,6 +15,7 @@ */ package org.opsli.modulars.creater.table.service.impl; +import cn.hutool.core.util.StrUtil; import org.opsli.common.enums.DictType; import org.opsli.common.exception.ServiceException; import org.opsli.common.utils.WrapperUtil; @@ -22,7 +23,9 @@ import org.opsli.core.base.service.impl.CrudServiceImpl; import org.opsli.core.creater.msg.CreaterMsg; import org.opsli.modulars.creater.column.service.ITableColumnService; import org.opsli.modulars.creater.column.wrapper.CreaterTableColumnModel; -import org.opsli.modulars.creater.general.actuator.SQLActuator; +import org.opsli.modulars.creater.importable.ImportTableUtil; +import org.opsli.modulars.creater.importable.entity.DatabaseColumn; +import org.opsli.modulars.creater.importable.entity.DatabaseTable; import org.opsli.modulars.creater.table.entity.CreaterTable; import org.opsli.modulars.creater.table.mapper.TableMapper; import org.opsli.modulars.creater.table.service.ITableService; @@ -32,6 +35,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.util.ArrayList; import java.util.List; @@ -51,9 +55,6 @@ public class TableServiceImpl extends CrudServiceImpl findAllByTableName() { + return mapper.findAllByTableName(); + } + + @Override + @Transactional(rollbackFor = Exception.class) + public void importTables(String[] tableNames) { + for (String tableName : tableNames) { + // 获得当前表 + DatabaseTable table = null; + List tables = ImportTableUtil.findTables(tableName); + if(tables != null && !tables.isEmpty()){ + table = tables.get(0); + } + + if(table == null){ + String msg = StrUtil.format(CreaterMsg.EXCEPTION_IMPORT_TABLE_NULL.getMessage(), tableName); + // 暂无该表 + throw new ServiceException(CreaterMsg.EXCEPTION_IMPORT_TABLE_NULL.getCode(), msg); + } + + // 获得表字段 + List columns = ImportTableUtil.findColumns(tableName); + List columnModels = new ArrayList<>(); + for (int i = 0; i < columns.size(); i++) { + DatabaseColumn column = columns.get(i); + CreaterTableColumnModel columnModel = new CreaterTableColumnModel(); + columnModel.setFieldName(column.getColumnName()); + columnModel.setFieldType(column.getColumnType()); + columnModel.setFieldLength(column.getColumnLength()); + columnModel.setFieldPrecision(column.getColumnScale()); + columnModel.setFieldComments(column.getColumnComment()); + columnModel.setIzPk(column.getIzPk()); + columnModel.setIzNull(column.getIzNull()); + columnModel.setSort(i); + // 赋默认值 + columnModel.setJavaType("String"); + columnModels.add(columnModel); + } + + // 生成本地数据 + CreaterTableAndColumnModel createrTableModel = new CreaterTableAndColumnModel(); + createrTableModel.setComments(table.getTableComments()); + createrTableModel.setTableName(table.getTableName()); + createrTableModel.setOldTableName(table.getTableName()); + createrTableModel.setIzSync('1'); + createrTableModel.setJdbcType(ImportTableUtil.DB_TYPE); + createrTableModel.setTableType('0'); + createrTableModel.setColumnList(columnModels); + createrTableModel.setIzApi(true); + this.insertAny(createrTableModel); + } + } } diff --git a/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/modulars/creater/table/web/TableRestController.java b/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/modulars/creater/table/web/TableRestController.java index 34d195b..79f22b1 100644 --- a/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/modulars/creater/table/web/TableRestController.java +++ b/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/modulars/creater/table/web/TableRestController.java @@ -24,6 +24,7 @@ import org.opsli.common.annotation.EnableLog; import org.opsli.common.exception.ServiceException; import org.opsli.common.utils.WrapperUtil; import org.opsli.core.base.concroller.BaseRestController; +import org.opsli.core.creater.msg.CreaterMsg; import org.opsli.core.creater.strategy.sync.util.SQLSyncUtil; import org.opsli.core.msg.CoreMsg; import org.opsli.core.persistence.Page; @@ -31,6 +32,7 @@ import org.opsli.core.persistence.querybuilder.QueryBuilder; import org.opsli.core.persistence.querybuilder.WebQueryBuilder; import org.opsli.modulars.creater.column.service.ITableColumnService; import org.opsli.modulars.creater.column.wrapper.CreaterTableColumnModel; +import org.opsli.modulars.creater.importable.ImportTableUtil; import org.opsli.modulars.creater.table.api.TableApi; import org.opsli.modulars.creater.table.entity.CreaterTable; import org.opsli.modulars.creater.table.service.ITableService; @@ -56,6 +58,7 @@ public class TableRestController extends BaseRestController getTables() { + return ResultVo.success(ImportTableUtil.findTables()); + } + + @ApiOperation(value = "导入数据库表", notes = "导入数据库表") + @RequiresPermissions("deve_creater_import") + @EnableLog + @Override + public ResultVo importTables(String[] tableNames) { + if(tableNames == null){ + // 未选中表,无法导入 + throw new ServiceException(CreaterMsg.EXCEPTION_IMPORT_NULL); + } + IService.importTables(tableNames); + return ResultVo.success("导入成功"); + } } diff --git a/opsli-modulars/opsli-modulars-creater/src/main/resources/creater.yaml b/opsli-modulars/opsli-modulars-creater/src/main/resources/creater.yaml index 9ab2d25..30ab981 100644 --- a/opsli-modulars/opsli-modulars-creater/src/main/resources/creater.yaml +++ b/opsli-modulars/opsli-modulars-creater/src/main/resources/creater.yaml @@ -1,5 +1,9 @@ # 排除表 防止代码生成器直接 非法删除关键表 opsli: + # 数据库类型 mysql oracle + db-type: mysql + # 数据库名 + db-name: opsli-boot exclude-tables: - creater_table - creater_table_column