add
This commit is contained in:
@ -15,32 +15,32 @@ public enum DatasourceErrorCode {
|
||||
/**
|
||||
* 未找到
|
||||
*/
|
||||
NOT_FIND(50101, "未找到数据源信息"),
|
||||
NOT_FIND(50401, "未找到数据源信息"),
|
||||
|
||||
/**
|
||||
* 连接错误
|
||||
*/
|
||||
CONNECT_ERROR(50102, "数据源无法连接"),
|
||||
CONNECT_ERROR(50402, "数据源无法连接"),
|
||||
|
||||
/**
|
||||
* 切换模式错误
|
||||
*/
|
||||
SWITCH_SCHEMA_ERROR(50103, "切换模式错误"),
|
||||
SWITCH_SCHEMA_ERROR(50403, "切换模式错误"),
|
||||
|
||||
/**
|
||||
* 创建表错误
|
||||
*/
|
||||
CREATE_TABLE_ERROR(50104, "创建表错误"),
|
||||
CREATE_TABLE_ERROR(50404, "创建表错误"),
|
||||
|
||||
/**
|
||||
* 创建模式/库错误
|
||||
*/
|
||||
CREATE_SCHEMA_ERROR(50105, "创建模式/库错误"),
|
||||
CREATE_SCHEMA_ERROR(50405, "创建模式/库错误"),
|
||||
|
||||
/**
|
||||
* 初始化数据错误
|
||||
*/
|
||||
INIT_DATA_ERROR(50106, "初始化数据错误");
|
||||
INIT_DATA_ERROR(50406, "初始化数据错误");
|
||||
|
||||
private final Integer code;
|
||||
private final String msg;
|
||||
|
@ -16,6 +16,7 @@ public enum TenantErrorCode {
|
||||
* 未找到
|
||||
*/
|
||||
NOT_FIND(5010, "未找到租户信息"),
|
||||
|
||||
/**
|
||||
* 禁用
|
||||
*/
|
||||
@ -24,7 +25,12 @@ public enum TenantErrorCode {
|
||||
/**
|
||||
* 过期
|
||||
*/
|
||||
EXPIRE(5012, "租户已过期");
|
||||
EXPIRE(5012, "租户已过期"),
|
||||
|
||||
/**
|
||||
* 租户模式不存在
|
||||
*/
|
||||
MODE_NOT_FIND(50103, "未找到租户模式");
|
||||
|
||||
private final Integer code;
|
||||
private final String msg;
|
||||
|
@ -1,16 +1,12 @@
|
||||
package com.qiaoba.common.database.interceptors;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.db.sql.SqlExecutor;
|
||||
import com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor;
|
||||
import com.qiaoba.common.base.constants.BaseConstant;
|
||||
import com.qiaoba.common.base.constants.TenantConstant;
|
||||
import com.qiaoba.common.base.context.BaseContext;
|
||||
import com.qiaoba.common.database.context.TenantDbTypeContext;
|
||||
import com.qiaoba.common.database.properties.TenantSchema;
|
||||
import com.qiaoba.common.database.utils.DbUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.ibatis.executor.statement.StatementHandler;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
@ -26,17 +22,12 @@ import java.util.Objects;
|
||||
@Slf4j
|
||||
public class SchemaInterceptor implements InnerInterceptor {
|
||||
|
||||
@Value("${spring.application.name}")
|
||||
private String schemaPrefix;
|
||||
|
||||
@Override
|
||||
public void beforePrepare(StatementHandler sh, Connection conn, Integer transactionTimeout) {
|
||||
|
||||
if (Objects.nonNull(BaseContext.isSchemaMode()) && BaseContext.isSchemaMode()) {
|
||||
try {
|
||||
// schemaPrefix + '-' + tenantId
|
||||
// eg: schema = qiaoba-boot-2
|
||||
DbUtil.setSchema(TenantDbTypeContext.getDefault(), conn, schemaPrefix + BaseConstant.HYPHEN_JOIN_STR + BaseContext.getTenantId());
|
||||
DbUtil.setSchema(TenantDbTypeContext.getDefault(), conn, TenantSchema.getSchema(BaseContext.getTenantId()));
|
||||
} catch (SQLException e) {
|
||||
log.info("切换SCHEMA失败, 原因: {}", e.getMessage());
|
||||
}
|
||||
|
@ -0,0 +1,37 @@
|
||||
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;
|
||||
|
||||
/**
|
||||
* 租户Schema
|
||||
*
|
||||
* @author ailanyin
|
||||
* @version 1.0
|
||||
* @since 2023/6/29 8:53
|
||||
*/
|
||||
@Component
|
||||
public class TenantSchema {
|
||||
|
||||
|
||||
private static String schemaPrefix;
|
||||
|
||||
@Value("${spring.application.name}")
|
||||
public void setSchemaPrefix(String name) {
|
||||
schemaPrefix = name;
|
||||
}
|
||||
|
||||
/**
|
||||
* 租户schema生成逻辑
|
||||
* schemaPrefix + '-' + tenantId
|
||||
* eg: schema = abc-2
|
||||
*
|
||||
* @param tenantId 租户ID
|
||||
* @return schema
|
||||
*/
|
||||
public static String getSchema(String tenantId) {
|
||||
return schemaPrefix + BaseConstant.HYPHEN_JOIN_STR + tenantId;
|
||||
}
|
||||
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
package com.qiaoba.common.database.utils;
|
||||
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
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.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
|
||||
/**
|
||||
* @author ailanyin
|
||||
@ -40,4 +42,14 @@ public class DbUtil {
|
||||
scriptRunner.runScript(reader);
|
||||
reader.close();
|
||||
}
|
||||
|
||||
public static void runSql(Connection conn, String sql) throws SQLException {
|
||||
Statement statement = null;
|
||||
try {
|
||||
statement = conn.createStatement();
|
||||
statement.execute(sql);
|
||||
} finally {
|
||||
IoUtil.close(statement);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -8,7 +8,10 @@ import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.sql.*;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.Statement;
|
||||
|
||||
/**
|
||||
* JdbcUtil
|
||||
@ -100,19 +103,4 @@ public class JdbcUtil {
|
||||
private String msg;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Connection connection = getConnection("oracle.jdbc.OracleDriver", "jdbc:oracle:thin:@//192.168.0.205:1521/ORCL", "system", "root");
|
||||
try {
|
||||
DbUtil.setSchema("Oracle",connection,"scott");
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
connection.close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
|
@ -1,3 +1,4 @@
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
com.qiaoba.common.database.factories.DynamicDataSourceFactory,\
|
||||
com.qiaoba.common.database.properties.TenantSchema,\
|
||||
com.qiaoba.common.database.config.MybatisPlusConfig
|
||||
|
Reference in New Issue
Block a user