parent
ba4a597b14
commit
018f73732b
6 changed files with 98 additions and 5 deletions
@ -0,0 +1,34 @@ |
||||
package com.dreamlyn.dynamic; |
||||
|
||||
import com.dreamlyn.dynamic.annotation.RoutingWith; |
||||
import com.dreamlyn.dynamic.entity.Menu; |
||||
import com.dreamlyn.dynamic.mapper.MenuMapper; |
||||
import com.dreamlyn.dynamic.mapper.UserMapper; |
||||
import lombok.extern.slf4j.Slf4j; |
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.boot.ApplicationArguments; |
||||
import org.springframework.boot.ApplicationRunner; |
||||
import org.springframework.context.annotation.Configuration; |
||||
|
||||
import java.util.List; |
||||
|
||||
@Slf4j |
||||
@Configuration |
||||
public class UseDynamicDatasource implements ApplicationRunner { |
||||
|
||||
@Autowired |
||||
private MenuMapper menuMapper; |
||||
@Autowired |
||||
private UserMapper userMapper; |
||||
|
||||
@Override |
||||
@RoutingWith("slave") |
||||
public void run(ApplicationArguments args) throws Exception { |
||||
//设置数据源
|
||||
log.info("current dataSource key:{}", DynamicDataSourceContextHolder.getDataSource()); |
||||
List<Menu> menus = menuMapper.selectList(null); |
||||
log.info("menu count:{}", menus.size()); |
||||
// List<User> users = userMapper.selectList(null);
|
||||
// log.info("user count:{}", users.size());
|
||||
} |
||||
} |
@ -0,0 +1,17 @@ |
||||
package com.dreamlyn.dynamic.annotation; |
||||
|
||||
import java.lang.annotation.*; |
||||
|
||||
/** |
||||
* |
||||
*/ |
||||
@Target(ElementType.METHOD) |
||||
@Retention(RetentionPolicy.RUNTIME) |
||||
@Documented |
||||
public @interface RoutingWith { |
||||
/** |
||||
* 使用的数据源 |
||||
* @return |
||||
*/ |
||||
String value() default "master"; |
||||
} |
@ -0,0 +1,36 @@ |
||||
package com.dreamlyn.dynamic.aspect; |
||||
|
||||
import com.dreamlyn.dynamic.DynamicDataSourceContextHolder; |
||||
import com.dreamlyn.dynamic.annotation.RoutingWith; |
||||
import org.aspectj.lang.ProceedingJoinPoint; |
||||
import org.aspectj.lang.annotation.Around; |
||||
import org.aspectj.lang.annotation.Aspect; |
||||
import org.aspectj.lang.annotation.Pointcut; |
||||
import org.aspectj.lang.reflect.MethodSignature; |
||||
import org.springframework.stereotype.Component; |
||||
|
||||
import java.lang.reflect.Method; |
||||
|
||||
/** |
||||
* 系统日志,切面处理类 |
||||
*/ |
||||
@Aspect |
||||
@Component |
||||
public class RoutingAspect { |
||||
@Pointcut("@annotation(com.dreamlyn.dynamic.annotation.RoutingWith)") |
||||
public void routingPointCut() { |
||||
|
||||
} |
||||
|
||||
@Around("routingPointCut()") |
||||
public Object around(ProceedingJoinPoint point) throws Throwable { |
||||
MethodSignature signature = (MethodSignature) point.getSignature(); |
||||
Method method = signature.getMethod(); |
||||
RoutingWith routingWith = method.getAnnotation(RoutingWith.class); |
||||
if (routingWith != null) { |
||||
String key = routingWith.value(); |
||||
DynamicDataSourceContextHolder.setDataSource(key); |
||||
} |
||||
return point.proceed(); |
||||
} |
||||
} |
Loading…
Reference in new issue