springboot:mybatis多数据源配置
1.application.properties
#CMS数据源(主库)
spring.datasource.cms.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.cms.url=jdbc:mysql://192.168.2.21:3306/cms?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull
spring.datasource.cms.username=root
spring.datasource.cms.password=root123!@#
spring.datasource.cms.max-active=20
spring.datasource.cms.initial-size=1
spring.datasource.cms.min-idle=3
spring.datasource.cms.max-wait=60000
spring.datasource.cms.time-between-eviction-runs-millis=60000
spring.datasource.cms.min-evictable-idle-time-millis=300000
spring.datasource.cms.test-while-idle=true
spring.datasource.cms.test-on-borrow=false
spring.datasource.cms.test-on-return=false
spring.datasource.cms.poolPreparedStatements=true
spring.datasource.cms.filters=stat,wall,slf4j #BASE数据源
spring.datasource.base.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.base.url=jdbc:mysql://192.168.2.21:3306/base?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull
spring.datasource.base.username=root
spring.datasource.base.password=root123!@#
spring.datasource.base.max-active=20
spring.datasource.base.initial-size=1
spring.datasource.base.min-idle=3
spring.datasource.base.max-wait=60000
spring.datasource.base.time-between-eviction-runs-millis=60000
spring.datasource.base.min-evictable-idle-time-millis=300000
spring.datasource.base.test-while-idle=true
spring.datasource.base.test-on-borrow=false
spring.datasource.base.test-on-return=false
spring.datasource.base.poolPreparedStatements=true
spring.datasource.base.filters=stat,wall,slf4j
2.配置主数据源
import java.sql.SQLException; import javax.sql.DataSource; import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement; import com.alibaba.druid.pool.DruidDataSource; import net.common.utils.constant.ConstantCms; @Configuration
@EnableTransactionManagement
@MapperScan(basePackages = ConstantCms.MAPPER_PACKAGE, sqlSessionTemplateRef = "cmsSqlSessionTemplate")
public class ConfigCmsDataSource implements EnvironmentAware { private Logger logger = LoggerFactory.getLogger(ConfigCmsDataSource.class); private RelaxedPropertyResolver propertyResolver; @Override
public void setEnvironment(Environment env) {
this.propertyResolver = new RelaxedPropertyResolver(env, "spring.datasource.cms.");
} @Bean(name = "cmsDataSource")
@Primary
public DataSource cmsDataSource() {
DruidDataSource datasource = new DruidDataSource();
datasource.setUrl(propertyResolver.getProperty("url"));
datasource.setDriverClassName(propertyResolver.getProperty("driver-class-name"));
datasource.setUsername(propertyResolver.getProperty("username"));
datasource.setPassword(propertyResolver.getProperty("password"));
if (StringUtils.isNotBlank(propertyResolver.getProperty("initial-size"))) {
datasource.setInitialSize(Integer.valueOf(propertyResolver.getProperty("initial-size")));
}
if (StringUtils.isNotBlank(propertyResolver.getProperty("min-idle"))) {
datasource.setMinIdle(Integer.valueOf(propertyResolver.getProperty("min-idle")));
}
if (StringUtils.isNotBlank(propertyResolver.getProperty("max-wait"))) {
datasource.setMaxWait(Integer.valueOf(propertyResolver.getProperty("max-wait")));
}
if (StringUtils.isNotBlank(propertyResolver.getProperty("max-active"))) {
datasource.setMaxActive((Integer.valueOf(propertyResolver.getProperty("max-active"))));
}
if (StringUtils.isNotBlank(propertyResolver.getProperty("min-evictable-idle-time-millis"))) {
datasource.setMinEvictableIdleTimeMillis(
Integer.valueOf(propertyResolver.getProperty("min-evictable-idle-time-millis")));
}
try {
datasource.setFilters("stat,wall");
} catch (SQLException e) {
logger.error("初始化BASE数据库连接池发生异常:{}", e.toString());
}
return datasource;
} @Bean(name = "cmsSqlSessionFactory")
@Primary
public SqlSessionFactory cmsSqlSessionFactory(@Qualifier("cmsDataSource") DataSource cmsDataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(cmsDataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(ConstantCms.MAPPER_XML_PACKAGE));
return bean.getObject();
} @Bean(name = "cmsTransactionManager")
@Primary
public DataSourceTransactionManager cmsTransactionManager(@Qualifier("cmsDataSource") DataSource cmsDataSource) {
return new DataSourceTransactionManager(cmsDataSource);
} @Bean(name = "cmsSqlSessionTemplate")
@Primary
public SqlSessionTemplate cmsSqlSessionTemplate(
@Qualifier("cmsSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
} }
2.配置从数据源:没有 @Primary
import java.sql.SQLException; import javax.sql.DataSource; import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement; import com.alibaba.druid.pool.DruidDataSource; import net.common.utils.constant.ConstantBase; @Configuration
@EnableTransactionManagement
@MapperScan(basePackages = ConstantBase.MAPPER_PACKAGE, sqlSessionTemplateRef = "baseSqlSessionTemplate")
public class ConfigBaseDataSource implements EnvironmentAware { private Logger logger = LoggerFactory.getLogger(ConfigBaseDataSource.class); private RelaxedPropertyResolver propertyResolver; @Override
public void setEnvironment(Environment env) {
this.propertyResolver = new RelaxedPropertyResolver(env, "spring.datasource.base.");
} @Bean(name = "baseDataSource")
public DataSource baseDataSource() {
DruidDataSource datasource = new DruidDataSource();
datasource.setUrl(propertyResolver.getProperty("url"));
datasource.setDriverClassName(propertyResolver.getProperty("driver-class-name"));
datasource.setUsername(propertyResolver.getProperty("username"));
datasource.setPassword(propertyResolver.getProperty("password"));
if (StringUtils.isNotBlank(propertyResolver.getProperty("initial-size"))) {
datasource.setInitialSize(Integer.valueOf(propertyResolver.getProperty("initial-size")));
}
if (StringUtils.isNotBlank(propertyResolver.getProperty("min-idle"))) {
datasource.setMinIdle(Integer.valueOf(propertyResolver.getProperty("min-idle")));
}
if (StringUtils.isNotBlank(propertyResolver.getProperty("max-wait"))) {
datasource.setMaxWait(Integer.valueOf(propertyResolver.getProperty("max-wait")));
}
if (StringUtils.isNotBlank(propertyResolver.getProperty("max-active"))) {
datasource.setMaxActive((Integer.valueOf(propertyResolver.getProperty("max-active"))));
}
if (StringUtils.isNotBlank(propertyResolver.getProperty("min-evictable-idle-time-millis"))) {
datasource.setMinEvictableIdleTimeMillis(
Integer.valueOf(propertyResolver.getProperty("min-evictable-idle-time-millis")));
}
try {
datasource.setFilters("stat,wall");
} catch (SQLException e) {
logger.error("初始化BASE数据库连接池发生异常:{}", e.toString());
}
return datasource;
} @Bean(name = "baseSqlSessionFactory")
public SqlSessionFactory baseSqlSessionFactory(@Qualifier("baseDataSource") DataSource baseDataSource)
throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(baseDataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(ConstantBase.MAPPER_XML_PACKAGE));
return bean.getObject();
} @Bean(name = "baseTransactionManager")
public DataSourceTransactionManager baseTransactionManager(@Qualifier("baseDataSource") DataSource baseDataSource) {
return new DataSourceTransactionManager(baseDataSource);
} @Bean(name = "baseSqlSessionTemplate")
public SqlSessionTemplate baseSqlSessionTemplate(
@Qualifier("baseSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
} }
3.注意扫描包路径和XML路径,红色标注部分,改成自己的。
其中XML路径:classpath*:net/common/cms/mapper/xml/**/*.xml,第一个星号必不可少。
springboot:mybatis多数据源配置的更多相关文章
- springboot mybatis 多数据源配置
首先导入mybatis等包,这里就不多说. 下面是配置多数据源和mybatis,每个数据源对应一套mybatis模板 数据源1: package com.aaaaaaa.config.datasour ...
- springboot mybatis 多数据源配置支持切换以及一些坑
一 添加每个数据源的config配置,单个直接默认,多个需要显示写出来 @Configuration @MapperScan(basePackages ="com.zhuzher.*.map ...
- springboot+ibatis 多数据源配置
这个是boot基本版本包,因为我用的打包方式是war所以去除掉了boot内置的tomcat,但是为了方便测试又引入了内置tomcat,只要添加<scope>provided</sco ...
- spring-boot (四) springboot+mybatis多数据源最简解决方案
学习文章来自:http://www.ityouknow.com/spring-boot.html 配置文件 pom包就不贴了比较简单该依赖的就依赖,主要是数据库这边的配置: mybatis.confi ...
- springboot + mybatis + 多数据源
此文已由作者赵计刚薪授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验 在实际开发中,我们一个项目可能会用到多个数据库,通常一个数据库对应一个数据源. 代码结构: 简要原理: 1) ...
- Spring Boot 2.X(五):MyBatis 多数据源配置
前言 MyBatis 多数据源配置,最近在项目建设中,需要在原有系统上扩展一个新的业务模块,特意将数据库分库,以便减少复杂度.本文直接以简单的代码示例,如何对 MyBatis 多数据源配置. 准备 创 ...
- springmvc+mybatis多数据源配置,AOP注解动态切换数据源
springmvc与springboot没多大区别,springboot一个jar包配置几乎包含了所有springmvc,也不需要繁琐的xml配置,springmvc需要配置多种jar包,需要繁琐的x ...
- MyBatis多数据源配置(读写分离)
原文:http://blog.csdn.net/isea533/article/details/46815385 MyBatis多数据源配置(读写分离) 首先说明,本文的配置使用的最直接的方式,实际用 ...
- 基于springboot的多数据源配置
发布时间:2018-12-11 技术:springboot1.5.1 + maven3.0.1+ mybatis-plus-boot-starter2.3.1 + dynamic-datasour ...
- SpringBoot 的多数据源配置
最近在项目开发中,需要为一个使用 MySQL 数据库的 SpringBoot 项目,新添加一个 PLSQL 数据库数据源,那么就需要进行 SpringBoot 的多数据源开发.代码很简单,下面是实现的 ...
随机推荐
- dispatch_block_t
通常我写一个不带参数的块回调函数是这样写的 在 . h 头文件中 定义类型 typedef void (^leftBlockAction)(); 在定义一个回调函数 -(void)leftButton ...
- Tomcat下bootstrap启动分析
"C:\Program Files\Java\jdk1.7.0\bin\javaw.exe" -agentlib:jdwp=transport=dt_socket,suspend= ...
- Oracle 标准版 企业版 个人版的区别 转帖
转帖来源: https://blog.csdn.net/flg_inwind/article/details/2628133 同事方总:http://www.oracle.com/us/corpora ...
- 查看Jira 使用的H2数据库 数据结构以及内容的方法
1. 同事在研究jira 想看看jira的数据库 数据结构, 告知使用的是java的H2数据库. 如图示 2. 然后根据此内容 进行百度等. 下载 可以进行数据库连接的工具,主要找到两个,下载地址分别 ...
- thnkphp框架面试问题
Thinkphp面试问题 1.如何理解TP中的单一入口文件? 答:ThinkPHP采用单一入口模式进行项目部署和访问,无论完成什么功能,一个项目都有一个统一(但不一定是唯一)的入口.应该说,所有项目都 ...
- 在vue中使用weixin-js-sdk自定义微信分享效果
在做微信分享的时候,产品要求分享效果要有文字和图片,使用weixin-js-sdk解决了, 原始的分享效果: 使用微信JS-SDK的分享效果: 首先需要引入weixin-js-sdk npm inst ...
- 深入理解Vue的生命周期
谈到Vue的生命周期,相信许多人并不陌生.但大部分人和我一样,只是听过而已,具体用在哪,怎么用,却不知道.我在学习vue一个多礼拜后,感觉现在还停留在初级阶段,对于mounted这个挂载还不是很清楚. ...
- pgm5
这部分讨论 inference 里面基本的问题,即计算 这类 query,这一般可以认为等价于计算 ,因为我们只需要重新 normalize 一下关于 的分布就得到了需要的值,特别是像 MAP 这类 ...
- 使用System.getProperty方法,如何配置JVM系统属性
原创文章,欢迎转载,转载请注明出处! 很多时候我们需要在项目中读取外部属性文件,用到了System.getProperty("")方法.这个方法需要配置JVM系统属性,那么如何配置 ...
- 洛谷 P1437 [HNOI2004]敲砖块 解题报告
P1437 [HNOI2004]敲砖块 题目描述 在一个凹槽中放置了 n 层砖块.最上面的一层有n 块砖,从上到下每层依次减少一块砖.每块砖 都有一个分值,敲掉这块砖就能得到相应的分值,如下所示. 1 ...