spring boot 中 Mybatis plus 多数据源的配置方法
最近在学习spring boot,发现在jar包依赖方面做很少的工作量就可以了,对于数据库操作,我用的比较多的是mybatis plus,在中央仓库已经有mybatis-plus的插件了,对于单数据源来说直接使用就是了,但我自己的项目经常会有多数据源的情况,自己去试着写数据源的代码,核心的方法参考mp说明文档中多数据源的处理,使用动态数据源,根据需求去切换数据源
新建spring-boot项目
这一步大家去参考其它教程,很简单
定义数据源相关Model
动态数据源
继承了抽像的数据源,并实现了DataSource,归根结底还是数据就是了,在这里面进行扩展,实现determiniCurrentlookupKey,也就是获取当前需要使用数据源的key值,在父类方法中有一个map,用来保存key值与数据源的对应关系,而key值是与当前线程相关的,DbcontextHolder代码见下
package com.zhangshuo.common.dataSource;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
/**
* Created by Administrator on 2017/5/31 0031.
*/
publicclassDynamicDataSourceextendsAbstractRoutingDataSource{
/**
* 取得当前使用哪个数据源
*
* @return
*/
@Override
protectedObject determineCurrentLookupKey(){
returnDbContextHolder.getDbType();
}
}
定义DbContextHolder
里面包含静态方法,用来设置或获取线程相关的数据源别名
publicclassDbContextHolder{
privatestaticfinalThreadLocal<String> contextHolder =newThreadLocal<>();
/**
* 设置数据源
*
* @param dbTypeEnum
*/
publicstaticvoid setDbType(DBTypeEnum dbTypeEnum){
contextHolder.set(dbTypeEnum.getValue());
}
/**
* 取得当前数据源
*
* @return
*/
publicstaticString getDbType(){
return contextHolder.get();
}
/**
* 清除上下文数据
*/
publicstaticvoid clearDbType(){
contextHolder.remove();
}
}
定义数据源枚举类
简化设置线程相关数据源名称的记忆压力;直接从所有的数据源的枚举中去选就可以了~
publicenumDBTypeEnum{
datasource1("datasource1"), datasource2("datasource2");
privateString value;
DBTypeEnum(String value){
this.value = value;
}
publicString getValue(){
return value;
}
}
生成 dataource 对象及 mp需要的对象
resources/application.properties的配置
在这里定义的属性名要与DruidDataSource和 SqlSessionFactory中需要的属性相同,使用ConfigurationProperties注解来减少代码
datasource1:
username: root
password: 123456
filters: mergeStat,wall,logback
initialSize: 5
maxActive: 50
minIdle: 5
maxWait: 6000
validationQuery: SELECT 'x'
testOnBorrow: true
testOnReturn: true
testWhileIdle: true
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
removeAbandoned: true
removeAbandonedTimeout: 1800
logAbandoned: true
url: jdbc:mysql://192.168.168.118:3306/test?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&useSSL=false mybatis-plus1:
# 数据源名称
datasource: datasource1
# mapper配置路径
mapperLocations:
classpath:/mapper/test1/*.xml
# mybatis配置路径
configLocation: classpath:/mybatis-config.xml
# entity的包
typeAliasesPackage: com.zhangshuo/test1/entity
# 全局配置
globalConfiguration:
# id生成策略 0 自增 1 用户输入
idType: 0
# 灵据数类型
dbType: mysql
# 字段是否为下划线格式
dbColumnUnderline: false datasource2:
username: root
password: 123456
filters: mergeStat,wall,logback
initialSize: 5
maxActive: 50
minIdle: 5
maxWait: 6000
validationQuery: SELECT 'x'
testOnBorrow: true
testOnReturn: true
testWhileIdle: true
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
removeAbandoned: true
removeAbandonedTimeout: 1800
logAbandoned: true
url: jdbc:mysql://192.168.168.118:3306/sms?useUnicode=true&characterEncoding=utf-8 mybatis-plus2:
# 数据源名称
datasource: datasource2
# mapper配置路径
mapperLocations:
classpath:/mapper/test2/*.xml
# mybatis配置路径
configLocation:
# entity的包
typeAliasesPackage: com.zhangshuo/test2/entity
# 全局配置
globalConfiguration:
# id生成策略 0 自增 1 用户输入
idType: 0
# 灵据数类型
dbType: mysql
# 字段是否为下划线格式
dbColumnUnderline: false
生成相应对象
import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean;
import com.zhangshuo.common.dataSource.DynamicDataSource;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.ConfigurationPropertiesBinding;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import javax.sql.DataSource;
import java.util.Map;
import java.sql.SQLException;
import java.util.HashMap;
/**
* Created by Administrator on 2017/5/31 0031.
*/
@Configuration
@MapperScan(value ={"com.zhangshuo.test1.mapper","com.zhangshuo.test2.mapper"})
publicclassDataSourceConfig{
@ConfigurationProperties(prefix ="datasource1")
@Bean(name ="datasource1")
// @Primary
/**
* 在方法上注解configurationProperties时,将会把属性注入到返回结果的bean中
*/
publicDruidDataSource dataSource1()throwsSQLException{
returnnewDruidDataSource();
}
@ConfigurationProperties(prefix ="datasource2")
@Bean(name ="datasource2")
/**
* 在方法上注解configurationProperties时,将会把属性注入到返回结果的bean中
*/
publicDruidDataSource dataSource2()throwsSQLException{
returnnewDruidDataSource();
}
@Bean(name ="datasource")
@Primary
publicDynamicDataSource dynamicDataSource(@Qualifier(value ="datasource1")DataSource dataSource1,@Qualifier(value ="datasource2")DataSource dataSource2){
DynamicDataSource bean =newDynamicDataSource();
Map<Object,Object> targetDataSources =newHashMap<>();
targetDataSources.put("datasource1",dataSource1);
targetDataSources.put("datasource2", dataSource2);
bean.setTargetDataSources(targetDataSources);
bean.setDefaultTargetDataSource(dataSource1);
return bean;
}
@Bean(name ="sessionFactory1")
@ConfigurationProperties(prefix ="mybatis-plus1")
@ConfigurationPropertiesBinding()
@Primary
publicMybatisSqlSessionFactoryBean sqlSessionFactory1(@Qualifier(value ="datasource")DataSource dataSource){
MybatisSqlSessionFactoryBean bean =newMybatisSqlSessionFactoryBean();
bean.setDataSource(dataSource);
return bean;
}
@Bean(name ="sessionFactory2")
@ConfigurationProperties(prefix ="mybatis-plus2")
@ConfigurationPropertiesBinding()
publicMybatisSqlSessionFactoryBean sqlSessionFactory2(@Qualifier(value ="datasource")DataSource dataSource){
MybatisSqlSessionFactoryBean bean =newMybatisSqlSessionFactoryBean();
bean.setDataSource(dataSource);
return bean;
}
}
设置AOP用来自动切换数据源
在这里说明下我的目录结构:
com.zhangshuo.test1.controller/service/mapper
com.zhangshuo.test2.controller/service/mapper
设置AOP
在dao层进行切换
import com.zhangshuo.common.dataSource.DBTypeEnum;
import com.zhangshuo.common.dataSource.DbContextHolder;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
/**
* Created by Administrator on 2017/5/31 0031.
* 以dao层进行切换
*/
@Component
@Aspect
publicclassDataSourceInterceptor{
Logger logger =LoggerFactory.getLogger(DataSourceInterceptor.class);
@Pointcut(value ="execution(public * com.zhangshuo.test1.mapper.**.*(..))")
privatevoid datasource1ServicePointcut(){};
@Pointcut(value ="execution(public * com.zhangshuo.test2.mapper.**.*(..))")
privatevoid datasource2ServicePointcut(){};
/**
* 切换数据源1
*/
@Before("datasource1ServicePointcut()")
publicvoid dataSource1Interceptor(){
logger.debug("切换到数据源{}..............................","datasource1");
DbContextHolder.setDbType(DBTypeEnum.datasource1);
}
/**
* 切换数据源2
*/
@Before("datasource2ServicePointcut()")
publicvoid dataSource2Interceptor(){
logger.debug("切换到数据源{}.......................","datasource2");
DbContextHolder.setDbType(DBTypeEnum.datasource2);
}
}
到这里就可以进行测试了;
顺带贴下mybatis-plus的分页插件,我是定义到了mybatis-config.xml中,也可以手动在sqlSessionfacotry中的setPlugins中()定义;
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//ibatis.apache.org//DTD Config 3.0//EN"
"http://ibatis.apache.org/dtd/ibatis-3-config.dtd"> <configuration>
<settings>
<setting name="cacheEnabled" value="false"/>
<setting name="lazyLoadingEnabled" value="false"/>
<setting name="aggressiveLazyLoading" value="true"/>
<setting name="logImpl" value="slf4j"/>
</settings>
<plugins>
<plugin interceptor="com.baomidou.mybatisplus.plugins.PaginationInterceptor">
<property name="dialectType" value="mysql" />
<property name="optimizeType" value="aliDruid" />
</plugin>
</plugins>
</configuration>
spring boot 中 Mybatis plus 多数据源的配置方法的更多相关文章
- 【spring boot】【mybatis】spring boot中mybatis打印sql语句
spring boot中mybatis打印sql语句,怎么打印出来?[参考:https://www.cnblogs.com/sxdcgaq8080/p/9100178.html] 在applicati ...
- Spring Boot 实战 —— MyBatis(注解版)使用方法
原文链接: Spring Boot 实战 -- MyBatis(注解版)使用方法 简介 MyBatis 官网 是这么介绍它自己的: MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过 ...
- spring boot + druid + mybatis + atomikos 多数据源配置 并支持分布式事务
文章目录 一.综述 1.1 项目说明 1.2 项目结构 二.配置多数据源并支持分布式事务 2.1 导入基本依赖 2.2 在yml中配置多数据源信息 2.3 进行多数据源的配置 三.整合结果测试 3.1 ...
- Spring Boot中的Mongodb多数据源扩展
在日常工作中,我们通过Spring Data Mongodb来操作Mongodb数据库,在Spring Boot中只需要引入spring-boot-starter-data-mongodb即可. 然后 ...
- 太妙了!Spring boot 整合 Mybatis Druid,还能配置监控?
Spring boot 整合 Mybatis Druid并配置监控 添加依赖 <!--druid--> <dependency> <groupId>com.alib ...
- 徒手撸一个 Spring Boot 中的 Starter ,解密自动化配置黑魔法!
我们使用 Spring Boot,基本上都是沉醉在它 Stater 的方便之中.Starter 为我们带来了众多的自动化配置,有了这些自动化配置,我们可以不费吹灰之力就能搭建一个生产级开发环境,有的小 ...
- Spring Boot中实现logback多环境日志配置
在Spring Boot中,可以在logback.xml中的springProfile标签中定义多个环境logback.xml: <springProfile name="produc ...
- 利用 Spring Boot 中的 @ConfigurationProperties,优雅绑定配置参数
使用 @Value("${property}") 注释注入配置属性有时会很麻烦,尤其是当你使用多个属性或你的数据是分层的时候. Spring Boot 引入了一个可替换的方案 -- ...
- Spring Boot 集成 Mybatis 实现双数据源
这里用到了Spring Boot + Mybatis + DynamicDataSource配置动态双数据源,可以动态切换数据源实现数据库的读写分离. 添加依赖 加入Mybatis启动器,这里添加了D ...
随机推荐
- 为UWP应用开启回环访问权限
最近在项目中遇到UWP调用WCF的需求,考虑到UWP不能寄宿WCF服务(如果能,或者有类似技术,请告知),于是写了一个WPF程序寄宿WCF服务,然后再用UWP调用服务. 写的时候并没有碰到什么问题,直 ...
- ThreadPoolExecutor系列<一、ThreadPoolExecutor 机制>
本文系作者原创,转载请注明出处:http://www.cnblogs.com/further-further-further/p/7681529.html 解决问题: 1. 处理大量异步任务时能减少每 ...
- 【NOIP2015提高组】 Day1 T3 斗地主
[题目描述] 牛牛最近迷上了一种叫斗地主的扑克游戏.斗地主是一种使用黑桃.红心.梅花.方片的A到K加上大小王的共54张牌来进行的扑克牌游戏.在斗地主中,牌的大小关系根据牌的数码表示如下:3<4& ...
- (转)MySQL存储过程/存储过程与自定义函数的区别
转自:http://www.cnblogs.com/caoruiy/p/4486249.html 语法: 创建存储过程: CREATE [definer = {user|current_user}] ...
- Hadoop Streaming详解
一: Hadoop Streaming详解 1.Streaming的作用 Hadoop Streaming框架,最大的好处是,让任何语言编写的map, reduce程序能够在hadoop集群上运行:m ...
- component及刚体rigidbody用法
关于getcomponent函数,rigidbody(2d)的嵌套关系及用法 1.getcomponent函数 在unity中脚本可以看成是可定义的组件,我们经常要访问同一对象或不同对象中的脚本,可以 ...
- echarts教程-asp.net+ashx实现堆积柱状
说说看.崔西莲夫人紧接着说. 想不到史春吉是这种人. 你会这样说倒是有趣,因为这正是我当时的感觉.这跟奈维尔的个性不合.奈维尔,就像大部分男人一样,通常都是尽量避开任何可能造成尴尬或不愉快的场面.我怀 ...
- 使用selenium webdriver+beautifulsoup+跳转frame,实现模拟点击网页下一页按钮,抓取网页数据
记录一次快速实现的python爬虫,想要抓取中财网数据引擎的新三板板块下面所有股票的公司档案,网址为http://data.cfi.cn/data_ndkA0A1934A1935A1986A1995. ...
- js获取本地ip和地区
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Java基础笔记14
1.反射. Class:反射类 任何一个类都有一个Class反射类.(影子) java.lang.reflect.*; Field:字段类 Method:方法类类 Constructor:构造方法类. ...