From 127b532825ee27b62acd40d2c1ef806a0e19005c Mon Sep 17 00:00:00 2001 From: RuoYi Date: Sun, 5 Aug 2018 12:49:07 +0800 Subject: [PATCH] =?UTF-8?q?=E8=8B=A5=E4=BE=9D=202.3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/ruoyi/framework/aspectj/DsAspect.java | 10 ++--- .../datasource/DataSourceContextHolder.java | 35 ---------------- .../datasource/DynamicDataSource.java | 2 +- .../DynamicDataSourceContextHolder.java | 42 +++++++++++++++++++ .../system/post/mapper/PostMapper.java | 4 +- 5 files changed, 50 insertions(+), 43 deletions(-) delete mode 100644 src/main/java/com/ruoyi/framework/datasource/DataSourceContextHolder.java create mode 100644 src/main/java/com/ruoyi/framework/datasource/DynamicDataSourceContextHolder.java diff --git a/src/main/java/com/ruoyi/framework/aspectj/DsAspect.java b/src/main/java/com/ruoyi/framework/aspectj/DsAspect.java index 76c4e3cc8..bfc64db30 100644 --- a/src/main/java/com/ruoyi/framework/aspectj/DsAspect.java +++ b/src/main/java/com/ruoyi/framework/aspectj/DsAspect.java @@ -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(); } } } diff --git a/src/main/java/com/ruoyi/framework/datasource/DataSourceContextHolder.java b/src/main/java/com/ruoyi/framework/datasource/DataSourceContextHolder.java deleted file mode 100644 index 97a85a09d..000000000 --- a/src/main/java/com/ruoyi/framework/datasource/DataSourceContextHolder.java +++ /dev/null @@ -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 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(); - } - -} diff --git a/src/main/java/com/ruoyi/framework/datasource/DynamicDataSource.java b/src/main/java/com/ruoyi/framework/datasource/DynamicDataSource.java index 04fd8c4bb..f1c97cf09 100644 --- a/src/main/java/com/ruoyi/framework/datasource/DynamicDataSource.java +++ b/src/main/java/com/ruoyi/framework/datasource/DynamicDataSource.java @@ -21,7 +21,7 @@ public class DynamicDataSource extends AbstractRoutingDataSource @Override protected Object determineCurrentLookupKey() { - return DataSourceContextHolder.getDB(); + return DynamicDataSourceContextHolder.getDB(); } } \ No newline at end of file diff --git a/src/main/java/com/ruoyi/framework/datasource/DynamicDataSourceContextHolder.java b/src/main/java/com/ruoyi/framework/datasource/DynamicDataSourceContextHolder.java new file mode 100644 index 000000000..0d6d7e883 --- /dev/null +++ b/src/main/java/com/ruoyi/framework/datasource/DynamicDataSourceContextHolder.java @@ -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 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(); + } + +} diff --git a/src/main/java/com/ruoyi/project/system/post/mapper/PostMapper.java b/src/main/java/com/ruoyi/project/system/post/mapper/PostMapper.java index 7b71d2810..08c07e895 100644 --- a/src/main/java/com/ruoyi/project/system/post/mapper/PostMapper.java +++ b/src/main/java/com/ruoyi/project/system/post/mapper/PostMapper.java @@ -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);