pull/22/head
RuoYi 6 years ago
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…
Cancel
Save