第九章 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> ...
随机推荐
- 【python】类的继承和多态
比如,我们已经编写了一个名为Animal的class,有一个run()方法可以直接打印: class Animal(object): def run(self): print 'Animal is r ...
- 一个服务器上面配置多个IP ,实现指定IP的域名请求
//配置多个IP命名using System.Net; //********************************************************************** ...
- windows 代理服务器的搭建,提供Android 端访问公网.
这段时间遇到一个情况,移动的网络收费.但是可以访问学校内部的网络,比如说学校官网图书馆之类了.所以我这里便想到一个方法,用学校内部一个可以访问互联网的主机充当代理服务器(我这里使用自己的电脑,非服务器 ...
- H5课程大纲
K1模块课程: 课程模块 课程阶段 课程内容 K1 模块 第1阶段 认识前端开发 环境配置.使用标签的分类.写法及使用规范CSS样式的使用.各类常见样式Photoshop使用16大常用样式盒模型.语义 ...
- haproxy+keepalived实现高可用负载均衡
软件负载均衡一般通过两种方式来实现:基于操作系统的软负载实现和基于第三方应用的软负载实现.LVS就是基于Linux操作系统实现的一种软负载,HAProxy就是开源的并且基于第三应用实现的软负载. HA ...
- Linux nfs配置
***************节点2的arch2目录挂载到节点1下.那么节点2是主节点1是备******** 10.230.39.234(节点1)cat /etc/exports /arch2 10. ...
- MysqlServer如何实现成功卸载,并成功安装
MysqlServer卸载过程如下: (1).在控制面板或者通过其他卸载工具中,卸载MysqlServer. (2).打开C:\ProgramData---这个文件是隐藏的,需显示出来.在里面找到my ...
- System.TypeInitializationException: The type initializer for 'Mono.Unix.Native.Stdlib' threw an exception.
08-31 17:02:03: ### DEBUG ##########################System.TypeInitializationException: The type ini ...
- VBS数组
定义一个数组: dim a(3).这里要注意在VBS里面数组不像其他的例如C,C#,JAVA等数组用[]作为数组标志.VBS采用的是().还需要注意的是,这里定义的数组包含a(0),a(1),a(2) ...
- PHP中使用Session配合Javascript实现文件上传进度条功能
Web应用中常需要提供文件上传的功能.典型的场景包括用户头像上传.相册图片上传等.当需要上传的文件比较大的时候,提供一个显示上传进度的进度条就很有必要了. 在PHP .4以前,实现这样的进度条并不容易 ...