<repo_name>guojianping1234/JavaDemo<file_sep>/src/main/java/com/myspringbt/demo/dataSource/DataSourcesConfig.java
package com.myspringbt.demo.dataSource;

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 javax.sql.DataSource;

/**
 * 对数据源  和mybatis
 */
@Configuration
public class DataSourcesConfig {


    @Bean(name = "db1")
    @Qualifier("db1")
    @ConfigurationProperties(prefix = "spring.datasource.primary") // application.properteis中对应属性的前缀
    public DataSource dataSource1() {
        return DataSourceBuilder.create().build();
    }


    @Bean(name = "db2")
    @Qualifier("db2")
    @Primary
    @ConfigurationProperties(prefix = "spring.datasource.secondary") // application.properteis中对应属性的前缀
    public DataSource dataSource2() {
        return DataSourceBuilder.create().build();
    }

}<file_sep>/src/main/java/com/myspringbt/demo/controller/HelloWorld.java
package com.myspringbt.demo.controller;

import io.swagger.annotations.Api;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Controller
@RestController
@RequestMapping("v1/")
@Api("HelloWorldController")
public class HelloWorld {
    @GetMapping("helloworld")
    public String helloword() {
        return "hello world";
    }

}
