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 的多数据源开发.代码很简单,下面是实现的 ...
随机推荐
- 使用Webdriver刷博客文章评论
package com.zhc.webdriver; import java.util.ArrayList; import java.util.Iterator; import java.util.c ...
- 4 vuex的安装
安装可以看,引入又问题https://blog.csdn.net/u014196765/article/details/78022065?locationNum=9&fps=1(引入) htt ...
- hexo d 报错‘fatal: could not read Username for 'https://github.com': No error’
问题描述 今天早上,一如往常的往在github上创建的hexo博客上传文章,结果报错 'fatal: could not read Username for 'https://github.com': ...
- pgm8
前面的近似策略是寻找了 energy functional 的近似,该近似导致了 LBP,这使得 message passing 的算法不变.近似使用 I-projection,尽管这个一般说来并不容 ...
- 【笔记】自学ST表笔记
自学ST表笔记 说实话原先QBXT学的ST表忘的差不多了吧...... 我重新自学巩固一下(回忆一下) 顺便把原先一些思想来源的原博发上来 一.ST表简介 ST表,建表时间\(O(n\cdot log ...
- MySQL 5.7 GA 新特性
转载自: http://www.chinaxing.org/articles/Database/2015/10/23/2015-10-22-mysql-5.7.html sys-schema http ...
- bzoj 4631: 踩气球 线段树合并
4631: 踩气球 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 265 Solved: 136[Submit][Status][Discuss] ...
- 鸟哥的Linux私房菜——第十章
视频链接 土豆网:http://www.tudou.com/programs/view/YI5fpob0Wwk B站(推荐):http://www.bilibili.com/video/av98064 ...
- iOS11有哪些新功能?旧iPhone是否真的变慢了
1. [iOS 11] iOS 11十大实用新功能简介 2.[iOS 11] iPhone二维码扫描,通过内建相机就可以完成! 3. iOS 11内建屏幕录制功能!再也不需要通过第三方应用录屏 4. ...
- bzoj千题计划291:bzoj3640: JC的小苹果
http://www.lydsy.com/JudgeOnline/problem.php?id=3640 dp[i][j] 表示i滴血到达j的概率 dp[i][j] = Σ dp[i+val[i]][ ...