add
This commit is contained in:
@ -0,0 +1,40 @@
|
||||
package com.qiaoba.common.database.handlers.schema;
|
||||
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.qiaoba.common.base.context.BaseContext;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
|
||||
/**
|
||||
* MysqlSchemaHandler
|
||||
*
|
||||
* @author ailanyin
|
||||
* @version 1.0
|
||||
* @since 2023/6/20 9:25
|
||||
*/
|
||||
@Component
|
||||
public class MysqlSchemaHandler implements SchemaHandler {
|
||||
|
||||
@Value("${spring.application.name}")
|
||||
private String baseDatabase;
|
||||
|
||||
@Override
|
||||
public void setSchema(Connection conn) {
|
||||
// eg: use qiaoba-1;
|
||||
String sql = StrUtil.format("use `{}-{}`;", baseDatabase, BaseContext.getTenantId());
|
||||
Statement statement = null;
|
||||
try {
|
||||
statement = conn.createStatement();
|
||||
statement.execute(sql);
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
} finally {
|
||||
IoUtil.close(statement);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
package com.qiaoba.common.database.handlers.schema;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
|
||||
/**
|
||||
* PostgreSqlSchemaHandler
|
||||
*
|
||||
* @author ailanyin
|
||||
* @version 1.0
|
||||
* @since 2023/6/20 9:29
|
||||
*/
|
||||
@Component
|
||||
public class PostgreSqlSchemaHandler implements SchemaHandler {
|
||||
@Override
|
||||
public void setSchema(Connection conn) {
|
||||
try {
|
||||
conn.setSchema("");
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
package com.qiaoba.common.database.handlers.schema;
|
||||
|
||||
import java.sql.Connection;
|
||||
|
||||
/**
|
||||
* Schema处理器
|
||||
*
|
||||
* @author ailanyin
|
||||
* @version 1.0
|
||||
* @since 2023/6/20 9:18
|
||||
*/
|
||||
public interface SchemaHandler {
|
||||
|
||||
/**
|
||||
* 设置/切换schema
|
||||
*
|
||||
* @param conn conn
|
||||
*/
|
||||
void setSchema(Connection conn);
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
package com.qiaoba.common.database.handlers.schema;
|
||||
|
||||
import com.qiaoba.common.base.enums.DataBaseEnum;
|
||||
import com.qiaoba.common.base.exceptions.ServiceException;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* SchemaHandlerFactory-用于选择哪种类型处理器
|
||||
*
|
||||
* @author ailanyin
|
||||
* @version 1.0
|
||||
* @since 2023/6/20 9:22
|
||||
*/
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class SchemaHandlerFactory {
|
||||
|
||||
private static Map<String, SchemaHandler> handlerMap = new ConcurrentHashMap<>();
|
||||
|
||||
private final MysqlSchemaHandler mysqlSchemaHandler;
|
||||
|
||||
@PostConstruct
|
||||
public void register() {
|
||||
handlerMap.put(DataBaseEnum.MY_SQL.getType(), mysqlSchemaHandler);
|
||||
}
|
||||
|
||||
public static SchemaHandler getHandler(String name) {
|
||||
SchemaHandler schemaHandler = handlerMap.get(name);
|
||||
if (Objects.isNull(schemaHandler)) {
|
||||
throw new ServiceException("Schema处理器工厂异常, 类型:[" + name + "]找不到相对应的解析器");
|
||||
}
|
||||
return schemaHandler;
|
||||
}
|
||||
}
|
@ -4,6 +4,7 @@ import cn.hutool.core.io.IoUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor;
|
||||
import com.qiaoba.common.base.context.BaseContext;
|
||||
import com.qiaoba.common.database.handlers.schema.SchemaHandlerFactory;
|
||||
import org.apache.ibatis.executor.statement.StatementHandler;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
|
||||
@ -21,24 +22,13 @@ import java.util.Objects;
|
||||
*/
|
||||
public class SchemaInterceptor implements InnerInterceptor {
|
||||
|
||||
@Value("${spring.application.name}")
|
||||
private String baseDatabase;
|
||||
|
||||
|
||||
@Override
|
||||
public void beforePrepare(StatementHandler sh, Connection conn, Integer transactionTimeout) {
|
||||
|
||||
if (Objects.nonNull(BaseContext.isSchemaMode()) && BaseContext.isSchemaMode()) {
|
||||
// eg: use qiaoba-1;
|
||||
String sql = StrUtil.format("use `{}-{}`;", baseDatabase, BaseContext.getTenantId());
|
||||
Statement statement = null;
|
||||
try {
|
||||
statement = conn.createStatement();
|
||||
statement.execute(sql);
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
} finally {
|
||||
IoUtil.close(statement);
|
||||
}
|
||||
SchemaHandlerFactory.getHandler(BaseContext.getDatabaseType()).setSchema(conn);
|
||||
}
|
||||
InnerInterceptor.super.beforePrepare(sh, conn, transactionTimeout);
|
||||
}
|
||||
|
@ -5,8 +5,7 @@ import cn.hutool.core.util.StrUtil;
|
||||
import com.qiaoba.common.base.exceptions.ServiceException;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.*;
|
||||
|
||||
/**
|
||||
* JdbcUtil
|
||||
@ -53,4 +52,25 @@ public class JdbcUtil {
|
||||
throw new ServiceException(StrUtil.format("数据源连接失败,错误: {}", e.getMessage()));
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Connection connection = null;
|
||||
Statement statement = null;
|
||||
ResultSet resultSet = null;
|
||||
try {
|
||||
connection = getConnection("oracle.jdbc.OracleDriver", "jdbc:oracle:thin:@//101.34.251.155:1521/ORCL", "ROOT", "root");
|
||||
statement = connection.createStatement();
|
||||
statement.executeQuery("alter session set current_schema=ROOT");
|
||||
resultSet = statement.executeQuery("SELECT * FROM SYS_USER");
|
||||
while (resultSet.next()) {
|
||||
System.out.println(resultSet.getString("NICKNAME"));
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
IoUtil.close(connection);
|
||||
IoUtil.close(statement);
|
||||
IoUtil.close(resultSet);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user