add
This commit is contained in:
@ -40,7 +40,12 @@ public enum DatasourceErrorCode {
|
||||
/**
|
||||
* 初始化数据错误
|
||||
*/
|
||||
INIT_DATA_ERROR(50406, "初始化数据错误");
|
||||
INIT_DATA_ERROR(50406, "初始化数据错误"),
|
||||
|
||||
/**
|
||||
* schema 不存在
|
||||
*/
|
||||
SCHEMA_NOT_EXIST_ERROR(50407, "SCHEMA: {} 不存在");
|
||||
|
||||
private final Integer code;
|
||||
private final String msg;
|
||||
|
@ -26,14 +26,6 @@ public enum DataBaseEnum {
|
||||
"com.mysql.cj.jdbc.Driver",
|
||||
"SELECT 1"),
|
||||
|
||||
/**
|
||||
* Oracle
|
||||
*/
|
||||
ORACLE("Oracle",
|
||||
"jdbc:oracle:thin:@//{}:{}/{}",
|
||||
"oracle.jdbc.OracleDriver",
|
||||
"SELECT 1 from DUAL"),
|
||||
|
||||
/**
|
||||
* PostgreSQL
|
||||
*/
|
||||
|
@ -32,9 +32,6 @@ public class DatabaseUtil {
|
||||
} else if (DataBaseEnum.POSTGRE_SQL.getType().equals(databaseType)) {
|
||||
// (select position(',100,' in ',0,100,101,')) <> 0
|
||||
return "(select position('," + var + ",' in ','||" + var2 + "||',')) <> 0";
|
||||
} else if (DataBaseEnum.ORACLE.getType().equals(databaseType)) {
|
||||
// instr(',0,100,101,' , ',100,') <> 0
|
||||
return "instr(','||" + var2 + "||',' , '," + var + ",') <> 0";
|
||||
}
|
||||
// find_in_set(100 , '0,100,101')
|
||||
return "find_in_set(" + var + " , " + var2 + ") <> 0";
|
||||
|
@ -29,10 +29,6 @@
|
||||
<groupId>org.postgresql</groupId>
|
||||
<artifactId>postgresql</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.oracle.database.jdbc</groupId>
|
||||
<artifactId>ojdbc8</artifactId>
|
||||
</dependency>
|
||||
<!-- mybatis plus -->
|
||||
<dependency>
|
||||
<groupId>com.baomidou</groupId>
|
||||
|
@ -170,6 +170,9 @@ public class DynamicDataSourceConfig {
|
||||
dataSource.setMaxActive(dynamicDataSource.getMaxActive());
|
||||
|
||||
try {
|
||||
dataSource.addFilters("stat");
|
||||
// wall 防火墙 切勿开启, 开启后 导入SQL 会失败
|
||||
// dataSource.addFilters("wall")
|
||||
// 初始化数据源
|
||||
dataSource.init();
|
||||
return dataSource;
|
||||
|
@ -62,4 +62,5 @@ public class MybatisPlusConfig {
|
||||
public IdentifierGenerator idGenerator() {
|
||||
return new DefaultIdentifierGenerator(NetUtil.getLocalhost());
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -3,9 +3,13 @@ package com.qiaoba.common.database.factories;
|
||||
import com.qiaoba.common.database.config.DynamicDataSourceConfig;
|
||||
import com.qiaoba.common.database.context.DynamicDataSourceContext;
|
||||
import com.qiaoba.common.database.properties.DataSourceProperties;
|
||||
import com.qiaoba.common.database.properties.TenantSchema;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
/**
|
||||
* 动态数据源工厂
|
||||
*
|
||||
@ -16,6 +20,13 @@ import org.springframework.context.annotation.Configuration;
|
||||
@Configuration
|
||||
public class DynamicDataSourceFactory {
|
||||
|
||||
@Value("${spring.application.name}")
|
||||
private String schemaPrefix;
|
||||
|
||||
@PostConstruct
|
||||
public void setSchemaPrefix() {
|
||||
TenantSchema.setSchemaPrefix(schemaPrefix);
|
||||
}
|
||||
@Bean
|
||||
public DynamicDataSourceContext dataSource() {
|
||||
return new DynamicDataSourceContext();
|
||||
|
@ -2,6 +2,7 @@ package com.qiaoba.common.database.interceptors;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor;
|
||||
import com.qiaoba.common.base.context.BaseContext;
|
||||
import com.qiaoba.common.base.enums.DataBaseEnum;
|
||||
import com.qiaoba.common.database.context.TenantDbTypeContext;
|
||||
import com.qiaoba.common.database.properties.TenantSchema;
|
||||
import com.qiaoba.common.database.utils.DbUtil;
|
||||
@ -25,11 +26,12 @@ public class SchemaInterceptor implements InnerInterceptor {
|
||||
@Override
|
||||
public void beforePrepare(StatementHandler sh, Connection conn, Integer transactionTimeout) {
|
||||
|
||||
// SCHEMA 模式
|
||||
if (Objects.nonNull(BaseContext.isSchemaMode()) && BaseContext.isSchemaMode()) {
|
||||
try {
|
||||
DbUtil.setSchema(TenantDbTypeContext.getDefault(), conn, TenantSchema.getSchema(BaseContext.getTenantId()));
|
||||
} catch (SQLException e) {
|
||||
log.info("切换SCHEMA失败, 原因: {}", e.getMessage());
|
||||
log.error("切换SCHEMA失败, 原因: {}", e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,6 @@
|
||||
package com.qiaoba.common.database.properties;
|
||||
|
||||
import com.qiaoba.common.base.constants.BaseConstant;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
@ -17,8 +16,7 @@ public class TenantSchema {
|
||||
|
||||
private static String schemaPrefix;
|
||||
|
||||
@Value("${spring.application.name}")
|
||||
public void setSchemaPrefix(String name) {
|
||||
public static void setSchemaPrefix(String name) {
|
||||
schemaPrefix = name;
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,7 @@
|
||||
package com.qiaoba.common.database.utils;
|
||||
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.qiaoba.common.base.enums.DataBaseEnum;
|
||||
import org.apache.ibatis.jdbc.ScriptRunner;
|
||||
import org.postgresql.jdbc.PgConnection;
|
||||
@ -10,6 +11,7 @@ import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.sql.Connection;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
|
||||
@ -20,12 +22,16 @@ import java.sql.Statement;
|
||||
*/
|
||||
public class DbUtil {
|
||||
|
||||
/**
|
||||
* PgSQL 查询 schema 是否存在
|
||||
*/
|
||||
private static final String PGSQL_CHECK_SCHEMA_EXIST_SQL = "SELECT count(1) FROM information_schema.schemata WHERE schema_name = '{}'";
|
||||
|
||||
|
||||
public static void setSchema(String dbType, Connection conn, String schema) throws SQLException {
|
||||
|
||||
if (DataBaseEnum.MY_SQL.getType().equals(dbType)) {
|
||||
conn.setSchema(schema);
|
||||
} else if (DataBaseEnum.ORACLE.getType().equals(dbType)) {
|
||||
conn.setSchema(schema);
|
||||
} else if (DataBaseEnum.POSTGRE_SQL.getType().equals(dbType)) {
|
||||
conn.unwrap(PgConnection.class).setSchema(schema);
|
||||
} else if (DataBaseEnum.SQL_SERVER.getType().equals(dbType)) {
|
||||
@ -33,6 +39,23 @@ public class DbUtil {
|
||||
}
|
||||
}
|
||||
|
||||
public static Boolean checkSchema(String dbType, Connection conn, String schema) throws Exception {
|
||||
if (DataBaseEnum.POSTGRE_SQL.getType().equals(dbType)) {
|
||||
Statement statement = null;
|
||||
try {
|
||||
statement = conn.createStatement();
|
||||
ResultSet resultSet = statement.executeQuery(StrUtil.format(PGSQL_CHECK_SCHEMA_EXIST_SQL, schema));
|
||||
resultSet.next();
|
||||
return resultSet.getInt(1) > 0;
|
||||
|
||||
} finally {
|
||||
IoUtil.close(statement);
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static void runScript(Connection conn, String filePath) throws IOException {
|
||||
ClassPathResource resource = new ClassPathResource(filePath);
|
||||
File file = resource.getFile();
|
||||
@ -53,4 +76,21 @@ public class DbUtil {
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Connection connection = null;
|
||||
try {
|
||||
connection = JdbcUtil.getConnection(
|
||||
DataBaseEnum.getDriver("Oracle"),
|
||||
"jdbc:oracle:thin:@//192.168.0.205:1521/ORCL",
|
||||
"system",
|
||||
"root"
|
||||
);
|
||||
setSchema("Oracle", connection, "QIAOBA1");
|
||||
runSql(connection, "delete FROM SYS_USER where user_id = '1'");
|
||||
} catch (SQLException e) {
|
||||
System.out.println(e.getMessage());
|
||||
} finally {
|
||||
IoUtil.close(connection);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -4,8 +4,6 @@ import cn.hutool.core.io.IoUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.qiaoba.common.base.enums.DataBaseEnum;
|
||||
import com.qiaoba.common.base.exceptions.ServiceException;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.sql.Connection;
|
||||
@ -71,36 +69,22 @@ 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);
|
||||
return conn;
|
||||
return DriverManager.getConnection(url, username, password);
|
||||
} catch (Exception e) {
|
||||
throw new ServiceException(StrUtil.format("数据源连接失败,错误: {}", e.getMessage()));
|
||||
}
|
||||
}
|
||||
|
||||
public static CheckResult check(String driver, String url, String username, String password) {
|
||||
Connection conn = null;
|
||||
try {
|
||||
Class.forName(driver);
|
||||
//建立连接
|
||||
conn = DriverManager.getConnection(url, username, password);
|
||||
return new CheckResult(true, null);
|
||||
} catch (Exception e) {
|
||||
return new CheckResult(false, e.getMessage());
|
||||
} finally {
|
||||
IoUtil.close(conn);
|
||||
}
|
||||
public static Connection connection(String driver, String url, String username, String password) throws Exception {
|
||||
Class.forName(driver);
|
||||
return DriverManager.getConnection(url, username, password);
|
||||
}
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public static class CheckResult {
|
||||
private boolean result;
|
||||
private String msg;
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
Connection connection = getConnection("oracle.jdbc.OracleDriver", "jdbc:oracle:thin:@//192.168.0.205:1521/ORCL", "QIAOBA-BOOT-1", "123456");
|
||||
DbUtil.runSql(connection, "delete from sys_post where post_id = '1'");
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,3 @@
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
com.qiaoba.common.database.factories.DynamicDataSourceFactory,\
|
||||
com.qiaoba.common.database.properties.TenantSchema,\
|
||||
com.qiaoba.common.database.config.MybatisPlusConfig
|
||||
|
@ -20,6 +20,7 @@ public class ResponseUtil {
|
||||
public static void response(HttpServletResponse response, String msg) throws IOException {
|
||||
response.setStatus(HttpServletResponse.SC_OK);
|
||||
response.setContentType(ContentType.JSON.getValue());
|
||||
response.setCharacterEncoding("UTF-8");
|
||||
PrintWriter writer = response.getWriter();
|
||||
writer.write(msg);
|
||||
writer.close();
|
||||
|
Reference in New Issue
Block a user