本篇文章小编给大家分享一下springboot的yml配置文件通过db2的方式整合mysql代码示例,文章代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。

springboot整合MySQL很简单,多数据源就master,slave就行了,但是在整合DB2就需要另起一行,以下是同一个yml文件

先配置MySQL,代码如下

spring:

datasource:

type: com.alibaba.druid.pool.DruidDataSource

druid:

# 主库数据源

master:

url: jdbc:mysql://localhost:3308/?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8

username: root

password: 123456

# 从库数据源

slave:

# 从数据源开关/默认关闭

enabled: true

url: jdbc:mysql://localhost:3308/?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8

username: root

password: 123456

# 初始连接数

initialSize: 5

# 最小连接池数量

minIdle: 10

# 最大连接池数量

maxActive: 20

# 配置获取连接等待超时的时间

maxWait: 60000

# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒

timeBetweenEvictionRunsMillis: 60000

# 配置一个连接在池中最小生存的时间,单位是毫秒

minEvictableIdleTimeMillis: 300000

# 配置一个连接在池中最大生存的时间,单位是毫秒

maxEvictableIdleTimeMillis: 900000

# 配置检测连接是否有效

validationQuery: SELECT 1 FROM DUAL

testWhileIdle: true

testOnBorrow: false

testOnReturn: false

webStatFilter:

enabled: true

statViewServlet:

enabled: true

# 设置白名单,不填则允许所有访问

allow:

url-pattern: /druid/*

# 控制台管理用户名和密码

login-username:

login-password:

filter:

stat:

enabled: true

# 慢SQL记录

log-slow-sql: true

slow-sql-millis: 1000

merge-sql: true

wall:

config:

multi-statement-allow: true

接下来配置DB2

second:

spring:

datasource:

type: com.alibaba.druid.pool.DruidDataSource

driver-class-name: com.ibm.db2.jcc.DB2Driver

url: jdbc:db2://:/:currentSchema=;

username:

password:

# 初始连接数

initialSize: 5

# 最小连接池数量

minIdle: 10

# 最大连接池数量

maxActive: 20

# 配置获取连接等待超时的时间

maxWait: 60000

# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒

timeBetweenEvictionRunsMillis: 60000

# 配置一个连接在池中最小生存的时间,单位是毫秒

minEvictableIdleTimeMillis: 300000

# 配置一个连接在池中最大生存的时间,单位是毫秒

maxEvictableIdleTimeMillis: 900000

# 配置检测连接是否有效 注意这里DUAL是检测的表名,可以是当前schema下的任意一张表

validationQuery: SELECT 1 FROM ****

testWhileIdle: true

testOnBorrow: false

testOnReturn: false

webStatFilter:

enabled: true

statViewServlet:

enabled: true

# 设置白名单,不填则允许所有访问

allow:

url-pattern: /druid/*

# 控制台管理用户名和密码

login-username:

login-password:

filter:

stat:

enabled: true

# 慢SQL记录

log-slow-sql: true

slow-sql-millis: 1000

merge-sql: true

wall:

config:

multi-statement-allow: true

OK这样就能通过Config获取到了,下面是Config源码

package com.map.framework.config;

import java.io.IOException;

import java.util.HashMap;

import java.util.Map;

import javax.servlet.Filter;

import javax.servlet.FilterChain;

import javax.servlet.ServletException;

import javax.servlet.ServletRequest;

import javax.servlet.ServletResponse;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;

import org.springframework.boot.context.properties.ConfigurationProperties;

import org.springframework.boot.web.servlet.FilterRegistrationBean;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.context.annotation.Primary;

import com.alibaba.druid.pool.DruidDataSource;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;

import com.alibaba.druid.spring.boot.autoconfigure.properties.DruidStatProperties;

import com.alibaba.druid.util.Utils;

import com.map.common.enums.DataSourceType;

import com.map.common.utils.spring.SpringUtils;

import com.map.framework.config.properties.DruidProperties;

import com.map.framework.datasource.DynamicDataSource;

import org.springframework.jdbc.datasource.DataSourceTransactionManager;

/**

* druid 配置多数据源

*

*

*/

@Configuration

public class DruidConfig

{

@Bean

@ConfigurationProperties("spring.datasource.druid.master")

public DataSource masterDataSource(DruidProperties druidProperties)

{

DruidDataSource dataSource = DruidDataSourceBuilder.create().build();

return druidProperties.dataSource(dataSource);

}

@Bean

@ConfigurationProperties("spring.datasource.druid.slave")

@ConditionalOnProperty(prefix = "spring.datasource.druid.slave", name = "enabled", havingValue = "true")

public DataSource slaveDataSource(DruidProperties druidProperties)

{

DruidDataSource dataSource = DruidDataSourceBuilder.create().build();

return druidProperties.dataSource(dataSource);

}

@Bean

@ConfigurationProperties("second.spring.datasource")

public DataSource db2DataSource(DruidProperties druidProperties)

{

DruidDataSource dataSource = DruidDataSourceBuilder.create().build();

return druidProperties.dataSource(dataSource);

}

@Bean(name = "dynamicDataSource")

@Primary

public DynamicDataSource dataSource(DataSource masterDataSource)

{

MaptargetDataSources = new HashMap<>();

targetDataSources.put(DataSourceType.MASTER.name(), masterDataSource);

setDataSource(targetDataSources, DataSourceType.SLAVE.name(), "slaveDataSource");

setDataSource(targetDataSources, DataSourceType.DB2.name(), "db2DataSource");

return new DynamicDataSource(masterDataSource, targetDataSources);

}

/**

* 设置数据源

*

* @param targetDataSources 备选数据源集合

* @param sourceName 数据源名称

* @param beanName bean名称

*/

public void setDataSource(MaptargetDataSources, String sourceName, String beanName)

{

try

{

DataSource dataSource = SpringUtils.getBean(beanName);

targetDataSources.put(sourceName, dataSource);

}

catch (Exception e)

{

}

}

}

db2 springboot 整合_springboot的yml配置文件通过db2的方式整合mysql代码示例相关推荐

  1. db2 springboot 整合_springboot的yml配置文件通过db2的方式整合mysql的教程

    springboot整合MySQL很简单,多数据源就master,slave就行了,但是在整合DB2就需要另起一行,以下是同一个yml文件 先配置MySQL,代码如下 spring: datasour ...

  2. .net后台怎么提取html中的多个图片的绝对地址_SpringBoot中yml配置文件说明和一些常用配置项说明...

    1. 配置文件说明 Springboot启动默认扫描的配置为classes目录下的application.yml 或者是 application.properties 我们项目中是使用的yml格式的配 ...

  3. Destoon数据库配置文件在哪_SpringBoot中yml配置文件说明和一些常用配置项说明

    1. 配置文件说明 Springboot启动默认扫描的配置为classes目录下的application.yml 或者是 application.properties 我们项目中是使用的yml格式的配 ...

  4. SpringBoot - application.yml配置文件中yes/no,on/off在代码中读取的值为true/false

    写在前面 在SpringBoot的项目中,当在配置文件中配置的值为yes/no或者on/off时,在SpringBoot内部解析时会将yes/no,on/off解析为true/false. 参数配置 ...

  5. springboot中如何获取yml配置文件中的配置信息

    yml 取值 import org.springframework.beans.factory.annotation.Value; import org.springframework.stereot ...

  6. c++读取txt文件中的数字_SpringBoot 多种读取配置文件中参数的方式

    点击上方☝SpringForAll社区 轻松关注!及时获取有趣有料的技术文章 本文来源:http://www.mydlq.club/article/61/ . 一.简介 . 1.SpringBoot ...

  7. 对spring boot yml配置文件敏感信息加密处理的两种方式

    目录 方式一:手动配置加密处理(手动配置分三种情况) 方式二:spring boot整合Jasypt实现yml配置文件敏感信息加密 yml配置文件敏感信息无非就是数据库密码,redis密码,以及整合的 ...

  8. 继承redis spring_实例讲解Springboot以Repository方式整合Redis

    1 简介 Redis是高性能的NoSQL数据库,经常作为缓存流行于各大互联网架构中.本文将介绍如何在Springboot中整合Spring Data Redis,使用Repository的方式操作. ...

  9. SpringMVC学习(六)——Spring四种方式整合MyBatis

    文章目录 1.引言 2.Spring整合Mybatis 2.1.常规整合 2.1.1.项目的结构 2.1.2.applicationContext.xml配置 2.1.3.UserInfoMapper ...

最新文章

  1. 活久见!如何看待北京理工大学某硕士生被指几乎一字不差地抄袭论文?
  2. 同步等待异步操作,为什么Wait()在这里冻结程序
  3. Apache 配置的性能调优
  4. linux route命令详解
  5. webuploader在bootstrap模态对话框中选择文件按钮无效的问题
  6. vue2.0 点击跳转传参--vue路由跳转传参数
  7. bzoj4543. [POI2014]Hotel加强版
  8. 获取freemarker处理后的内容
  9. eclipse Maven配置
  10. Node.js:Node核心模块
  11. Linux下创建虚拟软盘镜像
  12. 【算法基础一】字符编码分类
  13. Mendeley如何设置某期刊对应的参考文献格式?
  14. android q mix3,Android Q+5G 小米MIX3现场播放8K视频
  15. C++二进制文件读写,以及数据的拼合与还原
  16. APUE-文件和目录(六)函数ftw和nftw
  17. windows server 一键启用图片查看器
  18. 3-24 浅谈多元正态分布的基本性质
  19. picker多选 vant_vant的Picker 选择器
  20. HTML中的长度单位px、em、rem

热门文章

  1. curl下载失败返回0_curl返回常见错误码
  2. 构建空列表的两种法是_Python 基础3之列表
  3. CTF(pwn) Fastbin Attack
  4. Python基础教程:内置类型之数值
  5. Python3 中hasattr()、getattr()、setattr()、delattr()函数及示例代码数
  6. Python教程:collections的deque()方法
  7. linux怎么连续退回上次目录?cd - 只能在两个目录间来回切换(autojump)(pushd popd dirs)
  8. C语言 enum和typedef enum的区别
  9. socket编程为什么要选择AF_INET?
  10. 元素class属性中的空格