代码生成器优化-解决手动更改数据库痛点

v1.4.1
Parker 5 years ago
parent 981e679163
commit 18728a44ce

@ -0,0 +1,63 @@
package org.opsli.core.creater.enums;
/**
*
*
* @author Mybatis-plus
*/
public enum DataBaseType {
/** 数据库类型 */
MYSQL("mysql", "MySql数据库"),
MARIADB("mariadb", "MariaDB数据库"),
ORACLE("oracle", "Oracle11g及以下数据库(高版本推荐使用ORACLE_NEW)"),
ORACLE_12C("oracle12c", "Oracle12c+数据库"),
DB2("db2", "DB2数据库"),
H2("h2", "H2数据库"),
HSQL("hsql", "HSQL数据库"),
SQLITE("sqlite", "SQLite数据库"),
POSTGRE_SQL("postgresql", "Postgre数据库"),
SQL_SERVER2005("sqlserver2005", "SQLServer2005数据库"),
SQL_SERVER("sqlserver", "SQLServer数据库"),
DM("dm", "达梦数据库"),
XU_GU("xugu", "虚谷数据库"),
KINGBASE_ES("kingbasees", "人大金仓数据库"),
PHOENIX("phoenix", "Phoenix HBase数据库"),
GAUSS("zenith", "Gauss 数据库"),
CLICK_HOUSE("clickhouse", "clickhouse 数据库"),
GBASE("gbase", "南大通用数据库"),
OSCAR("oscar", "神通数据库"),
SYBASE("sybase", "Sybase ASE 数据库"),
OCEAN_BASE("oceanbase", "OceanBase 数据库"),
FIREBIRD("Firebird", "Firebird 数据库"),
OTHER("other", "其他数据库");
private final String db;
private final String desc;
public static DataBaseType getDbType(String dbType) {
DataBaseType[] var1 = values();
int var2 = var1.length;
for (DataBaseType type : var1) {
if (type.db.equalsIgnoreCase(dbType)) {
return type;
}
}
return OTHER;
}
public String getDb() {
return this.db;
}
public String getDesc() {
return this.desc;
}
private DataBaseType(final String db, final String desc) {
this.db = db;
this.desc = desc;
}
}

@ -18,6 +18,7 @@ package org.opsli.core.creater.strategy.sync;
import org.apache.commons.lang3.StringUtils;
import org.opsli.common.enums.DictType;
import org.opsli.common.utils.Props;
import org.opsli.core.creater.enums.DataBaseType;
import org.opsli.core.creater.exception.CreaterException;
import org.opsli.core.creater.msg.CreaterMsg;
import org.opsli.core.creater.strategy.sync.mysql.entity.FieldTypeAttribute;
@ -66,8 +67,8 @@ public class MySQLSyncBuilder implements SyncStrategy {
private ITableService iTableService;
@Override
public String getType() {
return "mysql";
public DataBaseType getType() {
return DataBaseType.MYSQL;
}
/**

@ -15,6 +15,7 @@
*/
package org.opsli.core.creater.strategy.sync;
import org.opsli.core.creater.enums.DataBaseType;
import org.opsli.modulars.creater.table.wrapper.CreaterTableAndColumnModel;
/**
@ -30,7 +31,7 @@ public interface SyncStrategy {
*
* @return
*/
String getType();
DataBaseType getType();
/**
*

@ -17,6 +17,7 @@ package org.opsli.core.creater.strategy.sync.util;
import cn.hutool.core.util.ClassUtil;
import lombok.extern.slf4j.Slf4j;
import org.opsli.core.creater.enums.DataBaseType;
import org.opsli.core.creater.strategy.sync.SyncStrategy;
import org.opsli.core.utils.SpringContextHolder;
import org.opsli.modulars.creater.table.wrapper.CreaterTableAndColumnModel;
@ -38,11 +39,11 @@ import java.util.concurrent.ConcurrentMap;
*/
@Slf4j
@Configuration
public class SQLSyncUtil {
public class SqlSyncUtil {
/** 处理方法集合 */
private static final ConcurrentMap<String, SyncStrategy> HANDLER_MAP = new ConcurrentHashMap<>();
private static final ConcurrentMap<DataBaseType, SyncStrategy> HANDLER_MAP = new ConcurrentHashMap<>();
@Bean
@ -74,7 +75,11 @@ public class SQLSyncUtil {
return;
}
SyncStrategy syncStrategy = HANDLER_MAP.get(model.getJdbcType());
// 获得数据库类型 枚举
String jdbcType = model.getJdbcType();
DataBaseType dbType = DataBaseType.getDbType(jdbcType);
SyncStrategy syncStrategy = HANDLER_MAP.get(dbType);
if(syncStrategy != null){
syncStrategy.execute(model);
}

@ -15,20 +15,25 @@
*/
package org.opsli.modulars.creater.importable;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ClassUtil;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.opsli.common.utils.Props;
import org.opsli.core.creater.strategy.sync.SyncStrategy;
import org.apache.commons.lang3.StringUtils;
import org.opsli.core.autoconfigure.DbSourceProperties;
import org.opsli.core.creater.enums.DataBaseType;
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.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.lang.reflect.Modifier;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
@ -44,55 +49,130 @@ import java.util.concurrent.ConcurrentMap;
@Configuration
public class ImportTableUtil{
/** 数据库名 */
public static final String DB_NAME;
/** 数据库类型 */
public static final String DB_TYPE;
/** 处理方法集合 */
private static final ConcurrentMap<String, DatabaseTableService> HANDLER_MAP = new ConcurrentHashMap<>();
private static final ConcurrentMap<DataBaseType, DatabaseTableService> HANDLER_MAP = new ConcurrentHashMap<>();
/** 数据库类型 */
private static final Map<String, DataBaseType> DB_TYPE_MAP;
/** 指定 master 数据库 */
private static final String ASSIGN_DB = "master";
static {
Props props = new Props("creater.yaml");
DB_NAME = props.getStr("opsli.db-name","opsli-boot");
DB_TYPE = props.getStr("opsli.db-type");
DB_TYPE_MAP = Maps.newHashMap();
DB_TYPE_MAP.put("com.mysql.jdbc.Driver", DataBaseType.MYSQL);
DB_TYPE_MAP.put("com.mysql.cj.jdbc.Driver", DataBaseType.MYSQL);
DB_TYPE_MAP.put("com.microsoft.jdbc.sqlserver.SQLServerDriver", DataBaseType.SQL_SERVER);
DB_TYPE_MAP.put("com.microsoft.sqlserver.jdbc.SQLServerDriver", DataBaseType.SQL_SERVER);
DB_TYPE_MAP.put("com.sybase.jdbc.SybDriver", DataBaseType.SYBASE);
DB_TYPE_MAP.put("oracle.jdbc.driver.OracleDriver", DataBaseType.ORACLE);
DB_TYPE_MAP.put("org.postgresql.Driver", DataBaseType.POSTGRE_SQL);
DB_TYPE_MAP.put("com.ibm.db2.jdbc.app.DB2.Driver", DataBaseType.DB2);
}
/** 数据库信息 */
private static DbSourceProperties dbSourceProperties;
/**
*
* @return
*/
public static DataBaseType getDbType(){
if(dbSourceProperties != null){
Map<String, DbSourceProperties.DataSourceInfo> dataSourceInfoMap = dbSourceProperties.getDataSourceInfoMap();
DbSourceProperties.DataSourceInfo dataSourceInfo = dataSourceInfoMap.get(ASSIGN_DB);
if(dataSourceInfo != null){
return DB_TYPE_MAP.get(dataSourceInfo.getDriverClassName());
}
}
return null;
}
/**
*
* @return
* @return List
*/
public static List<DatabaseTable> findTables() {
DatabaseTableService databaseTableService = HANDLER_MAP.get(DB_TYPE);
if(databaseTableService == null){
return null;
}
return databaseTableService.findTables(DB_NAME);
return ImportTableUtil.findTables(null);
}
/**
*
* @param tableName
* @return
* @param tableName
* @return List
*/
public static List<DatabaseTable> findTables(String tableName) {
DatabaseTableService databaseTableService = HANDLER_MAP.get(DB_TYPE);
if(databaseTableService == null){
Map<String, DbSourceProperties.DataSourceInfo> dataSourceInfoMap =
dbSourceProperties.getDataSourceInfoMap();
// 非法判断
if(CollUtil.isEmpty(dataSourceInfoMap)){
return null;
}
return databaseTableService.findTables(DB_NAME, tableName);
List<DatabaseTable> databaseTables = Lists.newArrayList();
for (Map.Entry<String, DbSourceProperties.DataSourceInfo> dataSourceInfoEntry
: dataSourceInfoMap.entrySet()) {
// 如果master不为空 且 多数据源不为主数据源 则不进行处理
if(!StringUtils.equals(ASSIGN_DB, dataSourceInfoEntry.getKey())){
continue;
}
// 数据源
String key = dataSourceInfoEntry.getKey();
DbSourceProperties.DataSourceInfo dataSource = dataSourceInfoEntry.getValue();
// 根据类型获得查询器
DataBaseType dataBaseType = DB_TYPE_MAP.get(dataSource.getDriverClassName());
DatabaseTableService databaseTableService = HANDLER_MAP.get(dataBaseType);
if(databaseTableService == null){
continue;
}
// 获得当前库下表集合
List<DatabaseTable> tables;
if(StringUtils.isNotEmpty(tableName)){
tables = databaseTableService.findTables(dataSource.getDbName(), tableName);
}else{
tables = databaseTableService.findTables(dataSource.getDbName());
}
// 如果不为空则存入集合
if(CollUtil.isNotEmpty(tables)){
for (DatabaseTable table : tables) {
table.setDbSource(key);
}
databaseTables.addAll(tables);
}
}
return databaseTables;
}
/**
*
* @param tableName
* @return
* @param tableName
* @return List
*/
public static List<DatabaseColumn> findColumns(String tableName) {
DatabaseTableService databaseTableService = HANDLER_MAP.get(DB_TYPE);
Map<String, DbSourceProperties.DataSourceInfo> dataSourceInfoMap =
dbSourceProperties.getDataSourceInfoMap();
// 非法判断
if(CollUtil.isEmpty(dataSourceInfoMap)){
return null;
}
DbSourceProperties.DataSourceInfo dataSource = dataSourceInfoMap.get(ASSIGN_DB);
if(dataSource == null){
return null;
}
// 根据类型获得查询器
DataBaseType dataBaseType = DB_TYPE_MAP.get(dataSource.getDriverClassName());
DatabaseTableService databaseTableService = HANDLER_MAP.get(dataBaseType);
if(databaseTableService == null){
return null;
}
return databaseTableService.findColumns(DB_NAME, tableName);
return databaseTableService.findColumns(dataSource.getDbName(), tableName);
}
@ -119,4 +199,8 @@ public class ImportTableUtil{
}
}
@Autowired
public void setDbSourceProperties(DbSourceProperties dbSourceProperties) {
ImportTableUtil.dbSourceProperties = dbSourceProperties;
}
}

@ -1,19 +0,0 @@
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";
}

@ -28,6 +28,8 @@ import lombok.EqualsAndHashCode;
@EqualsAndHashCode(callSuper = false)
public class DatabaseTable {
/** 数据源 */
private String dbSource;
/** 数据库 */
private String dbName;
@ -38,7 +40,6 @@ public class DatabaseTable {
/** 描述 */
private String tableComments;
// ========================================
}

@ -15,6 +15,7 @@
*/
package org.opsli.modulars.creater.importable.service;
import org.opsli.core.creater.enums.DataBaseType;
import org.opsli.modulars.creater.importable.entity.DatabaseColumn;
import org.opsli.modulars.creater.importable.entity.DatabaseTable;
@ -33,7 +34,7 @@ public interface DatabaseTableService {
*
* @return
*/
String getType();
DataBaseType getType();
/**
*

@ -17,7 +17,7 @@ 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.core.creater.enums.DataBaseType;
import org.opsli.modulars.creater.importable.entity.DatabaseColumn;
import org.opsli.modulars.creater.importable.entity.DatabaseTable;
import org.opsli.modulars.creater.importable.mapper.MySQLDatabaseTableMapper;
@ -52,8 +52,8 @@ public class MySQLDatabaseTableServiceImpl implements DatabaseTableService {
private ITableService iTableService;
@Override
public String getType() {
return DbType.DB_MYSQL;
public DataBaseType getType() {
return DataBaseType.MYSQL;
}
@Override

@ -243,7 +243,7 @@ public class TableServiceImpl extends CrudServiceImpl<TableMapper, CreaterTable,
createrTableModel.setTableName(table.getTableName());
createrTableModel.setOldTableName(table.getTableName());
createrTableModel.setIzSync("1");
createrTableModel.setJdbcType(ImportTableUtil.DB_TYPE);
createrTableModel.setJdbcType(ImportTableUtil.getDbType()!=null?ImportTableUtil.getDbType().getDb():null);
createrTableModel.setTableType("0");
createrTableModel.setColumnList(columnModels);
createrTableModel.setIzApi(true);

@ -27,7 +27,7 @@ import org.opsli.common.utils.WrapperUtil;
import org.opsli.core.base.controller.BaseRestController;
import org.opsli.core.creater.exception.CreaterException;
import org.opsli.core.creater.msg.CreaterMsg;
import org.opsli.core.creater.strategy.sync.util.SQLSyncUtil;
import org.opsli.core.creater.strategy.sync.util.SqlSyncUtil;
import org.opsli.core.msg.CoreMsg;
import org.opsli.core.persistence.Page;
import org.opsli.core.persistence.querybuilder.QueryBuilder;
@ -227,7 +227,7 @@ public class TableRestController extends BaseRestController<CreaterTable, Create
currModel.setColumnList(columnModelList);
// 执行同步操作
SQLSyncUtil.execute(currModel);
SqlSyncUtil.execute(currModel);
return ResultVo.success("同步成功");
}

@ -1,8 +1,5 @@
# 暂时只支持 mysql
opsli:
# 数据库类型 mysql oracle 暂不支持)
db-type: mysql
# 数据库名
db-name: opsli-boot
# 排除表 防止代码生成器直接 非法删除关键表
exclude-tables:
- creater_table

Loading…
Cancel
Save