add
This commit is contained in:
@ -20,5 +20,9 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-validation</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-aop</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
@ -19,6 +19,11 @@ public class BaseConstant {
|
||||
*/
|
||||
public static final String DEFAULT_SPLIT_STR = ",";
|
||||
|
||||
/**
|
||||
* 竖线拼接符号: '|'(英文竖线)
|
||||
*/
|
||||
public static final String LINE_JOIN_STR = "|";
|
||||
|
||||
/**
|
||||
* 树的key的命名
|
||||
*/
|
||||
|
@ -0,0 +1,43 @@
|
||||
package com.qiaoba.common.base.context;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 全局上下文对象
|
||||
*
|
||||
* @author ailanyin
|
||||
* @version 1.0
|
||||
* @since 2023/5/22 17:24
|
||||
*/
|
||||
public class BaseContext {
|
||||
|
||||
private static final String DATABASE_TYPE_MAP_KEY = "databaseType";
|
||||
|
||||
private static final ThreadLocal<Map<String, Object>> CONTEXT_HOLDER = new ThreadLocal<>();
|
||||
|
||||
/**
|
||||
* 获取上下文中的数据源
|
||||
*/
|
||||
public String getDatabaseType() {
|
||||
return CONTEXT_HOLDER.get().get(DATABASE_TYPE_MAP_KEY).toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置上下文中的数据源
|
||||
*/
|
||||
public void setDatabaseType(String type) {
|
||||
Map<String, Object> map = new HashMap<>(1);
|
||||
map.put(DATABASE_TYPE_MAP_KEY, type);
|
||||
CONTEXT_HOLDER.set(map);
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除上下文中的数据源
|
||||
*/
|
||||
public void clearDataSource() {
|
||||
CONTEXT_HOLDER.remove();
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
package com.qiaoba.common.database.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;
|
||||
}
|
||||
}
|
@ -3,6 +3,7 @@ package com.qiaoba.common.database.filters;
|
||||
import com.qiaoba.common.base.utils.TenantUtil;
|
||||
import com.qiaoba.common.database.config.DynamicDataSourceContext;
|
||||
import com.qiaoba.common.database.constants.DynamicDatasourceConstant;
|
||||
import com.qiaoba.common.database.utils.DatabaseUtil;
|
||||
import com.qiaoba.common.web.utils.ResponseUtil;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.stereotype.Component;
|
||||
@ -36,6 +37,7 @@ public class DynamicDataSourceFilter extends OncePerRequestFilter {
|
||||
dynamicDataSourceContext.setDataSource(DynamicDatasourceConstant.DEFAULT_MASTER_DATASOURCE_KEY);
|
||||
// todo
|
||||
TenantUtil.setTenantId("1");
|
||||
DatabaseUtil.handleFindInSet();
|
||||
filterChain.doFilter(request, response);
|
||||
dynamicDataSourceContext.clearDataSource();
|
||||
TenantUtil.clearTenantId();
|
||||
|
@ -0,0 +1,55 @@
|
||||
package com.qiaoba.common.database.utils;
|
||||
|
||||
import cn.hutool.core.convert.Convert;
|
||||
import com.qiaoba.common.base.exceptions.ServiceException;
|
||||
import com.qiaoba.common.database.config.DynamicDataSourceConfig;
|
||||
import com.qiaoba.common.database.enums.DataBaseTypeEnum;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DatabaseMetaData;
|
||||
import java.sql.SQLException;
|
||||
|
||||
/**
|
||||
* 数据库工具类
|
||||
*
|
||||
* @author ailanyin
|
||||
* @version 1.0
|
||||
* @since 2023/5/22 16:41
|
||||
*/
|
||||
@Slf4j
|
||||
public class DatabaseUtil {
|
||||
|
||||
|
||||
/**
|
||||
* 获取当前数据库类型
|
||||
*/
|
||||
public static DataBaseTypeEnum getDataBaseType() {
|
||||
DataSource dataSource = (DataSource) DynamicDataSourceConfig.DATA_SOURCE_MAP.get("");
|
||||
try (Connection conn = dataSource.getConnection()) {
|
||||
DatabaseMetaData metaData = conn.getMetaData();
|
||||
String databaseProductName = metaData.getDatabaseProductName();
|
||||
return DataBaseTypeEnum.find(databaseProductName);
|
||||
} catch (SQLException e) {
|
||||
throw new ServiceException(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public static String handleFindInSet(Object var1, String var2) {
|
||||
DataBaseTypeEnum dataBaseType = getDataBaseType();
|
||||
String var = Convert.toStr(var1);
|
||||
if (dataBaseType == DataBaseTypeEnum.SQL_SERVER) {
|
||||
// charindex(',100,' , ',0,100,101,') <> 0
|
||||
return "charindex('," + var + ",' , ','+" + var2 + "+',') <> 0";
|
||||
} else if (dataBaseType == DataBaseTypeEnum.POSTGRE_SQL) {
|
||||
// (select position(',100,' in ',0,100,101,')) <> 0
|
||||
return "(select position('," + var + ",' in ','||" + var2 + "||',')) <> 0";
|
||||
} else if (dataBaseType == DataBaseTypeEnum.ORACLE) {
|
||||
// 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";
|
||||
}
|
||||
}
|
@ -50,9 +50,9 @@ public interface RedisService {
|
||||
/**
|
||||
* 批量删除
|
||||
*
|
||||
* @param collection keys
|
||||
* @param keys keys
|
||||
*/
|
||||
void del(Collection<String> collection);
|
||||
void del(Collection<String> keys);
|
||||
|
||||
|
||||
/**
|
||||
|
@ -12,6 +12,7 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 自定义Redis接口实现类
|
||||
@ -47,9 +48,9 @@ public class RedisServiceImpl implements RedisService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void del(Collection<String> collection) {
|
||||
// todo
|
||||
redisTemplate.delete(collection);
|
||||
public void del(Collection<String> keys) {
|
||||
List<String> list = keys.stream().map(key -> key = handleKey(key)).collect(Collectors.toList());
|
||||
redisTemplate.delete(list);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
Reference in New Issue
Block a user