zoukankan      html  css  js  c++  java
  • springBoot与多数据源的配置

    http://www.cnblogs.com/shenlanzhizun/p/5846475.html 

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

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

    复制代码
    <?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>
    复制代码

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

    复制代码
    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);
        }
    
    }
    复制代码

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

    复制代码
    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
    复制代码

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

    第三:JdbcTemplate绑定数据库:

    复制代码
    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);
        }
    }
    复制代码

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

    复制代码
    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();
        }
    }
    复制代码

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

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

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

    哈哈,完成啦!

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

  • 相关阅读:
    大数据下的质量体系建设
    快速打造属于你的接口自动化测试框架
    测试环境问题排查的那些事儿
    100个任务,用多机实现
    shell 在一个文件中查找数字
    shell中的awk使用
    shell怎么实现多进程
    删除字符串S1中的子串S2
    C++11的新特性
    C++里面普通指针怎么转换成智能指针
  • 原文地址:https://www.cnblogs.com/ceshi2016/p/6735040.html
Copyright © 2011-2022 走看看