【7.0.4】整理DbInitializer

pull/17/head
fengshuonan 2021-05-19 11:22:19 +08:00
parent 43f9fc3236
commit 3a56a63ee2
4 changed files with 51 additions and 91 deletions

View File

@ -1,34 +0,0 @@
package cn.stylefeng.roses.kernel.db.api.pojo.db;
import lombok.Data;
/**
*
*
* @author fengshuonan
* @date 2021/5/19 10:37
*/
@Data
public class DatabaseInfoDTO {
/**
* jdbc
*/
private String jdbcDriver;
/**
* jdbcurl
*/
private String jdbcUrl;
/**
*
*/
private String username;
/**
*
*/
private String password;
}

View File

@ -3,7 +3,6 @@ package cn.stylefeng.roses.kernel.db.api.util;
import cn.hutool.core.util.StrUtil;
import cn.stylefeng.roses.kernel.db.api.exception.DaoException;
import cn.stylefeng.roses.kernel.db.api.exception.enums.DatabaseExceptionEnum;
import cn.stylefeng.roses.kernel.db.api.pojo.db.DatabaseInfoDTO;
import cn.stylefeng.roses.kernel.db.api.pojo.db.TableFieldInfo;
import cn.stylefeng.roses.kernel.db.api.pojo.db.TableInfo;
import cn.stylefeng.roses.kernel.db.api.pojo.druid.DruidProperties;
@ -34,21 +33,21 @@ public class DatabaseUtil {
* @author fengshuonan
* @date 2021/5/19 10:35
*/
public static List<TableInfo> selectTables(DatabaseInfoDTO dbInfo) {
public static List<TableInfo> selectTables(DruidProperties druidProperties) {
List<TableInfo> tables = new ArrayList<>();
try {
Class.forName(dbInfo.getJdbcDriver());
Class.forName(druidProperties.getDriverClassName());
Connection conn = DriverManager.getConnection(
dbInfo.getJdbcUrl(), dbInfo.getUsername(), dbInfo.getPassword());
druidProperties.getUrl(), druidProperties.getUsername(), druidProperties.getPassword());
// 获取数据库名称
String dbName = getDbName(dbInfo);
String dbName = getDbName(druidProperties);
// 构造查询语句
PreparedStatement preparedStatement = conn.prepareStatement(new TableListSql().getSql(dbInfo.getJdbcUrl()));
PreparedStatement preparedStatement = conn.prepareStatement(new TableListSql().getSql(druidProperties.getUrl()));
// 拼接设置数据库名称
if (!dbInfo.getJdbcUrl().contains("sqlserver") && !dbInfo.getJdbcUrl().contains("postgresql")) {
if (!druidProperties.getUrl().contains("sqlserver") && !druidProperties.getUrl().contains("postgresql")) {
preparedStatement.setString(1, dbName);
}
@ -73,25 +72,25 @@ public class DatabaseUtil {
*
*
* @author fengshuonan
* @Date 2019-05-04 20:31
* @date 2021/5/19 11:01
*/
public static List<TableFieldInfo> getTableFields(DatabaseInfoDTO dbInfo, String tableName) {
public static List<TableFieldInfo> getTableFields(DruidProperties druidProperties, String tableName) {
ArrayList<TableFieldInfo> fieldList = new ArrayList<>();
try {
Class.forName(dbInfo.getJdbcDriver());
Class.forName(druidProperties.getDriverClassName());
Connection conn = DriverManager.getConnection(
dbInfo.getJdbcUrl(), dbInfo.getUsername(), dbInfo.getPassword());
druidProperties.getUrl(), druidProperties.getUsername(), druidProperties.getPassword());
PreparedStatement preparedStatement = conn.prepareStatement(new TableFieldListSql().getSql(dbInfo.getJdbcUrl()));
PreparedStatement preparedStatement = conn.prepareStatement(new TableFieldListSql().getSql(druidProperties.getUrl()));
if (dbInfo.getJdbcUrl().contains("oracle")) {
if (druidProperties.getUrl().contains("oracle")) {
preparedStatement.setString(1, tableName);
} else if (dbInfo.getJdbcUrl().contains("postgresql")) {
} else if (druidProperties.getUrl().contains("postgresql")) {
preparedStatement.setString(1, tableName);
} else if (dbInfo.getJdbcUrl().contains("sqlserver")) {
} else if (druidProperties.getUrl().contains("sqlserver")) {
preparedStatement.setString(1, tableName);
} else {
String dbName = getDbName(dbInfo);
String dbName = getDbName(druidProperties);
preparedStatement.setString(1, tableName);
preparedStatement.setString(2, dbName);
}
@ -147,32 +146,33 @@ public class DatabaseUtil {
* @author fengshuonan
* @date 2021/5/19 10:39
*/
private static String getDbName(DatabaseInfoDTO dbInfo) {
private static String getDbName(DruidProperties druidProperties) {
if (dbInfo.getJdbcUrl().contains("oracle")) {
if (druidProperties.getUrl().contains("oracle")) {
// 如果是oracle直接返回username
return dbInfo.getUsername();
return druidProperties.getUsername();
} else if (dbInfo.getJdbcUrl().contains("postgresql")) {
} else if (druidProperties.getUrl().contains("postgresql")) {
// postgresql直接返回最后一个/后边的字符
int first = dbInfo.getJdbcUrl().lastIndexOf("/") + 1;
return dbInfo.getJdbcUrl().substring(first);
int first = druidProperties.getUrl().lastIndexOf("/") + 1;
return druidProperties.getUrl().substring(first);
} else if (dbInfo.getJdbcUrl().contains("sqlserver")) {
} else if (druidProperties.getUrl().contains("sqlserver")) {
// sqlserver直接返回最后一个=后边的字符
int first = dbInfo.getJdbcUrl().lastIndexOf("=") + 1;
return dbInfo.getJdbcUrl().substring(first);
int first = druidProperties.getUrl().lastIndexOf("=") + 1;
return druidProperties.getUrl().substring(first);
} else {
// mysql返回/和?之间的字符
String jdbcUrl = dbInfo.getJdbcUrl();
String jdbcUrl = druidProperties.getUrl();
int first = jdbcUrl.lastIndexOf("/") + 1;
int last = jdbcUrl.indexOf("?");
return jdbcUrl.substring(first, last);
}
}
}

View File

@ -32,5 +32,12 @@
<version>${roses.version}</version>
</dependency>
<!--数据源容器的api-->
<dependency>
<groupId>cn.stylefeng.roses</groupId>
<artifactId>ds-container-api</artifactId>
<version>${roses.version}</version>
</dependency>
</dependencies>
</project>

View File

@ -29,6 +29,10 @@ import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;
import cn.stylefeng.roses.kernel.db.api.exception.enums.DbInitEnum;
import cn.stylefeng.roses.kernel.db.api.pojo.db.TableFieldInfo;
import cn.stylefeng.roses.kernel.db.api.pojo.db.TableInfo;
import cn.stylefeng.roses.kernel.db.api.pojo.druid.DruidProperties;
import cn.stylefeng.roses.kernel.db.api.util.DatabaseUtil;
import cn.stylefeng.roses.kernel.db.init.util.SqlExe;
import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
import com.alibaba.fastjson.JSON;
@ -36,10 +40,10 @@ import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
@ -66,6 +70,9 @@ public abstract class DbInitializer {
this.fieldValidatorExceptionFlag = fieldValidatorExceptionFlag;
}
@Resource
private DruidProperties druidProperties;
/**
*
*
@ -89,7 +96,7 @@ public abstract class DbInitializer {
*/
private void initTable() {
//校验参数
// 校验参数
String tableName = this.getTableName();
String tableInitSql = this.getTableInitSql();
if (ObjectUtil.isEmpty(tableName) || ObjectUtil.isEmpty(tableInitSql)) {
@ -98,16 +105,17 @@ public abstract class DbInitializer {
}
}
//列出数据库中所有的表
List<Map<String, Object>> tables = SqlExe.selectList("SHOW TABLES");
// 列出数据库中所有的表
List<TableInfo> tableInfos = DatabaseUtil.selectTables(druidProperties);
boolean haveSmsTableFlag = false;
for (Map<String, Object> tableInfo : tables) {
if (tableInfo.containsValue(tableName.toUpperCase()) || tableInfo.containsValue(tableName.toLowerCase())) {
for (TableInfo tableInfo : tableInfos) {
if (tableInfo.getTableName().equalsIgnoreCase(tableName)) {
haveSmsTableFlag = true;
break;
}
}
//判断数据库中是否有这张表,如果没有就初始化
// 判断数据库中是否有这张表,如果没有就初始化
if (!haveSmsTableFlag) {
SqlExe.update(tableInitSql);
log.info("初始化" + getTableName() + "成功!");
@ -123,16 +131,8 @@ public abstract class DbInitializer {
*/
private void fieldsValidate() {
//校验参数
String sql = this.showColumnsSql();
if (ObjectUtil.isEmpty(sql)) {
if (fieldValidatorExceptionFlag) {
throw new ServiceException(DbInitEnum.INIT_TABLE_EMPTY_PARAMS);
}
}
//检查数据库中的字段,是否和实体字段一致
List<Map<String, Object>> tableFields = SqlExe.selectList(sql);
List<TableFieldInfo> tableFields = DatabaseUtil.getTableFields(druidProperties, getTableName());
if (tableFields != null && !tableFields.isEmpty()) {
//用于保存实体中不存在的字段的名称集合
@ -140,11 +140,8 @@ public abstract class DbInitializer {
//反射获取字段的所有字段名称
List<String> classFields = this.getClassFields();
for (Map<String, Object> tableField : tableFields) {
String fieldName = (String) tableField.get("COLUMN_NAME");
if (fieldName == null) {
fieldName = (String) tableField.get("Field");
}
for (TableFieldInfo tableField : tableFields) {
String fieldName = tableField.getColumnName();
if (!classFields.contains(fieldName.toLowerCase())) {
fieldsNotInClass.add(fieldName);
}
@ -185,16 +182,6 @@ public abstract class DbInitializer {
return filedNamesUnderlineCase;
}
/**
*
*
* @author stylefeng
* @date 2018/7/29 22:49
*/
private String showColumnsSql() {
return "SHOW COLUMNS FROM " + this.getTableName();
}
/**
*
*