这块是比较基础的配置,阿里数据库配置还是比较好用的,并且可以用来监控数据源的情况。废话不多说,下面看代码。

基于maven项目,在pom.xml中添加引用:

        <dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.11</version>
</dependency>

配置文件如下:

server.port=9090
logging.level.tk.mybatis=TRACE druid1.type=com.alibaba.druid.pool.DruidDataSource
druid1.url=jdbc:mysql://192.168.0.190:3306/bestPractices
druid1.driver-class=com.mysql.jdbc.Driver
druid1.username=root
druid1.password=youeDATA2016_
druid1.initial-size=1
druid1.min-idle=1
druid1.max-active=20
druid1.test-on-borrow=true
druid1.filters=stat,wall,log4j
druid1.poolPreparedStatements=true
druid1.datasource.maxPoolPreparedStatementPerConnectionSize=20
druid1.datasource.logAbandoned=true druid2.type=com.alibaba.druid.pool.DruidDataSource
druid2.url=jdbc:mysql://192.168.0.190:3306/bestPracticesTwo
druid2.driver-class=com.mysql.jdbc.Driver
druid2.username=root
druid2.password=youeDATA2016_
druid2.initial-size=1
druid2.min-idle=1
druid2.max-active=20
druid2.test-on-borrow=true
druid2.filters=stat,wall,log4j
druid2.poolPreparedStatements=true
druid2.datasource.maxPoolPreparedStatementPerConnectionSize=20
druid2.datasource.logAbandoned=true spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
mybatis.type-aliases-package=bp.model
mybatis.mapper-locations=classpath:mybatis/mybatis-config.xml
mapper.mappers=bp.util.MyMapper
#mapper.not-empty=false
#mapper.identity=MYSQL
pagehelper.helperDialect=mysql
pagehelper.reasonable=true
pagehelper.supportMethodsArguments=true
pagehelper.dialect=com.github.pagehelper.dialect.helper.MySqlDialect
pagehelper.params=count=countSql
pagehelper.autoDialect=true
pagehelper.closeConn=false
pagehelper.offsetAsPageNum=true spring.http.encoding.force=true
spring.redis.database= 0
spring.redis.host= 192.168.237.128
spring.redis.port= 6379
spring.redis.pool.max-active= 8
spring.redis.pool.max-idle= 8
spring.redis.pool.max-wait=-1
spring.redis.pool.min-idle=0
spring.redis.timeout= 1500

配置文件:数据源1

package bp.config;

import java.sql.SQLException;

import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager; import com.alibaba.druid.pool.DruidDataSource; /**
* 数据源1
*/
@Configuration
@EnableConfigurationProperties(DruidProperties1.class)
@AutoConfigureBefore(DataSourceAutoConfiguration.class)
@ConditionalOnClass(DruidDataSource.class)
@MapperScan(basePackages = "bp.mapper.mapper1", sqlSessionTemplateRef = "SqlSessionTemplate1")
public class DataSource1Config {
@Autowired
private DruidProperties1 properties; @Bean(name = "DataSource1")
@ConditionalOnProperty(prefix = "druid1", name = "url")
@Primary
public DataSource dataSource() throws SQLException {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setUrl(properties.getUrl());
dataSource.setUsername(properties.getUsername());
dataSource.setPassword(properties.getPassword());
dataSource.setFilters(properties.getFilters());
if (properties.getInitialSize() > 0) {
dataSource.setInitialSize(properties.getInitialSize());
}
if (properties.getMinIdle() > 0) {
dataSource.setMinIdle(properties.getMinIdle());
}
if (properties.getMaxActive() > 0) {
dataSource.setMaxActive(properties.getMaxActive());
}
dataSource.setTestOnBorrow(properties.isTestOnBorrow());
try {
dataSource.init();
} catch (SQLException e) {
throw new RuntimeException(e);
}
return dataSource;
} @Bean(name = "SqlSessionFactory1")
@Primary
public SqlSessionFactory test1SqlSessionFactory(@Qualifier("DataSource1") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
bean.setDataSource(dataSource);
bean.setConfigLocation(resolver.getResource("classpath:mybatis/mybatis-config.xml"));
bean.setMapperLocations(resolver.getResources("bp/mapper/mapper1/*.xml"));
return bean.getObject();
} @Bean(name = "TransactionManager1")
@Primary
public DataSourceTransactionManager test1TransactionManager(@Qualifier("DataSource1") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
} @Bean(name = "SqlSessionTemplate1")
@Primary
public SqlSessionTemplate test1SqlSessionTemplate(@Qualifier("SqlSessionFactory1") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
} }

数据源2:

package bp.config;

import java.sql.SQLException;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager; import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import com.alibaba.druid.support.spring.stat.DruidStatInterceptor; import javax.annotation.Resource;
import javax.sql.DataSource; /**
* 数据源2
*/
@Configuration
@EnableConfigurationProperties(DruidProperties2.class)
@AutoConfigureBefore(DataSourceAutoConfiguration.class)
@ConditionalOnClass(DruidDataSource.class)
@MapperScan(basePackages = "bp.mapper.mapper2", sqlSessionTemplateRef = "SqlSessionTemplate2")
public class DataSource2Config { @Autowired
private DruidProperties2 properties; @Bean(name = "DataSource2")
@ConditionalOnProperty(prefix = "druid2", name = "url")
public DataSource dataSource() throws SQLException {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setUrl(properties.getUrl());
dataSource.setUsername(properties.getUsername());
dataSource.setPassword(properties.getPassword());
dataSource.setFilters(properties.getFilters());
if (properties.getInitialSize() > 0) {
dataSource.setInitialSize(properties.getInitialSize());
}
if (properties.getMinIdle() > 0) {
dataSource.setMinIdle(properties.getMinIdle());
}
if (properties.getMaxActive() > 0) {
dataSource.setMaxActive(properties.getMaxActive());
}
dataSource.setTestOnBorrow(properties.isTestOnBorrow());
try {
dataSource.init();
} catch (SQLException e) {
throw new RuntimeException(e);
}
return dataSource;
} @Bean(name = "SqlSessionFactory2")
public SqlSessionFactory test2SqlSessionFactory(@Qualifier("DataSource2") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
bean.setDataSource(dataSource);
bean.setConfigLocation(resolver.getResource("classpath:mybatis/mybatis-config.xml"));
bean.setMapperLocations(resolver.getResources("bp/mapper/mapper2/*.xml"));
return bean.getObject();
} @Bean(name = "TransactionManager2")
public DataSourceTransactionManager test2TransactionManager(@Qualifier("DataSource2") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
} @Bean(name = "SqlSessionTemplate2")
public SqlSessionTemplate test2SqlSessionTemplate(@Qualifier("SqlSessionFactory2") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
} }

启动项目就发现是双数据源了

spring boot 基础篇 -- 阿里多数据源的更多相关文章

  1. spring boot 基础篇 -- 定时任务

    在日常项目中,常常会碰到定时监控项目中某个业务的变化,下面是spring boot 集成的定时任务具体配置: @Component public class IndexWarningScheduled ...

  2. spring boot 基础篇 -- 集成接口测试Swagger

    一.在pom.xml加入Swagger jar包引入 <dependency> <groupId>io.springfox</groupId> <artifa ...

  3. spring boot 基础篇 -- 自带图片服务器

    我们平时在日常项目中经常会遇到图片的上传和访问的情景,平时我们可能习惯于把图片传到resource或者项项目中的某个位置,这样会有一个缺点,当我们重新项目打包时,这些图片会丢失.为了解决这一缺点,我们 ...

  4. spring boot基础学习教程

    Spring boot 标签(空格分隔): springboot HelloWorld 什么是spring boot Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新 ...

  5. Spring Boot 基础

    Spring Boot 基础 Spring Boot 项目(参考1) 提供了一个类似ASP.NET MVC的默认模板一样的标准样板,直接集成了一系列的组件并使用了默认的配置.使用Spring Boot ...

  6. Spring Boot 基础教程系列学习文档

    Spring Boot基础教程1-Spring Tool Suite工具的安装 Spring Boot基础教程2-RESTfull API简单项目的快速搭建 Spring Boot基础教程3-配置文件 ...

  7. Spring Boot HikariCP 一 ——集成多数据源

    其实这里介绍的东西主要是参考的另外一篇文章,数据库读写分离的. 参考文章就把链接贴出来,里面有那位的代码,简单明了https://gitee.com/comven/dynamic-datasource ...

  8. Spring Boot数据访问之动态数据源切换之使用注解式AOP优化

    在Spring Boot数据访问之多数据源配置及数据源动态切换 - 池塘里洗澡的鸭子 - 博客园 (cnblogs.com)中详述了如何配置多数据源及多数据源之间的动态切换.但是需要读数据库的地方,就 ...

  9. Spring boot 提高篇

    Spring boot 提高篇 上篇文章介绍了Spring boot初级教程:构建微服务:Spring boot 入门篇,方便大家快速入门.了解实践Spring boot特性:本篇文章接着上篇内容继续 ...

随机推荐

  1. Wireshark网络分析工具(二)

    一.TCP三次握手过称 1. 第一次握手的数据包 客户端发送一个TCP,标志位为SYN,序列号为0, 代表客户端请求建立连接. 如下图: 2. 第二次握手的数据包 服务器发回确认包, 标志位为 SYN ...

  2. 回顾: Python面向对象三大特性

    继承 待整理^_^ 封装 待整理^_^ 多态 待整理^_^

  3. B-Tree vs LSM-tree

    什么是B-树 一.已排序文件的查找时间 对一个有N笔记录的已排序表进行二叉查找,可以在O(log2N)比较级完成.如果表有1,000,000笔记录,那么定位其中一笔记录,将在20 ( log21,00 ...

  4. Selenium chrome配置不加载图片

    from selenium import webdriver chrome_options = webdriver.ChromeOptions() prefs = {"profile.man ...

  5. 命令行查看mysql的安装目录

    方法: 进入mysql命令行输入:show variables like "%char%"; 结果如下: 红色框框就是安装目录

  6. 003 F-47创建预付定金请求检查增强-20150819.docx

    Enhancement SE38:LEINRF26   操作F-47,预付定金请求回车时,检查输入的采购订单项目发票视图,预付定金% 栏位,若为空,则报错,不为空可继续.   检查逻辑:检查采购凭证项 ...

  7. ACM解题之素矩阵

    题意: 如果一个矩形的两条边都是素数,则称此矩形为素矩形.本题给出一个素矩形的面积,请计算其两条边的值.有多个测试用例.每个用例占一行,包含一个表示素矩形面积且不超过 108 的正整数.输入直至没有数 ...

  8. Use Private Members from Base Class

    最近看了一段代码:在导出类中调用继承自基类的某个public函数,该函数中对基类中的private数据成员 进行了赋值并将结果打印.看到程序的运行结果后,顿时感觉自己之前(我之前的理解这里就不说明了) ...

  9. android开发之网络棋牌类在线游戏开发心得(服务器端、Java) 好文章值得收藏

    标签: android服务器 2013-10-09 17:28 3618人阅读 评论(0) 收藏 举报 分类: android(11) 转自:http://blog.csdn.net/bromon/a ...

  10. android 加固防止反编译-重新打包

    http://blog.csdn.net/u010921385/article/details/52505094 1.需要加密的Apk(源Apk) 2.壳程序Apk(负责解密Apk工作) 3.加密工具 ...