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多数据源配置的更多相关文章

  1. springboot mybatis 多数据源配置

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

  2. springboot mybatis 多数据源配置支持切换以及一些坑

    一 添加每个数据源的config配置,单个直接默认,多个需要显示写出来 @Configuration @MapperScan(basePackages ="com.zhuzher.*.map ...

  3. springboot+ibatis 多数据源配置

    这个是boot基本版本包,因为我用的打包方式是war所以去除掉了boot内置的tomcat,但是为了方便测试又引入了内置tomcat,只要添加<scope>provided</sco ...

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

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

  5. springboot + mybatis + 多数据源

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

  6. Spring Boot 2.X(五):MyBatis 多数据源配置

    前言 MyBatis 多数据源配置,最近在项目建设中,需要在原有系统上扩展一个新的业务模块,特意将数据库分库,以便减少复杂度.本文直接以简单的代码示例,如何对 MyBatis 多数据源配置. 准备 创 ...

  7. springmvc+mybatis多数据源配置,AOP注解动态切换数据源

    springmvc与springboot没多大区别,springboot一个jar包配置几乎包含了所有springmvc,也不需要繁琐的xml配置,springmvc需要配置多种jar包,需要繁琐的x ...

  8. MyBatis多数据源配置(读写分离)

    原文:http://blog.csdn.net/isea533/article/details/46815385 MyBatis多数据源配置(读写分离) 首先说明,本文的配置使用的最直接的方式,实际用 ...

  9. 基于springboot的多数据源配置

    发布时间:2018-12-11   技术:springboot1.5.1 + maven3.0.1+ mybatis-plus-boot-starter2.3.1 + dynamic-datasour ...

  10. SpringBoot 的多数据源配置

    最近在项目开发中,需要为一个使用 MySQL 数据库的 SpringBoot 项目,新添加一个 PLSQL 数据库数据源,那么就需要进行 SpringBoot 的多数据源开发.代码很简单,下面是实现的 ...

随机推荐

  1. Spring源码解析二:IOC容器初始化过程详解

    IOC容器初始化分为三个步骤,分别是: 1.Resource定位,即BeanDefinition的资源定位. 2.BeanDefinition的载入 3.向IOC容器注册BeanDefinition ...

  2. PhpStorm 配置本地断点调试

    前言: 有够拖延症的,应该是一年多以前就使用过PhpStorm的debug断点调试了吧,不够过当时是别人帮我配的,我记得还挺复杂.后来重装系统后尝试了配置,好像没成吧,记得当初老师帮我配也没成(... ...

  3. 第十周PSP&进度条

    PSP 一.表格: D日期     C类型 C内容 S开始时间 E结束时间 I时间间隔 T净时间(mins) 预计花费时间(mins) 11月17号 站立会议 分配任务 13:00 13:30 0 3 ...

  4. SQL Server Collation解惑

    某些产品会有固定的DB Collation,如果提前创建DB的时候没有按照要求指定对应的Collation,这个时候就会报错,提示你Collation不匹配.在安装SQL Server的时候有时候需要 ...

  5. Linux命令(十) 在文件或目录之间创建链接 ln

    命令简介 ln 命令用于连接文件或目录,如同时指定两个以上的文件或目录,且最后的目的地是一个已经存在的目录,则会把前面指定的所有文件或目录复制到该目录中.若同时指定多个文件或目录,且最后的目的地是一个 ...

  6. subversion & MacOS & Xcode 10

    subversion & MacOS Xcode 10 https://developer.apple.com/search/?q=subversion No SVN any more! ht ...

  7. 安装spring-tool-suite插件

    spring-tool-suite是一个非常好用的spring插件,由于eclipse是一个很简洁的IDE,因此许多插件,需要我们自己去手动下载.而Spring-tool-suite插件即是其中之一. ...

  8. POJ2125 Destroying The Graph

    题目链接:ヾ(≧∇≦*)ゝ 大致题意: 给出一个有向图D=(V,E).对于每个点U,定义两种操作a(u),b(u) 操作a(u):删除点U的所有出边,即属于E,操作花费为Ca(u). 操作b(u):删 ...

  9. python里使用正则表达式的非贪婪模式

    在正则表达式里,什么是正则表达式的贪婪与非贪婪匹配 如:String str="abcaxc"; Patter p="ab*c"; 贪婪匹配:正则表达式一般趋向 ...

  10. 解决 Microsoft Excel has stopped working

    安装了project 2013后, excel 2013 一打开文件就报这个错,解决方法如下: Step1: Try to repair Office 2013 installation and ch ...