This commit is contained in:
2023-06-06 17:52:07 +08:00
parent bd2c01fe40
commit 7f9c2a64c1
12 changed files with 383 additions and 243 deletions

View File

@ -0,0 +1,62 @@
package com.qiaoba.common.base.enums;
import cn.hutool.core.util.StrUtil;
import lombok.AllArgsConstructor;
import lombok.Getter;
/**
* 数据库类型
*
* @author ailanyin
* @version 1.0
* @since 2023/5/22 16:41
*/
@Getter
@AllArgsConstructor
public enum DataBaseEnum {
/**
* MySQL
*/
MY_SQL("MySQL",
"jdbc:mysql://localhost:3306/{}?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8&autoReconnect=true&rewriteBatchedStatements=true",
"com.mysql.cj.jdbc.Driver"),
/**
* Oracle
*/
ORACLE("Oracle",
"jdbc:oracle:thin:@//localhost:1521/{}",
"oracle.jdbc.OracleDriver"),
/**
* PostgreSQL
*/
POSTGRE_SQL("PostgreSQL",
"jdbc:postgresql://localhost:5432/{}?useUnicode=true&characterEncoding=utf8&useSSL=true&autoReconnect=true&reWriteBatchedInserts=true",
"org.postgresql.Driver"),
/**
* SQL Server
*/
SQL_SERVER("Microsoft SQL Server",
"jdbc:sqlserver://localhost:1433;DatabaseName={};SelectMethod=cursor;encrypt=false;rewriteBatchedStatements=true",
"com.microsoft.sqlserver.jdbc.SQLServerDriver");
private final String type;
private final String url;
private final String driver;
public static DataBaseEnum find(String databaseProductName) {
if (StrUtil.isBlank(databaseProductName)) {
return null;
}
for (DataBaseEnum type : values()) {
if (type.getType().equals(databaseProductName)) {
return type;
}
}
return null;
}
}

View File

@ -1,52 +0,0 @@
package com.qiaoba.common.base.enums;
import cn.hutool.core.util.StrUtil;
import lombok.AllArgsConstructor;
import lombok.Getter;
/**
* 数据库类型
*
* @author ailanyin
* @version 1.0
* @since 2023/5/22 16:41
*/
@Getter
@AllArgsConstructor
public enum DataBaseTypeEnum {
/**
* MySQL
*/
MY_SQL("MySQL"),
/**
* Oracle
*/
ORACLE("Oracle"),
/**
* PostgreSQL
*/
POSTGRE_SQL("PostgreSQL"),
/**
* SQL Server
*/
SQL_SERVER("Microsoft SQL Server");
private final String type;
public static DataBaseTypeEnum find(String databaseProductName) {
if (StrUtil.isBlank(databaseProductName)) {
return null;
}
for (DataBaseTypeEnum type : values()) {
if (type.getType().equals(databaseProductName)) {
return type;
}
}
return null;
}
}

View File

@ -2,7 +2,7 @@ package com.qiaoba.common.base.utils;
import cn.hutool.core.convert.Convert;
import com.qiaoba.common.base.context.BaseContext;
import com.qiaoba.common.base.enums.DataBaseTypeEnum;
import com.qiaoba.common.base.enums.DataBaseEnum;
import lombok.extern.slf4j.Slf4j;
/**
@ -23,15 +23,15 @@ public class DatabaseUtil {
* @return 处理后的sql
*/
public static String handleFindInSet(Object var1, String var2) {
DataBaseTypeEnum dataBaseType = DataBaseTypeEnum.find(BaseContext.getDatabaseType());
DataBaseEnum dataBaseType = DataBaseEnum.find(BaseContext.getDatabaseType());
String var = Convert.toStr(var1);
if (dataBaseType == DataBaseTypeEnum.SQL_SERVER) {
if (dataBaseType == DataBaseEnum.SQL_SERVER) {
// charindex(',100,' , ',0,100,101,') <> 0
return "charindex('," + var + ",' , ','+" + var2 + "+',') <> 0";
} else if (dataBaseType == DataBaseTypeEnum.POSTGRE_SQL) {
} else if (dataBaseType == DataBaseEnum.POSTGRE_SQL) {
// (select position(',100,' in ',0,100,101,')) <> 0
return "(select position('," + var + ",' in ','||" + var2 + "||',')) <> 0";
} else if (dataBaseType == DataBaseTypeEnum.ORACLE) {
} else if (dataBaseType == DataBaseEnum.ORACLE) {
// instr(',0,100,101,' , ',100,') <> 0
return "instr(','||" + var2 + "||',' , '," + var + ",') <> 0";
}

View File

@ -21,7 +21,7 @@ public class DynamicDataSource {
/**
* 租户Code
*/
private String tenantCode;
private String tenantId;
/**
* 数据库-url

View File

@ -45,4 +45,16 @@ public class JdbcUtil {
}
}
}
public static Connection getConnection(String driver, String url, String username, String password) {
Connection conn = null;
try {
Class.forName(driver);
//建立连接
conn = DriverManager.getConnection(url, username, password);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
}

View File

@ -24,14 +24,17 @@ public class IpConfig {
@PostConstruct
public void dbToCache() {
InputStream inputStream = null;
try {
ClassPathResource classPathResource = new ClassPathResource("ip2region.xdb");
InputStream inputStream = classPathResource.getInputStream();
inputStream = classPathResource.getInputStream();
IpUtil.setSearcher(Searcher.newWithBuffer(IoUtil.read(inputStream).toByteArray()));
log.info("加载IP离线库到内存成功");
} catch (Exception e) {
e.printStackTrace();
log.error("加载IP离线库到内存失败, 请联系管理员!");
} finally {
IoUtil.close(inputStream);
}
}