mirror of https://gitee.com/y_project/RuoYi.git
若依 2.3
parent
92fffea429
commit
127b532825
|
@ -12,7 +12,7 @@ import org.springframework.core.annotation.Order;
|
|||
import org.springframework.stereotype.Component;
|
||||
import com.ruoyi.common.utils.StringUtils;
|
||||
import com.ruoyi.framework.aspectj.lang.annotation.Ds;
|
||||
import com.ruoyi.framework.datasource.DataSourceContextHolder;
|
||||
import com.ruoyi.framework.datasource.DynamicDataSourceContextHolder;
|
||||
|
||||
/**
|
||||
* 多数据源处理
|
||||
|
@ -27,12 +27,12 @@ public class DsAspect
|
|||
protected Logger logger = LoggerFactory.getLogger(getClass());
|
||||
|
||||
@Pointcut("@annotation(com.ruoyi.framework.aspectj.lang.annotation.Ds)")
|
||||
public void dataSourcePointCut()
|
||||
public void dsPointCut()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
@Around("dataSourcePointCut()")
|
||||
@Around("dsPointCut()")
|
||||
public Object around(ProceedingJoinPoint point) throws Throwable
|
||||
{
|
||||
MethodSignature signature = (MethodSignature) point.getSignature();
|
||||
|
@ -44,7 +44,7 @@ public class DsAspect
|
|||
Ds dataSource = method.getAnnotation(Ds.class);
|
||||
if (StringUtils.isNotNull(dataSource) && StringUtils.isNotEmpty(dataSource.name()))
|
||||
{
|
||||
DataSourceContextHolder.setDB(dataSource.name());
|
||||
DynamicDataSourceContextHolder.setDB(dataSource.name());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -54,7 +54,7 @@ public class DsAspect
|
|||
}
|
||||
finally
|
||||
{
|
||||
DataSourceContextHolder.clearDB();
|
||||
DynamicDataSourceContextHolder.clearDB();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,35 +0,0 @@
|
|||
package com.ruoyi.framework.datasource;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* 当前线程数据源
|
||||
*
|
||||
* @author ruoyi
|
||||
*/
|
||||
public class DataSourceContextHolder
|
||||
{
|
||||
public static final Logger log = LoggerFactory.getLogger(DataSourceContextHolder.class);
|
||||
|
||||
private static final ThreadLocal<String> contextHolder = new ThreadLocal<>();
|
||||
|
||||
// 设置数据源名
|
||||
public static void setDB(String dbType)
|
||||
{
|
||||
log.info("切换到{}数据源", dbType);
|
||||
contextHolder.set(dbType);
|
||||
}
|
||||
|
||||
// 获取数据源名
|
||||
public static String getDB()
|
||||
{
|
||||
return contextHolder.get();
|
||||
}
|
||||
|
||||
public static void clearDB()
|
||||
{
|
||||
contextHolder.remove();
|
||||
}
|
||||
|
||||
}
|
|
@ -21,7 +21,7 @@ public class DynamicDataSource extends AbstractRoutingDataSource
|
|||
@Override
|
||||
protected Object determineCurrentLookupKey()
|
||||
{
|
||||
return DataSourceContextHolder.getDB();
|
||||
return DynamicDataSourceContextHolder.getDB();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
package com.ruoyi.framework.datasource;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* 当前线程数据源
|
||||
*
|
||||
* @author ruoyi
|
||||
*/
|
||||
public class DynamicDataSourceContextHolder
|
||||
{
|
||||
public static final Logger log = LoggerFactory.getLogger(DynamicDataSourceContextHolder.class);
|
||||
|
||||
private static final ThreadLocal<String> CONTEXT_HOLDER = new ThreadLocal<>();
|
||||
|
||||
/**
|
||||
* 设置数据源名
|
||||
*/
|
||||
public static void setDB(String dbType)
|
||||
{
|
||||
log.info("切换到{}数据源", dbType);
|
||||
CONTEXT_HOLDER.set(dbType);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取数据源名
|
||||
*/
|
||||
public static String getDB()
|
||||
{
|
||||
return CONTEXT_HOLDER.get();
|
||||
}
|
||||
|
||||
/**
|
||||
* 清理数据源名
|
||||
*/
|
||||
public static void clearDB()
|
||||
{
|
||||
CONTEXT_HOLDER.remove();
|
||||
}
|
||||
|
||||
}
|
|
@ -69,7 +69,7 @@ public interface PostMapper
|
|||
/**
|
||||
* 校验岗位名称
|
||||
*
|
||||
* @param post 岗位信息
|
||||
* @param postName 岗位名称
|
||||
* @return 结果
|
||||
*/
|
||||
public Post checkPostNameUnique(String postName);
|
||||
|
@ -77,7 +77,7 @@ public interface PostMapper
|
|||
/**
|
||||
* 校验岗位编码
|
||||
*
|
||||
* @param post 岗位信息
|
||||
* @param postCode 岗位编码
|
||||
* @return 结果
|
||||
*/
|
||||
public Post checkPostCodeUnique(String postCode);
|
||||
|
|
Loading…
Reference in New Issue