在第八章 springboot + mybatis + 多数据源代码的基础上,做两点修改
1、ShopDao

1 package com.xxx.firstboot.dao; 2 3 import org.springframework.beans.factory.annotation.Autowired; 4 import org.springframework.stereotype.Repository; 5 6 import com.xxx.firstboot.domain.Shop; 7 import com.xxx.firstboot.mapper.ShopMapper; 8 9 @Repository 10 public class ShopDao { 11 @Autowired 12 private ShopMapper mapper; 13 14 /** 15 * 获取shop 16 */ 17 public Shop getShop(int id) { 18 return mapper.getShop(id); 19 } 20 }
说明:只是去掉了设置数据源key的那一句代码
2、DataSourceAspect

1 package com.xxx.firstboot.common.datasource; 2 3 import org.aspectj.lang.JoinPoint; 4 import org.aspectj.lang.annotation.Aspect; 5 import org.aspectj.lang.annotation.Before; 6 import org.springframework.stereotype.Component; 7 8 import com.xxx.firstboot.dao.ShopDao; 9 10 @Aspect 11 @Component 12 public class DataSourceAspect { 13 14 @Before("execution(* com.xxx.firstboot.dao.*.*(..))") 15 public void setDataSourceKey(JoinPoint point){ 16 //连接点所属的类实例是ShopDao 17 if(point.getTarget() instanceof ShopDao){ 18 DatabaseContextHolder.setDatabaseType(DatabaseType.mytestdb2); 19 }else{//连接点所属的类实例是UserDao(当然,这一步也可以不写,因为defaultTargertDataSource就是该类所用的mytestdb) 20 DatabaseContextHolder.setDatabaseType(DatabaseType.mytestdb); 21 } 22 } 23 24 // @Around("execution(* com.xxx.firstboot.dao.*.*(..))") 25 // public Object setDataSourceKeyByAround(ProceedingJoinPoint point) throws Throwable{ 26 // if(point.getTarget() instanceof ShopDao){ 27 // DatabaseContextHolder.setDatabaseType(DatabaseType.mytestdb2); 28 // }else{//连接点所属的类实例是UserDao(当然,这一步也可以不写,因为defaultTargertDataSource就是该类所用的mytestdb) 29 // DatabaseContextHolder.setDatabaseType(DatabaseType.mytestdb); 30 // } 31 // return point.proceed();//执行目标方法 32 // } 33 34 }
说明:列出了两种切面方法,在这里推荐使用前者,原因:
- @Around:需要写执行目标方法的那一行代码,而这一行代码可能会抛异常,还需要抛出或捕获
对于切点表达式,可以抽取出来,进行重复利用。如上代码可以改为如下:

1 package com.xxx.firstboot.common.datasource; 2 3 import org.aspectj.lang.JoinPoint; 4 import org.aspectj.lang.annotation.Aspect; 5 import org.aspectj.lang.annotation.Before; 6 import org.aspectj.lang.annotation.Pointcut; 7 import org.springframework.stereotype.Component; 8 9 import com.xxx.firstboot.dao.ShopDao; 10 11 @Aspect 12 @Component 13 public class DataSourceAspect { 14 15 /** 16 * 使用空方法定义切点表达式 17 */ 18 @Pointcut("execution(* com.xxx.firstboot.dao.*.*(..))") 19 public void declareJointPointExpression() { 20 } 21 22 /** 23 * 使用定义切点表达式的方法进行切点表达式的引入 24 */ 25 @Before("declareJointPointExpression()") 26 public void setDataSourceKey(JoinPoint point) { 27 // 连接点所属的类实例是ShopDao 28 if (point.getTarget() instanceof ShopDao) { 29 DatabaseContextHolder.setDatabaseType(DatabaseType.mytestdb2); 30 } else {// 连接点所属的类实例是UserDao(当然,这一步也可以不写,因为defaultTargertDataSource就是该类所用的mytestdb) 31 DatabaseContextHolder.setDatabaseType(DatabaseType.mytestdb); 32 } 33 } 34 35 }
注意:该切点表达式也可以用在其他切面类中,引入的时候使用"全类名.切点方法名()",例:@Before("com.xxx.firstboot.common.datasource.DataSourceAspect.declareJointPointExpression()")
关于AOP,查看:第一章 AOP
分类:
微服务
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 【杂谈】分布式事务——高大上的无用知识?