代码生成器完成 50% , 可生成数据库表, 可导入数据库表

v1.4.1
Parker 4 years ago
parent fc3de4123a
commit 37f6b62975

@ -21,6 +21,7 @@ import org.yaml.snakeyaml.Yaml;
import java.io.InputStream; import java.io.InputStream;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
@ -162,6 +163,7 @@ public class Props {
Object obj = this.getObject(keys); Object obj = this.getObject(keys);
if(obj != null){ if(obj != null){
def = "";
def = Convert.convert(def.getClass(), obj); def = Convert.convert(def.getClass(), obj);
} }
return def; return def;
@ -339,6 +341,34 @@ public class Props {
return def; return def;
} }
/**
*
* @return
*/
public List<String> getList(String key){
return this.getList(key, null);
}
/**
*
* @return
*/
public List<String> getList(String key, List<String> 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;
}
/** /**
* *

@ -42,6 +42,12 @@ public enum CreaterMsg implements BaseMsg {
EXCEPTION_SYNC_NULL(50100,"同步表失败,暂无该表"), EXCEPTION_SYNC_NULL(50100,"同步表失败,暂无该表"),
EXCEPTION_SYNC_CORE(50101,"系统核心关键表不允许同步"), EXCEPTION_SYNC_CORE(50101,"系统核心关键表不允许同步"),
/**
*
*/
EXCEPTION_IMPORT_NULL(50120,"未选中表,无法导入"),
EXCEPTION_IMPORT_TABLE_NULL(50121,"暂无{}该表"),
; ;
private int code; private int code;

@ -1,7 +1,9 @@
package org.opsli.core.creater.strategy.sync; package org.opsli.core.creater.strategy.sync;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.opsli.common.exception.ServiceException; import org.opsli.common.exception.ServiceException;
import org.opsli.common.utils.Props;
import org.opsli.core.creater.msg.CreaterMsg; 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.entity.FieldTypeAttribute;
import org.opsli.core.creater.strategy.sync.mysql.enums.MySQLSyncColumnType; 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 YES = '1';
/** 否 */ /** 否 */
private static final char NO = '0'; private static final char NO = '0';
/** 排除表 */
private static final List<String> EXCLUDE_TABLES;
static {
Props props = new Props("creater.yaml");
EXCLUDE_TABLES = props.getList("opsli.exclude-tables");
}
@Autowired(required = false) @Autowired(required = false)
private SQLActuator sqlActuator; private SQLActuator sqlActuator;
@ -66,9 +75,7 @@ public class MySQLSyncBuilder implements SyncStrategy {
} }
// 排查该表 是否是 在排除外的表, 如果是则不允许同步 // 排查该表 是否是 在排除外的表, 如果是则不允许同步
List<String> excludeTables = new ArrayList<>(); if(EXCLUDE_TABLES.contains(currTable.getOldTableName()) || EXCLUDE_TABLES.contains(currTable.getTableName())){
excludeTables.add("creater_table");
if(excludeTables.contains(currTable.getOldTableName()) || excludeTables.contains(currTable.getTableName())){
// 同步表失败 系统核心关键表不允许同步 // 同步表失败 系统核心关键表不允许同步
throw new ServiceException(CreaterMsg.EXCEPTION_SYNC_CORE); throw new ServiceException(CreaterMsg.EXCEPTION_SYNC_CORE);
} }
@ -123,10 +130,13 @@ public class MySQLSyncBuilder implements SyncStrategy {
if(fieldAttr != null){ if(fieldAttr != null){
// 字段有长度 // 字段有长度
if(fieldAttr.isIzLength()){ if(fieldAttr.isIzLength()){
str.append("(").append(tmp.getFieldLength()); Integer len = tmp.getFieldLength();
str.append("(");
// 字段有精度 // 字段有精度
if(fieldAttr.isIzPrecision()){ if(fieldAttr.isIzPrecision()){
str.append(",").append(tmp.getFieldPrecision()); str.append( len + tmp.getFieldPrecision() ).append(",").append(tmp.getFieldPrecision());
} else {
str.append(len);
} }
str.append(")"); str.append(")");
} }

@ -160,7 +160,7 @@ public class TableColumnServiceImpl extends CrudServiceImpl<TableColumnMapper, C
QueryBuilder<CreaterTableColumn> queryBuilder = QueryBuilder<CreaterTableColumn> queryBuilder =
new GenQueryBuilder<>(); new GenQueryBuilder<>();
QueryWrapper<CreaterTableColumn> wrapper = queryBuilder.build(); QueryWrapper<CreaterTableColumn> wrapper = queryBuilder.build();
wrapper.eq("tableId", tableId); wrapper.eq("table_id", tableId);
super.remove(wrapper); super.remove(wrapper);
} }
} }

@ -0,0 +1,122 @@
/**
* Copyright 2020 OPSLI https://www.opsli.com
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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<String, DatabaseTableService> 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<DatabaseTable> findTables() {
DatabaseTableService databaseTableService = HANDLER_MAP.get(DB_TYPE);
if(databaseTableService == null){
return null;
}
return databaseTableService.findTables(DB_NAME);
}
/**
*
* @param tableName
* @return
*/
public static List<DatabaseTable> 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<DatabaseColumn> 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<Class<?>> 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);
}
}
}

@ -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";
}

@ -0,0 +1,64 @@
/**
* Copyright 2020 OPSLI https://www.opsli.com
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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;
// ========================================
}

@ -0,0 +1,44 @@
/**
* Copyright 2020 OPSLI https://www.opsli.com
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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;
// ========================================
}

@ -0,0 +1,48 @@
/**
* Copyright 2020 OPSLI https://www.opsli.com
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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<DatabaseTable> findTables(DatabaseTable table);
/**
*
* @param column
* @return
*/
List<DatabaseColumn> findColumns(DatabaseColumn column);
}

@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.opsli.modulars.creater.importable.mapper.MySQLDatabaseTableMapper">
<select id="findTables" resultType="org.opsli.modulars.creater.importable.entity.DatabaseTable">
SELECT
TB.TABLE_SCHEMA AS dbName,
TB.TABLE_NAME AS tableName,
TB.TABLE_COMMENT AS tableComments
FROM
INFORMATION_SCHEMA.TABLES TB
WHERE
TB.TABLE_SCHEMA = #{dbName}
<if test="tableName != null and tableName != ''">
AND TB.TABLE_NAME = #{tableName}
</if>
</select>
<select id="findColumns" resultType="org.opsli.modulars.creater.importable.entity.DatabaseColumn">
SELECT
TABLE_SCHEMA AS dbName,
TABLE_NAME AS tableName,
COLUMN_NAME AS columnName,
data_type AS columnType,
CHARACTER_MAXIMUM_LENGTH AS columnLength,
NUMERIC_PRECISION AS columnPrecision,
NUMERIC_SCALE AS columnScale,
COLUMN_COMMENT AS columnComment,
IF( IS_NULLABLE = 'NO', '1', '0' ) AS izNull,
IF( COLUMN_KEY = 'PRI', '1', '0' ) AS izPk
FROM
INFORMATION_SCHEMA.COLUMNS
WHERE
TABLE_NAME = #{tableName}
AND TABLE_SCHEMA = #{dbName}
ORDER BY ORDINAL_POSITION
</select>
</mapper>

@ -0,0 +1,60 @@
/**
* Copyright 2020 OPSLI https://www.opsli.com
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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<DatabaseTable> findTables(String dbName);
/**
*
* @param dbName
* @return
*/
List<DatabaseTable> findTables(String dbName, String tableName);
/**
*
* @param dbName
* @param tableName
* @return
*/
List<DatabaseColumn> findColumns(String dbName, String tableName);
}

@ -0,0 +1,116 @@
/**
* Copyright 2020 OPSLI https://www.opsli.com
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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<String> 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<DatabaseTable> findTables(String dbName) {
return this.findTables(dbName, null);
}
@Override
public List<DatabaseTable> findTables(String dbName, String tableName) {
DatabaseTable table = new DatabaseTable();
table.setDbName(dbName);
if(StringUtils.isNotEmpty(tableName)){
table.setTableName(tableName);
}
List<DatabaseTable> tables = mapper.findTables(table);
// 表去重复
List<String> 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<DatabaseColumn> findColumns(String dbName, String tableName) {
DatabaseColumn entity = new DatabaseColumn();
entity.setDbName(dbName);
entity.setTableName(tableName);
List<DatabaseColumn> 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;
}
}

@ -110,4 +110,18 @@ public interface TableApi {
@PostMapping("/sync") @PostMapping("/sync")
ResultVo<?> sync(String id); ResultVo<?> sync(String id);
/**
*
* @return ResultVo
*/
@GetMapping("/getTables")
ResultVo<?> getTables();
/**
*
* @return ResultVo
*/
@PostMapping("/importTables")
ResultVo<?> importTables(String[] tableNames);
} }

@ -19,6 +19,8 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Mapper;
import org.opsli.modulars.creater.table.entity.CreaterTable; import org.opsli.modulars.creater.table.entity.CreaterTable;
import java.util.List;
/** /**
* @BelongsProject: opsli-boot * @BelongsProject: opsli-boot
@ -42,4 +44,10 @@ public interface TableMapper extends BaseMapper<CreaterTable> {
*/ */
void renewSyncState(String id); void renewSyncState(String id);
/**
*
* @return
*/
List<String> findAllByTableName();
} }

@ -16,6 +16,13 @@
</where> </where>
</select> </select>
<select id="findAllByTableName" resultType="String">
select
table_name AS tableName
from
creater_table
</select>
<update id="renewSyncState"> <update id="renewSyncState">
update creater_table update creater_table
set iz_sync = '1' set iz_sync = '1'

@ -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.CreaterTableAndColumnModel;
import org.opsli.modulars.creater.table.wrapper.CreaterTableModel; import org.opsli.modulars.creater.table.wrapper.CreaterTableModel;
import java.util.List;
/** /**
* @BelongsProject: opsli-boot * @BelongsProject: opsli-boot
@ -63,4 +65,16 @@ public interface ITableService extends CrudServiceInterface<CreaterTable, Create
*/ */
void renewSyncState(String id); void renewSyncState(String id);
/**
*
* @return
*/
List<String> findAllByTableName();
/**
*
* @param tableNames
*/
void importTables(String[] tableNames);
} }

@ -15,6 +15,7 @@
*/ */
package org.opsli.modulars.creater.table.service.impl; package org.opsli.modulars.creater.table.service.impl;
import cn.hutool.core.util.StrUtil;
import org.opsli.common.enums.DictType; import org.opsli.common.enums.DictType;
import org.opsli.common.exception.ServiceException; import org.opsli.common.exception.ServiceException;
import org.opsli.common.utils.WrapperUtil; 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.core.creater.msg.CreaterMsg;
import org.opsli.modulars.creater.column.service.ITableColumnService; import org.opsli.modulars.creater.column.service.ITableColumnService;
import org.opsli.modulars.creater.column.wrapper.CreaterTableColumnModel; 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.entity.CreaterTable;
import org.opsli.modulars.creater.table.mapper.TableMapper; import org.opsli.modulars.creater.table.mapper.TableMapper;
import org.opsli.modulars.creater.table.service.ITableService; 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.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -51,9 +55,6 @@ public class TableServiceImpl extends CrudServiceImpl<TableMapper, CreaterTable,
@Autowired @Autowired
private ITableColumnService iTableColumnService; private ITableColumnService iTableColumnService;
@Autowired(required = false)
private SQLActuator sqlActuator;
@Override @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public CreaterTableModel insert(CreaterTableModel model) { public CreaterTableModel insert(CreaterTableModel model) {
@ -67,9 +68,11 @@ public class TableServiceImpl extends CrudServiceImpl<TableMapper, CreaterTable,
throw new ServiceException(CreaterMsg.EXCEPTION_TABLE_NAME_REPEAT); throw new ServiceException(CreaterMsg.EXCEPTION_TABLE_NAME_REPEAT);
} }
// 新增后 默认未同步 if(!model.getIzApi()){
model.setIzSync( // 新增后 默认未同步
DictType.NO_YES_NO.getCode().charAt(0)); model.setIzSync(
DictType.NO_YES_NO.getCode().charAt(0));
}
// 默认旧表名称为当前新增名称(用于删除表操作) // 默认旧表名称为当前新增名称(用于删除表操作)
model.setOldTableName(model.getTableName()); model.setOldTableName(model.getTableName());
@ -140,9 +143,6 @@ public class TableServiceImpl extends CrudServiceImpl<TableMapper, CreaterTable,
} }
iTableColumnService.insertBatch(columnList); iTableColumnService.insertBatch(columnList);
} }
sqlActuator.execute("select * from sys_user");
} }
@Override @Override
@ -175,6 +175,61 @@ public class TableServiceImpl extends CrudServiceImpl<TableMapper, CreaterTable,
mapper.renewSyncState(id); mapper.renewSyncState(id);
} }
@Override
public List<String> findAllByTableName() {
return mapper.findAllByTableName();
}
@Override
@Transactional(rollbackFor = Exception.class)
public void importTables(String[] tableNames) {
for (String tableName : tableNames) {
// 获得当前表
DatabaseTable table = null;
List<DatabaseTable> 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<DatabaseColumn> columns = ImportTableUtil.findColumns(tableName);
List<CreaterTableColumnModel> 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);
}
}
} }

@ -24,6 +24,7 @@ import org.opsli.common.annotation.EnableLog;
import org.opsli.common.exception.ServiceException; import org.opsli.common.exception.ServiceException;
import org.opsli.common.utils.WrapperUtil; import org.opsli.common.utils.WrapperUtil;
import org.opsli.core.base.concroller.BaseRestController; 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.creater.strategy.sync.util.SQLSyncUtil;
import org.opsli.core.msg.CoreMsg; import org.opsli.core.msg.CoreMsg;
import org.opsli.core.persistence.Page; 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.core.persistence.querybuilder.WebQueryBuilder;
import org.opsli.modulars.creater.column.service.ITableColumnService; import org.opsli.modulars.creater.column.service.ITableColumnService;
import org.opsli.modulars.creater.column.wrapper.CreaterTableColumnModel; 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.api.TableApi;
import org.opsli.modulars.creater.table.entity.CreaterTable; import org.opsli.modulars.creater.table.entity.CreaterTable;
import org.opsli.modulars.creater.table.service.ITableService; import org.opsli.modulars.creater.table.service.ITableService;
@ -56,6 +58,7 @@ public class TableRestController extends BaseRestController<CreaterTable, Create
@Autowired @Autowired
private ITableColumnService iTableColumnService; private ITableColumnService iTableColumnService;
/** /**
* *
* @param model * @param model
@ -224,4 +227,23 @@ public class TableRestController extends BaseRestController<CreaterTable, Create
return ResultVo.success("同步成功"); return ResultVo.success("同步成功");
} }
@ApiOperation(value = "获得当前数据库表", notes = "获得当前数据库表")
@RequiresPermissions("deve_creater_select")
@Override
public ResultVo<?> 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("导入成功");
}
} }

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

Loading…
Cancel
Save