第九章 springboot + mybatis + 多数据源 (AOP实现)(转载)
本编博客转发自:http://www.cnblogs.com/java-zhao/p/5415896.html
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.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()")
第九章 springboot + mybatis + 多数据源 (AOP实现)(转载)的更多相关文章
- 第九章 springboot + mybatis + 多数据源 (AOP实现)
在第八章 springboot + mybatis + 多数据源代码的基础上,做两点修改 1.ShopDao package com.xxx.firstboot.dao; import org.spr ...
- 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> ...
随机推荐
- String类常用方法
1.String类的特点,字符串一旦被初始化就不会被改变. 2.String对象定义的两种方式 ①String s = "affdf";这种定义方式是在字符串常量池中创建一个Str ...
- Ajax请求数据
后台使用数数组的形式存放数据(以键值对的形式存放).让后再Json转码. Map<String,String> map=new HashMap<String,String>() ...
- 获取iPhone 联系人列表,并且根据分析得到的姓名首字母进行排序
获取手机联系人以iOS9为分界点,大家都知道到了iOS9很多方法都更新了,好多接口都弃用,被新的接口代替.这Demo种有新旧两个接口,使用前判断当前iOS版本. 下面是Demo连接地址:Github的 ...
- jq实现全选、全不选、反选
基本思路: 1全选:点击全选按钮的时候,将input的属性checked设置为true; 2全不选:点击全不选按钮的时候,将input的属性checked设置为false; 3反选:点击反选按钮的时候 ...
- Generate transparent shape on image
Here is an example code to generate transparent shape on image. Need to pay attention can not use cv ...
- Linux和windows之间通过scp复制文件
Windows是不支持ssh协议的 需要安装WinSSHD 安装以及设置过程如下: BvSshServer(原名winsshd)官方下载页在这里:https://www.bitvise.com/dow ...
- splinter(python操作浏览器魔魁啊)
from splinter import Browser def main(): browser = Browser() browser.visit('http://google.com') brow ...
- 视频聊天APP
1 通讯 P2P 1.1 关键字[java udp 打洞] 参考文章 http://www.oschina.net/code/snippet_165555_17279 关键代码 package org ...
- C++ 里大写TRUE和小写true区别
1.C++里大写TRUE和小写true区别 true是bool型的: TRUE是int型的,VC里这个是ms自己定义的: C++规定不允许只通过返回类型不同区别两个函数 2.MFC中的”false“和 ...
- c#自制视屏监控
项目需要开发一个监控程序,主要是监控其它电脑的操作情况. 先说下原理吧,首先我们列出做远程监控的基本步骤,远端电脑的ip,捕捉屏幕的方法,传输,接收并显示. 突然不知道怎么写下去了....... 程序 ...