mirror of https://gitee.com/stylefeng/roses
【7.0.4】整理DbInitializer
parent
43f9fc3236
commit
3a56a63ee2
|
@ -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;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* jdbc的url
|
|
||||||
*/
|
|
||||||
private String jdbcUrl;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 数据库连接的账号
|
|
||||||
*/
|
|
||||||
private String username;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 数据库连接密码
|
|
||||||
*/
|
|
||||||
private String password;
|
|
||||||
|
|
||||||
}
|
|
|
@ -3,7 +3,6 @@ package cn.stylefeng.roses.kernel.db.api.util;
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
import cn.stylefeng.roses.kernel.db.api.exception.DaoException;
|
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.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.TableFieldInfo;
|
||||||
import cn.stylefeng.roses.kernel.db.api.pojo.db.TableInfo;
|
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.pojo.druid.DruidProperties;
|
||||||
|
@ -34,21 +33,21 @@ public class DatabaseUtil {
|
||||||
* @author fengshuonan
|
* @author fengshuonan
|
||||||
* @date 2021/5/19 10:35
|
* @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<>();
|
List<TableInfo> tables = new ArrayList<>();
|
||||||
try {
|
try {
|
||||||
Class.forName(dbInfo.getJdbcDriver());
|
Class.forName(druidProperties.getDriverClassName());
|
||||||
Connection conn = DriverManager.getConnection(
|
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);
|
preparedStatement.setString(1, dbName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -73,25 +72,25 @@ public class DatabaseUtil {
|
||||||
* 查询某个表的所有字段
|
* 查询某个表的所有字段
|
||||||
*
|
*
|
||||||
* @author fengshuonan
|
* @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<>();
|
ArrayList<TableFieldInfo> fieldList = new ArrayList<>();
|
||||||
try {
|
try {
|
||||||
Class.forName(dbInfo.getJdbcDriver());
|
Class.forName(druidProperties.getDriverClassName());
|
||||||
Connection conn = DriverManager.getConnection(
|
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);
|
preparedStatement.setString(1, tableName);
|
||||||
} else if (dbInfo.getJdbcUrl().contains("postgresql")) {
|
} else if (druidProperties.getUrl().contains("postgresql")) {
|
||||||
preparedStatement.setString(1, tableName);
|
preparedStatement.setString(1, tableName);
|
||||||
} else if (dbInfo.getJdbcUrl().contains("sqlserver")) {
|
} else if (druidProperties.getUrl().contains("sqlserver")) {
|
||||||
preparedStatement.setString(1, tableName);
|
preparedStatement.setString(1, tableName);
|
||||||
} else {
|
} else {
|
||||||
String dbName = getDbName(dbInfo);
|
String dbName = getDbName(druidProperties);
|
||||||
preparedStatement.setString(1, tableName);
|
preparedStatement.setString(1, tableName);
|
||||||
preparedStatement.setString(2, dbName);
|
preparedStatement.setString(2, dbName);
|
||||||
}
|
}
|
||||||
|
@ -147,32 +146,33 @@ public class DatabaseUtil {
|
||||||
* @author fengshuonan
|
* @author fengshuonan
|
||||||
* @date 2021/5/19 10:39
|
* @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
|
// 如果是oracle,直接返回username
|
||||||
return dbInfo.getUsername();
|
return druidProperties.getUsername();
|
||||||
|
|
||||||
} else if (dbInfo.getJdbcUrl().contains("postgresql")) {
|
} else if (druidProperties.getUrl().contains("postgresql")) {
|
||||||
|
|
||||||
// postgresql,直接返回最后一个/后边的字符
|
// postgresql,直接返回最后一个/后边的字符
|
||||||
int first = dbInfo.getJdbcUrl().lastIndexOf("/") + 1;
|
int first = druidProperties.getUrl().lastIndexOf("/") + 1;
|
||||||
return dbInfo.getJdbcUrl().substring(first);
|
return druidProperties.getUrl().substring(first);
|
||||||
|
|
||||||
} else if (dbInfo.getJdbcUrl().contains("sqlserver")) {
|
} else if (druidProperties.getUrl().contains("sqlserver")) {
|
||||||
|
|
||||||
// sqlserver,直接返回最后一个=后边的字符
|
// sqlserver,直接返回最后一个=后边的字符
|
||||||
int first = dbInfo.getJdbcUrl().lastIndexOf("=") + 1;
|
int first = druidProperties.getUrl().lastIndexOf("=") + 1;
|
||||||
return dbInfo.getJdbcUrl().substring(first);
|
return druidProperties.getUrl().substring(first);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
// mysql,返回/和?之间的字符
|
// mysql,返回/和?之间的字符
|
||||||
String jdbcUrl = dbInfo.getJdbcUrl();
|
String jdbcUrl = druidProperties.getUrl();
|
||||||
int first = jdbcUrl.lastIndexOf("/") + 1;
|
int first = jdbcUrl.lastIndexOf("/") + 1;
|
||||||
int last = jdbcUrl.indexOf("?");
|
int last = jdbcUrl.indexOf("?");
|
||||||
return jdbcUrl.substring(first, last);
|
return jdbcUrl.substring(first, last);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,5 +32,12 @@
|
||||||
<version>${roses.version}</version>
|
<version>${roses.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!--数据源容器的api-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>cn.stylefeng.roses</groupId>
|
||||||
|
<artifactId>ds-container-api</artifactId>
|
||||||
|
<version>${roses.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</project>
|
</project>
|
||||||
|
|
|
@ -29,6 +29,10 @@ import cn.hutool.core.util.ObjectUtil;
|
||||||
import cn.hutool.core.util.ReflectUtil;
|
import cn.hutool.core.util.ReflectUtil;
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
import cn.stylefeng.roses.kernel.db.api.exception.enums.DbInitEnum;
|
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.db.init.util.SqlExe;
|
||||||
import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
|
import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
|
||||||
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSON;
|
||||||
|
@ -36,10 +40,10 @@ import lombok.Getter;
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -66,6 +70,9 @@ public abstract class DbInitializer {
|
||||||
this.fieldValidatorExceptionFlag = fieldValidatorExceptionFlag;
|
this.fieldValidatorExceptionFlag = fieldValidatorExceptionFlag;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private DruidProperties druidProperties;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 初始化数据库
|
* 初始化数据库
|
||||||
*
|
*
|
||||||
|
@ -89,7 +96,7 @@ public abstract class DbInitializer {
|
||||||
*/
|
*/
|
||||||
private void initTable() {
|
private void initTable() {
|
||||||
|
|
||||||
//校验参数
|
// 校验参数
|
||||||
String tableName = this.getTableName();
|
String tableName = this.getTableName();
|
||||||
String tableInitSql = this.getTableInitSql();
|
String tableInitSql = this.getTableInitSql();
|
||||||
if (ObjectUtil.isEmpty(tableName) || ObjectUtil.isEmpty(tableInitSql)) {
|
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;
|
boolean haveSmsTableFlag = false;
|
||||||
for (Map<String, Object> tableInfo : tables) {
|
for (TableInfo tableInfo : tableInfos) {
|
||||||
if (tableInfo.containsValue(tableName.toUpperCase()) || tableInfo.containsValue(tableName.toLowerCase())) {
|
if (tableInfo.getTableName().equalsIgnoreCase(tableName)) {
|
||||||
haveSmsTableFlag = true;
|
haveSmsTableFlag = true;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//判断数据库中是否有这张表,如果没有就初始化
|
// 判断数据库中是否有这张表,如果没有就初始化
|
||||||
if (!haveSmsTableFlag) {
|
if (!haveSmsTableFlag) {
|
||||||
SqlExe.update(tableInitSql);
|
SqlExe.update(tableInitSql);
|
||||||
log.info("初始化" + getTableName() + "成功!");
|
log.info("初始化" + getTableName() + "成功!");
|
||||||
|
@ -123,16 +131,8 @@ public abstract class DbInitializer {
|
||||||
*/
|
*/
|
||||||
private void fieldsValidate() {
|
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()) {
|
if (tableFields != null && !tableFields.isEmpty()) {
|
||||||
|
|
||||||
//用于保存实体中不存在的字段的名称集合
|
//用于保存实体中不存在的字段的名称集合
|
||||||
|
@ -140,11 +140,8 @@ public abstract class DbInitializer {
|
||||||
|
|
||||||
//反射获取字段的所有字段名称
|
//反射获取字段的所有字段名称
|
||||||
List<String> classFields = this.getClassFields();
|
List<String> classFields = this.getClassFields();
|
||||||
for (Map<String, Object> tableField : tableFields) {
|
for (TableFieldInfo tableField : tableFields) {
|
||||||
String fieldName = (String) tableField.get("COLUMN_NAME");
|
String fieldName = tableField.getColumnName();
|
||||||
if (fieldName == null) {
|
|
||||||
fieldName = (String) tableField.get("Field");
|
|
||||||
}
|
|
||||||
if (!classFields.contains(fieldName.toLowerCase())) {
|
if (!classFields.contains(fieldName.toLowerCase())) {
|
||||||
fieldsNotInClass.add(fieldName);
|
fieldsNotInClass.add(fieldName);
|
||||||
}
|
}
|
||||||
|
@ -185,16 +182,6 @@ public abstract class DbInitializer {
|
||||||
return filedNamesUnderlineCase;
|
return filedNamesUnderlineCase;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取表的字段
|
|
||||||
*
|
|
||||||
* @author stylefeng
|
|
||||||
* @date 2018/7/29 22:49
|
|
||||||
*/
|
|
||||||
private String showColumnsSql() {
|
|
||||||
return "SHOW COLUMNS FROM " + this.getTableName();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取表的初始化语句
|
* 获取表的初始化语句
|
||||||
*
|
*
|
||||||
|
|
Loading…
Reference in New Issue