赞
踩
Java使用多数据源 方法配置
首先需要引入druid连接池依赖 ,此处选择alibaba连接池。
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.8</version>
</dependency>
配置数据源信息 可创建多个。
spring: datasource: druid: #第一个数据库连接信息 local local: type: com.alibaba.druid.pool.DruidDataSource username: root password: mima driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/local?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true #第二个数据库连接信息 cloud cloud: type: com.alibaba.druid.pool.DruidDataSource username: root password: mima driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/cloud?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
创建配类DataSourceConfig 注入配置信息
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
@Slf4j
@Configuration
public class DataSourceConf赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。