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. CodeM Qualifying Match Q2

    问题描述: 组委会正在为美团点评CodeM大赛的决赛设计新赛制. 比赛有 n 个人参加(其中 n 为2的幂),每个参赛者根据资格赛和预赛.复赛的成绩,会有不同的积分. 比赛采取锦标赛赛制,分轮次进行, ...

  2. 设置macbook休眠模式

    前言: macbook默认合上盖默认是进入混合休眠模式模式(mode 3),此时电脑还会供电.不想耗电的话关机的话当前的工作状态就丢失了. macbook实际上是可以进入休眠模式的,只是没开放出来,我 ...

  3. Ubuntu中sublime和Foxit Reader不能使用中文输入法解决方案

    虽然Ubuntu下面很多软件同windows下一样,但是经常会出现各种各样的小问题,其中最让人头疼的是软件中的输入法问题. sublime作为一个跨平台的编辑软件,可以支持win,linux和mac系 ...

  4. java 堆和栈二

    1.数组 整数默认初始化值0 浮点数默认初始化值0.0 布尔类型默认初始化值false 字符类型默认初始化值\u0000 [I@7852e922 [有几个代表是几维数组 I代表是Int类型 @固定格式 ...

  5. 洛谷P2114 [NOI2014]起床困难综合症

    P2114 [NOI2014]起床困难综合症 题目描述 21世纪,许多人得了一种奇怪的病:起床困难综合症,其临床表现为:起床难,起床后精神不佳.作为一名青春阳光好少年,atm一直坚持与起床困难综合症作 ...

  6. luogu2679 [NOIp2015]子串 (dp)

    设f[i][j][k][b]表示在A串第i位.这是第j组.B串第k位.i号选不选(b=0/1) 那么就有$f[i][j][k][1]=(A[i]==B[k])*(f[i-1][j-1][k][0]+f ...

  7. 特别翔实的adaboost分类算法讲解 转的

    转https://www.cnblogs.com/litthorse/p/9332370.html 作为(曾)被认为两大最好的监督分类算法之一的adaboost元算法(另一个为前几节介绍过的SVM算法 ...

  8. 素数筛选法(prime seive)

    素数筛选法比较有名的,较常用的是Sieve of Eratosthenes,为古希腊数学家埃拉托色尼(Eratosthenes 274B.C.-194B.C.)提出的一种筛选法.详细步骤及图示讲解,还 ...

  9. ubuntu ssh root登陆

    原文:https://blog.csdn.net/wy_97/article/details/78294562 1.默认使用ubuntu用户登录,密码为服务器配置时设置的密码,可在重置密码中修改 2. ...

  10. 在ubuntu server上搭建Hadoop

    1. Java安装: Because everything work with java. $ sudo apt-get install openjdk-7-jdk 安装之后,可以查看java的版本信 ...