Springboot 多源mysql连接

Pom引用

org.springframework.boot

spring-boot-starter-parent

2.0.4.RELEASE

UTF-8

UTF-8

1.8

org.springframework.boot

spring-boot-starter

org.springframework.boot

spring-boot-starter-test

test

org.springframework.boot

spring-boot-starter-mail

org.springframework.boot

spring-boot-starter-freemarker


   

org.springframework

spring-test

5.0.9.RELEASE

test

org.springframework.boot

spring-boot-test

junit

junit

4.12

org.springframework

spring-test

5.0.8.RELEASE

compile

org.springframework.boot

spring-boot-starter-freemarker

org.springframework

spring-jdbc

5.0.7.RELEASE

org.springframework.boot

spring-boot-starter-jdbc

mysql

mysql-connector-java

runtime

org.springframework.boot

spring-boot-starter-test

test

log4j

log4j

1.2.17

com.github.ulisesbocchio

jasypt-spring-boot-starter

2.1.0

org.springframework.boot

spring-boot-starter-thymeleaf

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-configuration-processor

true

Mysql数据源

spring.datasource.primary.jdbc-url=jdbc:mysql://XX.XX.XX.XX:3306/XX?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useCompression=true&useSSL=false

spring.datasource.primary.username=XX

spring.datasource.primary.password=XX

spring.datasource.primary.max-idle=10

spring.datasource.primary.max-wait=10000

spring.datasource.primary.min-idle=5

spring.datasource.primary.initial-size=5

#spring.datasource.primary.driver-class-name=com.mysql.cj.jdbc.Driver

spring.datasource.secondary.jdbc-url=jdbc:mysql://192.168.XX.XX:3306/XX?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useCompression=true&useSSL=false

spring.datasource.secondary.username=XX

spring.datasource.secondary.password=X

spring.datasource.secondary.max-idle=10

spring.datasource.secondary.max-wait=10000

spring.datasource.secondary.min-idle=5

spring.datasource.secondary.initial-size=5

双源配置DataSourceConfig


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

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

import org.springframework.boot.jdbc.DataSourceBuilder;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.context.annotation.Primary;

import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;

/**

* @Author: Liuyong

* @Description:

* @Time:2019/6/21 15:20

*/

@Configuration

public class DataSourceConfig {

@Bean(name ="primaryDataSource")

@Qualifier("primaryDataSource")

@ConfigurationProperties(prefix="spring.datasource.primary")

public DataSource primaryDataSource() {

return DataSourceBuilder.create().build();

}

@Bean(name ="secondaryDataSource")

@Qualifier("secondaryDataSource")

@Primary

    @ConfigurationProperties(prefix="spring.datasource.secondary")

public DataSource secondaryDataSource() {

return DataSourceBuilder.create().build();

}

@Bean(name ="primaryJdbcTemplate")

public JdbcTemplate primaryJdbcTemplate(

@Qualifier("primaryDataSource") DataSource dataSource) {

return new JdbcTemplate(dataSource);

}

@Bean(name ="secondaryJdbcTemplate")

public JdbcTemplate secondaryJdbcTemplate(

@Qualifier("secondaryDataSource") DataSource dataSource) {

return new JdbcTemplate(dataSource);

}

}


双源调用:

@Autowired

@Qualifier("primaryJdbcTemplate")

protected JdbcTemplatejdbcTemplate1;

@Autowired

@Qualifier("secondaryJdbcTemplate")

protected JdbcTemplatejdbcTemplate2;

你可能感兴趣的:(Springboot 多源mysql连接)