码迷,mamicode.com
首页 > 编程语言 > 详细

SpringBoot2.0 配置多数据源

时间:2020-07-22 11:30:31      阅读:56      评论:0      收藏:0      [点我收藏+]

标签:ssl   frame   tor   data   str   需要   action   for   意思   

一、简述

配置多数据源意思就是在一个项目中使用多个数据库,在项目使用中可以不用手动切换数据库来实现不同数据库的数据获取和更新。

源码地址:?https://github.com/hanguilin/boot-security

项目环境: SpringBoot?2.1.9、Spring Data Jpa

项目文件夹:

技术图片

二、配置

application.properties

  • spring.datasource.primary.driver-class-name? 数据库驱动包

  • spring.datasource.primary.jdbc-url 数据库连接

  • spring.datasource.primary.username 用户名

  • spring.datasource.primary.password 密码

数据库配置别名可自定义,此处一级库为primmary。二级数据库配置同上,别名为secondary。

hikara为数据库连接池的配置,同类产品还有druid。

server.port=8080

##########一级数据库配置##########
spring.datasource.primary.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.primary.jdbc-url=jdbc:mysql://localhost:3306/primary_db?autoReconnect=true&useUnicode=true&useSSL=false
spring.datasource.primary.username=root
spring.datasource.primary.password=****
#最大连接数
spring.datasource.primary.hikari.maximum-pool-size=20
#连接超时毫秒
spring.datasource.primary.hikari.connection-timeout=60000
#空闲的连接的生命时长毫秒
spring.datasource.primary.hikari.idle-timeout=60000
#连接的生命时长毫秒
spring.datasource.primary.hikari.max-lifetime=60000
#验证连接有效性毫秒
spring.datasource.primary.hikari.validation-timeout=3000
#登录超时毫秒
spring.datasource.primary.hikari.login-timeout=5
##########一级数据库配置##########

##########二级数据库配置##########
spring.datasource.secondary.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.secondary.jdbc-url=jdbc:mysql://localhost:3306/secondary_db?autoReconnect=true&useUnicode=true
spring.datasource.secondary.username=root
spring.datasource.secondary.password=****
#最大连接数
spring.datasource.secondary.hikari.maximum-pool-size=20
#连接超时毫秒
spring.datasource.secondary.hikari.connection-timeout=60000
#空闲的连接的生命时长毫秒
spring.datasource.secondary.hikari.idle-timeout=60000
#连接的生命时长毫秒
spring.datasource.secondary.hikari.max-lifetime=60000
#验证连接有效性毫秒
spring.datasource.secondary.hikari.validation-timeout=3000
#登录超时毫秒
spring.datasource.secondary.hikari.login-timeout=5
##########二级数据库配置##########

##########hibernate配置##########
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.hbm2ddl.auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
##########hibernate配置##########

DataSourcesConfig.java

  • @Bean(name = "primaryDataSource")

将该返回对象交由Spring管理,bean名称为primaryDataSource。

  • @Qualifier("primaryDataSource")

根据名称将bean注入到对象。

  • @Primary

自动装配时当出现多个Bean候选者时,被注解为@Primary的Bean将作为首选者,否则将抛出异常。

  • @ConfigurationProperties(prefix="spring.datasource.primary")

将前缀为spring.datasource.primary的配置注入到对应名称的属性字段中。

此类作用为生成两级DataSource,交由Spring管理,方便后续从Spring中调用。

package com.spring.security.config.datasource;

import javax.sql.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 com.zaxxer.hikari.HikariDataSource;

@Configuration
public class DataSourcesConfig {

    @Bean(name = "primaryDataSource")
    @Qualifier("primaryDataSource")
    @Primary
    @ConfigurationProperties(prefix="spring.datasource.primary")
    public DataSource primaryDataSource() {
        return DataSourceBuilder.create()
        		.type(HikariDataSource.class)
                .build();
    }

    @Bean(name = "secondaryDataSource")
    @Qualifier("secondaryDataSource")
    @ConfigurationProperties(prefix="spring.datasource.secondary")
    public DataSource secondaryDataSource() {
    	return DataSourceBuilder.create()
        		.type(HikariDataSource.class)
                .build();
    }
}

PrimaryConfig.java

  • @Configuration

在Spring中注册成bean

  • @EnableTransactionManagement

开启事务管理

@EnableJpaRepositories(
? ? ? ? entityManagerFactoryRef="entityManagerFactoryPrimary",
? ? ? ? transactionManagerRef="transactionManagerPrimary",
? ? ? ? basePackages= { "com.spring.security.dao.primary" })

  • @EnableJpaRepositories

注解用于Srping JPA的代码配置,用于取代xml形式的配置文件

  • entityManagerFactoryRef

实体管理工厂,下文代码中配置基础包为com.spring.security.entity.primary

  • transactionManagerRef

事务管理

  • basePackages

持久层基础包扫描,此处配置为com.spring.security.dao.primary

上述两个包的配置,意为一级数据库的实体和dao蹭接口分别所处的位置。当jpa进行实体映射时,对应包下面的实体会映射到不通的数据库。dao层接口也会随之切换。

package com.spring.security.config.datasource;

import java.util.Map;

import javax.persistence.EntityManager;
import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateSettings;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef="entityManagerFactoryPrimary",
        transactionManagerRef="transactionManagerPrimary",
        basePackages= { "com.spring.security.dao.primary" }) //设置Repository所在位置
public class PrimaryConfig {

    @Autowired 
    @Qualifier("primaryDataSource")
    private DataSource primaryDataSource;

    @Autowired(required=false)
    private JpaProperties jpaProperties;

    @Primary
    @Bean(name = "entityManagerPrimary")
    public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
        return entityManagerFactoryPrimary(builder).getObject().createEntityManager();
    }

    @Primary
    @Bean(name = "entityManagerFactoryPrimary")
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary (EntityManagerFactoryBuilder builder) {
        return builder
                .dataSource(primaryDataSource)
                .properties(getVendorProperties())
                .packages("com.spring.security.entity.primary") //设置实体类所在位置
                .persistenceUnit("primaryPersistenceUnit")
                .build();
    }

    private Map<String, Object> getVendorProperties() {
        return jpaProperties.getHibernateProperties(new HibernateSettings());
    }

    @Primary
    @Bean(name = "transactionManagerPrimary")
    public PlatformTransactionManager transactionManagerPrimary(EntityManagerFactoryBuilder builder) {
        return new JpaTransactionManager(entityManagerFactoryPrimary(builder).getObject());
    }

}

SecondaryConfig.java

package com.spring.security.config.datasource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateSettings;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.persistence.EntityManager;
import javax.sql.DataSource;
import java.util.Map;

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef="entityManagerFactorySecondary",
        transactionManagerRef="transactionManagerSecondary",
        basePackages= { "com.spring.security.dao.secondary" }) //设置Repository所在位置
public class SecondaryConfig {

    @Autowired
    private JpaProperties jpaProperties;

    @Autowired 
    @Qualifier("secondaryDataSource")
    private DataSource secondaryDataSource;

    @Bean(name = "entityManagerSecondary")
    public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
        return entityManagerFactorySecondary(builder).getObject().createEntityManager();
    }

    @Bean(name = "entityManagerFactorySecondary")
    public LocalContainerEntityManagerFactoryBean entityManagerFactorySecondary (EntityManagerFactoryBuilder builder) {
        return builder
                .dataSource(secondaryDataSource)
                .properties(getVendorProperties())
                .packages("com.spring.security.entity.secondary") //设置实体类所在位置
                .persistenceUnit("secondaryPersistenceUnit")
                .build();
    }

    private Map<String, Object> getVendorProperties() {
        return jpaProperties.getHibernateProperties(new HibernateSettings());
    }

    @Bean(name = "transactionManagerSecondary")
    PlatformTransactionManager transactionManagerSecondary(EntityManagerFactoryBuilder builder) {
        return new JpaTransactionManager(entityManagerFactorySecondary(builder).getObject());
    }

}

如果需要扩展更多数据库,可以根据上述配置自行增加。

SpringBoot2.0 配置多数据源

标签:ssl   frame   tor   data   str   需要   action   for   意思   

原文地址:https://www.cnblogs.com/yl-space/p/13358975.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!