package com.kung.datasource;
import java.lang.reflect.Method;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
//编写AOP切面,实现切换逻辑
@Aspect
@Component
public class DynamicDataSourceAspect {
@Before("@annotation(DS)")
public void beforeSwitchDS(JoinPoint point){
//获得当前访问的class
Class<?> className = point.getTarget().getClass();
//获得访问的方法名
String methodName = point.getSignature().getName();
//得到方法的参数的类型
Class[] argClass = ((MethodSignature)point.getSignature()).getParameterTypes();
String dataSource = DataSourceContextHolder.DEFAULT_DS;
try {
// 得到访问的方法对象
Method method = className.getMethod(methodName, argClass);
// 判断是否存在@DS注解
if (method.isAnnotationPresent(DS.class)) {
DS annotation = method.getAnnotation(DS.class);
// 取出注解中的数据源名
dataSource = annotation.value();
}
} catch (Exception e) {
e.printStackTrace();
}
// 切换数据源
DataSourceContextHolder.setDB(dataSource);
}
@After("@annotation(DS)")
public void afterSwitchDS(JoinPoint point){
DataSourceContextHolder.clearDB();
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
SpringBoot 2.X +Mybatis 多数据源
共47个文件
class:14个
java:14个
prefs:4个
需积分: 41 60 下载量 154 浏览量
2018-05-17
15:16:41
上传
评论
收藏 79KB ZIP 举报
温馨提示
SpringBoot 2.X +Mybatis 多数据源,SpringBoot 2.X +Mybatis 多数据源
资源推荐
资源详情
资源评论
收起资源包目录
SpringBootMybatisTwo.zip (47个子文件)
.project 986B
mvnw.cmd 5KB
.apt_generated
mvnw 6KB
src
test
java
com
kung
SpringMybatisTwoApplicationTests.java 335B
main
resources
application.properties 1KB
templates
static
java
com
kung
service
UserService.java 574B
controller
UserController.java 689B
SpringBootMybatisTwoApplication.java 816B
datasourceb
mapper
UserMapperB.java 532B
entity
User.java 2KB
datasource
DS.java 421B
MybatisDbBConfig.java 1KB
DataSourceConfig.java 1KB
MybatisDbAConfig.java 1KB
DynamicDataSource.java 477B
DataSourceContextHolder.java 1018B
DynamicDataSourceAspect.java 2KB
datasourcea
mapper
UserMapperA.java 532B
target
classes
application.properties 1KB
com
kung
service
UserService.class 950B
SpringBootMybatisTwoApplication.class 909B
controller
UserController.class 1KB
datasourceb
mapper
UserMapperB.class 731B
entity
User.class 2KB
datasource
DynamicDataSource.class 888B
DataSourceContextHolder.class 1KB
MybatisDbBConfig.class 1KB
DynamicDataSourceAspect.class 2KB
MybatisDbAConfig.class 1KB
DataSourceConfig.class 2KB
DS.class 451B
datasourcea
mapper
UserMapperA.class 731B
META-INF
maven
com.kung
SpringMybatisTwo
pom.properties 247B
pom.xml 2KB
spring-configuration-metadata.json 505B
test-classes
com
kung
SpringMybatisTwoApplicationTests.class 642B
.factorypath 13KB
.settings
org.eclipse.wst.common.project.facet.core.xml 145B
org.eclipse.m2e.core.prefs 97B
org.eclipse.jdt.core.prefs 301B
org.eclipse.jdt.apt.core.prefs 68B
org.eclipse.core.resources.prefs 155B
.mvn
wrapper
maven-wrapper.jar 46KB
maven-wrapper.properties 110B
pom.xml 2KB
.classpath 1KB
.gitignore 268B
共 47 条
- 1
资源评论
kung900519
- 粉丝: 114
- 资源: 26
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功