第九章 springboot + mybatis + 多数据源 (AOP实现)
在第八章 springboot + mybatis + 多数据源代码的基础上,做两点修改
1、ShopDao
package com.xxx.firstboot.dao; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository; import com.xxx.firstboot.domain.Shop;
import com.xxx.firstboot.mapper.ShopMapper; @Repository
public class ShopDao {
@Autowired
private ShopMapper mapper; /**
* 获取shop
*/
public Shop getShop(int id) {
return mapper.getShop(id);
}
}
说明:只是去掉了设置数据源key的那一句代码
2、DataSourceAspect
package com.xxx.firstboot.common.datasource; import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component; import com.xxx.firstboot.dao.ShopDao; @Aspect
@Component
public class DataSourceAspect { @Before("execution(* com.xxx.firstboot.dao.*.*(..))")
public void setDataSourceKey(JoinPoint point){
//连接点所属的类实例是ShopDao
if(point.getTarget() instanceof ShopDao){
DatabaseContextHolder.setDatabaseType(DatabaseType.mytestdb2);
}else{//连接点所属的类实例是UserDao(当然,这一步也可以不写,因为defaultTargertDataSource就是该类所用的mytestdb)
DatabaseContextHolder.setDatabaseType(DatabaseType.mytestdb);
}
} // @Around("execution(* com.xxx.firstboot.dao.*.*(..))")
// public Object setDataSourceKeyByAround(ProceedingJoinPoint point) throws Throwable{
// if(point.getTarget() instanceof ShopDao){
// DatabaseContextHolder.setDatabaseType(DatabaseType.mytestdb2);
// }else{//连接点所属的类实例是UserDao(当然,这一步也可以不写,因为defaultTargertDataSource就是该类所用的mytestdb)
// DatabaseContextHolder.setDatabaseType(DatabaseType.mytestdb);
// }
// return point.proceed();//执行目标方法
// } }
说明:列出了两种切面方法,在这里推荐使用前者,原因:
- @Around:需要写执行目标方法的那一行代码,而这一行代码可能会抛异常,还需要抛出或捕获
对于切点表达式,可以抽取出来,进行重复利用。如上代码可以改为如下:
package com.xxx.firstboot.common.datasource; import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component; import com.xxx.firstboot.dao.ShopDao; @Aspect
@Component
public class DataSourceAspect { /**
* 使用空方法定义切点表达式
*/
@Pointcut("execution(* com.xxx.firstboot.dao.*.*(..))")
public void declareJointPointExpression() {
} /**
* 使用定义切点表达式的方法进行切点表达式的引入
*/
@Before("declareJointPointExpression()")
public void setDataSourceKey(JoinPoint point) {
// 连接点所属的类实例是ShopDao
if (point.getTarget() instanceof ShopDao) {
DatabaseContextHolder.setDatabaseType(DatabaseType.mytestdb2);
} else {// 连接点所属的类实例是UserDao(当然,这一步也可以不写,因为defaultTargertDataSource就是该类所用的mytestdb)
DatabaseContextHolder.setDatabaseType(DatabaseType.mytestdb);
}
} }
注意:该切点表达式也可以用在其他切面类中,引入的时候使用"全类名.切点方法名()",例:@Before("com.xxx.firstboot.common.datasource.DataSourceAspect.declareJointPointExpression()")
关于AOP,查看:第一章 AOP
第九章 springboot + mybatis + 多数据源 (AOP实现)的更多相关文章
- 第九章 springboot + mybatis + 多数据源 (AOP实现)(转载)
本编博客转发自:http://www.cnblogs.com/java-zhao/p/5415896.html 1.ShopDao package com.xxx.firstboot.dao; imp ...
- springboot + mybatis + 多数据源
此文已由作者赵计刚薪授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验 在实际开发中,我们一个项目可能会用到多个数据库,通常一个数据库对应一个数据源. 代码结构: 简要原理: 1) ...
- SpringBoot整合Mybatis多数据源 (AOP+注解)
SpringBoot整合Mybatis多数据源 (AOP+注解) 1.pom.xml文件(开发用的JDK 10) <?xml version="1.0" encoding=& ...
- 第五章 springboot + mybatis(转载)
本编博客转发自:http://www.cnblogs.com/java-zhao/p/5350021.html springboot集成了springJDBC与JPA,但是没有集成mybatis,所以 ...
- 第五章 springboot + mybatis
springboot集成了springJDBC与JPA,但是没有集成mybatis,所以想要使用mybatis就要自己去集成.集成方式相当简单. 1.项目结构 2.pom.xml <!-- 与数 ...
- spring-boot (四) springboot+mybatis多数据源最简解决方案
学习文章来自:http://www.ityouknow.com/spring-boot.html 配置文件 pom包就不贴了比较简单该依赖的就依赖,主要是数据库这边的配置: mybatis.confi ...
- 【第九章】 springboot + mybatis + 多数据源 (AOP实现)
在第八章 springboot + mybatis + 多数据源代码的基础上,做两点修改 1.ShopDao package com.xxx.firstboot.dao; import org.spr ...
- springboot mybatis 多数据源配置
首先导入mybatis等包,这里就不多说. 下面是配置多数据源和mybatis,每个数据源对应一套mybatis模板 数据源1: package com.aaaaaaa.config.datasour ...
- 第八章 springboot + mybatis + 多数据源3(使用切面AOP)
引入 aop包 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...
随机推荐
- 什么是BOM头,及PHP解决办法
类似WINDOWS自带的记事本等软件,在保存一个以UTF-8编码的文件时,会在文件开始的地方插入三个不可见的字符(0xEF 0xBB 0xBF,即BOM).它是一串隐藏的字符,用于让记事本等编辑器识别 ...
- 【转】C# 使用消息队列,包括远程访问
出处:http://www.cnblogs.com/80X86/p/5557801.html 近期做一个小的功能需求,用到了队列,用的时候出了很多问题,现在总结一下,希望能对有需要的人提供帮助. 我的 ...
- jquery是如何架构的.
心里一直有个疑问. jquery是如何做到一个jQuery即可以当方法用比如$();又可以当对象用$.extend(); 现在总结一下吧 function method(){} var m=new m ...
- 源码解读—HashTable
在上一篇学习过HashMap(源码解读—HashMap)之后对hashTable也产生了兴趣,随即便把hashTable的源码看了一下.和hashMap类似,但是也有不同之处. public clas ...
- my first article
BLOG: http://codetask.org GIT: http://git.oschina.net/codetimer
- Biological Filtration
http://www.fishyou.com/filtration-biological.php Biological Filtration Biological filtration is the ...
- crosswalk-webview
https://github.com/crosswalk-project/cordova-plugin-crosswalk-webview https://cordova.apache.org/doc ...
- Fragment一些问题
1.使用fragment静态加载,当需要替换的时候使用replace方式是无效的....... 2.replace的容器如果是线性布局,那么将会出现之前的页面残留的情况,正确做法是使用FrameLay ...
- kindeditor-4.1.7应用
页面: <script type="text/javascript" src="include/kindeditor/kindeditor.js"> ...
- 转:RTC搭建android下三层应用程序访问服务器MsSql-服务器端
原文:http://www.cnblogs.com/delphi007/p/3346061.html 前几天通过Ro搭建webservice,然后在android下调用,虽然已近成功,但是返回的数据库 ...