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 的多数据源开发.代码很简单,下面是实现的 ...
随机推荐
- .net 开源组件推荐 之 StackExchange
已经两年没更新过博客了!!! StackExchange,地址:https://github.com/StackExchange,开源的这些项目都是在StackOverflow线上使用的. 说起Sta ...
- js css样式操作代码(批量操作)
js css样式操作代码(批量操作) 作者: 字体:[增加 减小] 类型:转载 时间:2009-10-09 用js控制css样式,能让网页达到良好的的用户体验甚至是动画的效果.并且考虑到效率. ...
- sublime text3 增加emmett插件
本内容基于Windows环境)一.已安装有Sublime Text3 二.安装Package Control 安装这个后,可以在线安装所需的插件 方法1.Ctrl+~打开控制台,在控制台输 ...
- Linux命令(二十二) 改变文件权限 chomd
目录 1.命令简介 2.常用参数介绍 3.实例 4.直达底部 命令简介 chmod 命令是用来改变文件权限或目录的命令,可以将指定文件的拥有着改为指定的用户或组,用户可以是用户名或用户ID,组可以是组 ...
- 3.23日PSP
工作 类型 日期 开始时间 结束时间 中断时间 净时间 搭hadoop环境(已终止) 技能 3.23 00:00 00:50 0min 50min 看构建之法 学习 3.23 9:30 10:00 3 ...
- 安装spring-tool-suite插件
spring-tool-suite是一个非常好用的spring插件,由于eclipse是一个很简洁的IDE,因此许多插件,需要我们自己去手动下载.而Spring-tool-suite插件即是其中之一. ...
- SP2713 GSS4
题目链接 这是一道假题,表面上看起来,好像使用了什么奇妙的操作,其实就是一个无脑暴力 我们会发现,即使是\(1e18\),在开方\(6\)次之后也已经变成了\(1\),而\(1\)再怎么开方还是\(1 ...
- CSUOJ 1170 A sample problem
J: A Simple Problem Submit Page Time Limit: 1 Sec Memory Limit: 128 Mb Submitted: 87 S ...
- web开发中的跨域整理
1.springboot通过CROS实现跨域: https://www.cnblogs.com/520playboy/p/7306008.html springboot下各种跨域方式: http:// ...
- vue+webpack开发(一)
一开始接触这个vue+webpack的时候,实在是摸不着头脑,根本无从下手. 但是经过这两天的研究,其实你会发现vue其实并不难,难度都在webpack你对webpack的理解. webpack顾名思 ...