Java SpringBoot连接多个数据库

1、idea新建springboot项目,在根目录下的pom.xml文件种添加相关依赖

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

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

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

      <!-- mysql-driver-->
      <dependency>
          <groupId>mysql</groupId>
          <artifactId>mysql-connector-java</artifactId>
          <scope>runtime</scope>
      </dependency>

      <!-- Druid:https://mvnrepository.com/artifact/com.alibaba/druid -->
      <dependency>
          <groupId>com.alibaba</groupId>
          <artifactId>druid</artifactId>
          <version>1.2.5</version>
      </dependency>

      <!-- https://mvnrepository.com/artifact/com.alibaba/druid-spring-boot-starter -->
      <dependency>
          <groupId>com.alibaba</groupId>
          <artifactId>druid-spring-boot-starter</artifactId>
          <version>1.2.5</version>
      </dependency>

	 //mybatis
      <dependency>
          <groupId>org.mybatis.spring.boot</groupId>
          <artifactId>mybatis-spring-boot-starter</artifactId>
          <version>2.1.1</version>
      </dependency>

2、在/src目录下新建application.yml文件,或者使用原来的application.properties,两种文件的语法稍有差别,我使用的是yml文件

文件位置:
在这里插入图片描述

代码:

spring:
  datasource:
    one:
      type: com.alibaba.druid.pool.DruidDataSource
      username: root
      password: 123456
      #?serverTimezone=UTC解决时区的报错,el_company是需要访问的数据库的名称
      #192.152.1.171练的远程数据库,因此写的远程服务器ip,如果连本机数据库的话直接改成localhost:3306
      url: jdbc:mysql://192.152.1.171:3306/el_company?autoReconnect=true&serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
      driver-class-name: com.mysql.jdbc.Driver

    two:
      type: com.alibaba.druid.pool.DruidDataSource
      username: root
      password: 123456
      #?serverTimezone=UTC解决时区的报错
      url: jdbc:mysql://192.152.1.171:3306/el_dataplus?autoReconnect=true&serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
      driver-class-name: com.mysql.jdbc.Driver

3、在此目录下src/main/java的这个包com.demo.my新建config配置包,然后在config中新建配置文件DataSourceConfig、JdbcTemplateConfig

文件位置:在这里插入图片描述
DataSourceConfig配置多个数据源:

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

//注:@Qualifier :告诉是使用哪个数据源
//@ConfigurationProperties :加载application.properties中的哪个数据源
//@Primary:自动装配时若出现多个Bean候选者时,被该注解修饰的bean将被作为首选者,否则会被抛出异常

@Configuration
public class DataSourceConfig {
    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.one")
    public DataSource dsOne(){
        return DruidDataSourceBuilder.create().build();
    }

    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.two")
    public DataSource dsTwo(){
        return DruidDataSourceBuilder.create().build();
    }
}

JdbcTemplateConfig配置多个JdbcTemplate:

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;

@Configuration
public class JDBCTemplateConfig {

    @Bean
    public JdbcTemplate jdbcTemplateOne(@Qualifier("dsOne") DataSource dataSource){
        return new JdbcTemplate(dataSource);
    }

    @Bean
    public JdbcTemplate jdbcTemplateTwo(@Qualifier("dsTwo") DataSource dataSource){
        return new JdbcTemplate(dataSource);
    }

}

4、在此目录下src/main/java的这个包com.demo.my新建controller层,开始测试

文件位置:
在这里插入图片描述

编写SimRecommendController层测试代码:

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


@RestController
public class SimRecommendController {

    @Resource(name = "jdbcTemplateOne")
    private JdbcTemplate jdbcTemplateOne;
    @Resource(name = "jdbcTemplateTwo")
    private JdbcTemplate jdbcTemplateTwo;

    @RequestMapping(value = "/test",method = RequestMethod.GET)
    public String result1() {

        String sql1 = "select entid from company  where ENTNAME ='*******'";
        //执行sql查询企业ID
        List<Map<String, Object>> list_maps1 = jdbcTemplateOne.queryForList(sql1);
        String sql2 = "select * from code_nic_ys where code='7931'";
        List<Map<String, Object>> list_maps2 = jdbcTemplateTwo.queryForList(sql2);
        System.out.println(list_maps1);
        System.out.println(list_maps2);
        return "查询成功";
    }
}

OK,完成!

如需转载请标明出处!

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值