SqlServer MySQL 双配置 SpringBoot

这篇博客介绍了如何在Spring Boot应用中配置多个数据源,包括一个SQL Server数据源和一个MySQL数据源。配置文件展示了两个数据源的详细连接信息,如用户名、密码、驱动类和URL。在配置类中,定义了两个数据源Bean,其中一个标记为`@Primary`,表示主要数据源。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

server:
  port: 39010
spring:
  datasource:
    first:
      username: sa
      password: root
      driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
      jdbc-url: jdbc:sqlserver://HUANGQUANHE:1433;DatabaseName=wm
    second:
      username: root
      password: root
      driver-class-name: com.mysql.cj.jdbc.Driver
      jdbc-url: jdbc:mysql://localhost:3306/wm?characterEncoding=UTF-8&useUnicode=true&useSSL=false&tinyInt1isBit=false&allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai

package com.mlfo.wmb.config;

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;

@Configuration
public class SqlServerDataSourceConfig {

    /**
     * sqlServer
     * @return
     */
//    @Bean(name = "sqlServerDataSource")
//    @Qualifier("sqlServerDataSource")
//    @ConfigurationProperties(prefix="spring.datasource.first")
//    public DataSource getMyDataSource(){
//        return DataSourceBuilder.create().build();
//    }

    /**
     * mysql
     */
    @Bean(name = "dataSource")
    @Primary
    @Qualifier("dataSource")
    @ConfigurationProperties(prefix="spring.datasource.second")
    public DataSource getMyDataSource(){
        return DataSourceBuilder.create().build();
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值