引入 aop包

        <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>

1.新建注解 DS

package com.example.abstractroutingdatasource.config;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; /** * 在方法上使用,用于指定使用哪个数据源 * * @version v.0.1 */ @Target({ ElementType.METHOD, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
public @interface DS {
String value(); }

2.新建AOP  DataSourceAspect

package com.example.abstractroutingdatasource.config;

import com.sun.corba.se.impl.orb.DataCollectorBase;
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.springframework.core.annotation.Order;
import org.springframework.stereotype.Component; @Aspect
@Order(-10)//保证该AOP在@Transactional之前执行
@Component
public class DataSourceAspect { /* * @Before("@annotation(ds)") * 的意思是:
* @Before:在方法执行之前进行执行: * @annotation(targetDataSource): * 会拦截注解targetDataSource的方法,否则不拦截; */ // @Before("execution(* com.example.*.dao..*.*(..))")
@Before("@annotation(ds)")
public void changeDataSource(JoinPoint point,DS ds) throws Throwable {
//获取当前的指定的数据源;
String dsId =ds.value();
//如果不在我们注入的所有的数据源范围之内,那么输出警告信息,系统自动使用默认的数据源。
DatabaseContextHolder.setDatabaseType(dsId); } @After("@annotation(ds)")
public void restoreDataSource(JoinPoint point, DS ds) { System.out.println("Revert DataSource : {} > {}"+ds.value()+point.getSignature()); //方法执行完毕之后,销毁当前数据源信息,进行垃圾回收。 DatabaseContextHolder.ClearDataBaseType(); } }

3.在dao 上加注解

package com.example.abstractroutingdatasource.dao;

import com.example.abstractroutingdatasource.config.DS;
import com.example.abstractroutingdatasource.config.DatabaseContextHolder;
import com.example.abstractroutingdatasource.entity.UcUser;
import com.example.abstractroutingdatasource.mapper.UcUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository; import java.net.UnknownHostException; @Repository
public class UcUserDao { @Autowired
private UcUserMapper ucUserMapper; @DS(value="1")
public UcUser selectOne(String id,String ds ){
// DatabaseContextHolder.setDatabaseType(ds); //手动设置库
return ucUserMapper.getUser(id);
} @DS(value="2")
public UcUser selectOne2(String id,String ds ){
// DatabaseContextHolder.setDatabaseType(ds); //手动设置库
return ucUserMapper.getUser(id);
}
}

4.完成,启动应用访问页面 数据源会自动切换

demo 链接: https://pan.baidu.com/s/1rpC7lMxF_ENW_zLr7MGlBQ 

第八章 springboot + mybatis + 多数据源3(使用切面AOP)的更多相关文章

  1. 第八章 springboot + mybatis + 多数据源(转载)

    本篇博客转发自:http://www.cnblogs.com/java-zhao/p/5413845.html 在实际开发中,我们一个项目可能会用到多个数据库,通常一个数据库对应一个数据源. 代码结构 ...

  2. 第八章 springboot + mybatis + 多数据源2(解决循环引用)

    解决了循环引用 1.application.properties #the first datasource jdbc.names:1,2 jdbc1.driverClassName = com.my ...

  3. 第八章 springboot + mybatis + 多数据源

    http://www.cnblogs.com/java-zhao/p/5413845.html

  4. 第九章 springboot + mybatis + 多数据源 (AOP实现)

    在第八章 springboot + mybatis + 多数据源代码的基础上,做两点修改 1.ShopDao package com.xxx.firstboot.dao; import org.spr ...

  5. 【第九章】 springboot + mybatis + 多数据源 (AOP实现)

    在第八章 springboot + mybatis + 多数据源代码的基础上,做两点修改 1.ShopDao package com.xxx.firstboot.dao; import org.spr ...

  6. spring-boot (四) springboot+mybatis多数据源最简解决方案

    学习文章来自:http://www.ityouknow.com/spring-boot.html 配置文件 pom包就不贴了比较简单该依赖的就依赖,主要是数据库这边的配置: mybatis.confi ...

  7. springboot + mybatis + 多数据源

    此文已由作者赵计刚薪授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验 在实际开发中,我们一个项目可能会用到多个数据库,通常一个数据库对应一个数据源. 代码结构: 简要原理: 1) ...

  8. springboot mybatis 多数据源配置

    首先导入mybatis等包,这里就不多说. 下面是配置多数据源和mybatis,每个数据源对应一套mybatis模板 数据源1: package com.aaaaaaa.config.datasour ...

  9. 【第八章】 springboot + mybatis + 多数据源

    在实际开发中,我们一个项目可能会用到多个数据库,通常一个数据库对应一个数据源. 代码结构: 简要原理: 1)DatabaseType列出所有的数据源的key---key 2)DatabaseConte ...

随机推荐

  1. 【最短路】【最大流】bzoj3931 [CQOI2015]网络吞吐量

    跑出最短路图,然后把结点拆点跑最大流. #include<cstdio> #include<queue> #include<cstring> #include< ...

  2. 【树链剖分/线段树】BZOJ1036-[ZJOI2008]树的统计Count

    [题目大意] 一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w.我们将以下面的形式来要求你对这棵树完成 一些操作: I. CHANGE u t : 把结点u的权值改为t II. QMAX ...

  3. Mybatis全部标签

    一.定义SQL语句 (1)select 标签的使用 属性介绍: id :唯一的标识符. parameterType:传给此语句的参数的全路径名或别名 例:com.test.poso.User或user ...

  4. Problem C: 零起点学算法18——3个数比较大小

    #include<stdio.h> int main() { int a,b,c; while(scanf("%d %d %d",&a,&b,& ...

  5. ubuntu下python3及idle3的安装

    一.使用以下命令检查自己的系统下是否有python3 python3 --version 如果出现类似“command not found",则说明你需要安装python3.如果能够出现py ...

  6. 上传ipa文件时报错 Your account already has a valid iOS distribution certificate

    这个问题是因为你本机的生产证书是在别人的电脑上创建的,所以才会提示你已经有一个有效的生产证书,但是没有安装到本地:

  7. winform 窗体实现增删改查(CRUD)窗体基类模式

    参考博客下方:http://www.cnblogs.com/wuhuacong/archive/2010/05/31/1748579.html 对于一般常用到的编辑数据.新增数据窗体,分开了两个不同的 ...

  8. nagios监控redis

    nagios是非常强大的监控工具,但是它本身没有监控redis的功能 但是网上有很多大神写了监控redis的插件,比较热门的使用perl写的check_redis.pl 但是由于我们监控mongodb ...

  9. mvn test 中文乱码

    有两种解决办法: 1.设置encoding:<argLine>-Dfile.encoding=UTF-8</argLine>,解决读取文件中的中文乱码问题 2.升级maven- ...

  10. window7下面安装pear.pchar--wamp环境

    准备工作: Wamp php版本:5.3.10 1.下载pear.phar 2.设置php路径的path环境变量 开始安装 1.以管理员身份运行cmd 2.进入pear.phar的所在目录 3.命令行 ...