最近有点忙,更新有点慢。今天进来说说一说springBoot中如何配置多数据源。

     第一,新建一个名为springBoot-mutidata的maven项目,完整的pom.xml配置如下:

springBoot与多数据源的配置_maven

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>springBoot-mutidata</groupId>
<artifactId>springBoot-mutidata</artifactId>
<version>1.0.1</version>
<packaging>war</packaging>

<name>springBoot-mutidata</name>
<description>Spring Boot project</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.2.RELEASE</version>
<relativePath/>
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>

springBoot与多数据源的配置_maven

第二,建立项目的启动项:Application.class

springBoot与多数据源的配置_maven

package com.didispace;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication
public class Application {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

}

springBoot与多数据源的配置_maven

第二:在application.properties中添加数据源的信息

springBoot与多数据源的配置_maven

spring.datasource.primary.url=jdbc:mysql://localhost:3306/test1
spring.datasource.primary.username=root
spring.datasource.primary.password=root
spring.datasource.primary.driver-class-name=com.mysql.jdbc.Driver

spring.datasource.secondary.url=jdbc:mysql://localhost:3306/test2
spring.datasource.secondary.username=root
spring.datasource.secondary.password=root
spring.datasource.secondary.driver-class-name=com.mysql.jdbc.Driver

springBoot与多数据源的配置_maven

这里配置的是两个不同的数据库,我们也可以把连库信息改为任意的。

第三:JdbcTemplate绑定数据库:

springBoot与多数据源的配置_maven

package org.shenlan;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
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;

/**
* Created by wangwei on 2016/9/6.
*/
@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);
}
}

springBoot与多数据源的配置_maven

第四,写一个测试的方法:

springBoot与多数据源的配置_maven

package org.shenlan;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
* Created by wangwei on 2016/9/6.
*/
@RestController
@RequestMapping("/home")
public class TestController {
@Autowired
@Qualifier("primaryJdbcTemplate")
JdbcTemplate jdbcTemplate1;

@Autowired
@Qualifier("secondaryJdbcTemplate")
JdbcTemplate jdbcTemplate2;

@RequestMapping("/test1")
public String test1(){
List<Map<String,Object>> list = jdbcTemplate1.queryForList("select * from user");
return Arrays.asList(list).toString();
}

@RequestMapping("/test2")
public String test2(){
List<Map<String,Object>> list = jdbcTemplate2.queryForList("SELECT * from USER");
return Arrays.asList(list).toString();
}
}

springBoot与多数据源的配置_maven

这样整个项目就完成了,打开浏览器测试一下吧。

在浏览器中输入:localhost:8080/home/test1   出现如下:

springBoot与多数据源的配置_mysql_11

在浏览器中输入:localhost:8080/home/test2  出现如下:

springBoot与多数据源的配置_maven_12

哈哈,完成啦!

本项目的完整代码链接:https://github.com/shenlanzhizunjustwangwei/springBoot/tree/master/springBoot-mutidata