栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot多数据源配置

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot多数据源配置

配置文件
spring:
  datasource:
    aps:
      type: com.alibaba.druid.pool.DruidDataSource
      driver-class-name: com.mysql.jdbc.Driver
      username: root
      password: root
      url: jdbc:mysql://pig-mysql:3306/myaps?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=Asia/Shanghai
    mes:
      driver-class-name: oracle.jdbc.driver.OracleDriver
      username: MES
      password: MESTEST
      url: jdbc:oracle:thin:@*.*.*.*:1523/orayofc
     
配置类(一)
package com.yofc.aps.config;

import com.alibaba.druid.pool.DruidDataSource;
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 org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;

@Configuration
public class DataSourceConfig {

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

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

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

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

    @Bean(name = "apsJdbcTemplate")
    public JdbcTemplate apsJdbcTemplate(@Qualifier(value = "apsDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

    @Bean(name = "iotJdbcTemplate")
    public JdbcTemplate iotJdbcTemplate(@Qualifier(value = "iotDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

    @Bean(name = "mesJdbcTemplate")
    public JdbcTemplate mesJdbcTemplate(@Qualifier(value = "mesDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

    @Bean(name = "fisJdbcTemplate")
    public JdbcTemplate fisJdbcTemplate(@Qualifier(value = "fisDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }
}
配置类(二)
package com.yofc.aps.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;
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.HashMap;
import java.util.Map;

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef = "entityManagerFactoryAps",
        transactionManagerRef = "transactionManagerAps",
        basePackages = {"com.yofc.aps.dao.aps"})
public class ApsConfig {

    @Autowired
    private Environment env;

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

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

    @Primary
    @Bean(name = "entityManagerFactoryAps")
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryAps(EntityManagerFactoryBuilder builder) {
        return builder
                .dataSource(apsDataSource)
                .properties(getVendorProperties())
                .packages("com.yofc.aps.domain.aps")
                .persistenceUnit("aps")
                .build();
    }

    private Map getVendorProperties() {
        Map jpaProperties = new HashMap<>(16);
        jpaProperties.put("hibernate.hbm2ddl.auto", "update");
        jpaProperties.put("hibernate.show_sql", env.getProperty("spring.jpa.aps.show-sql"));
        jpaProperties.put("hibernate.physical_naming_strategy", SpringPhysicalNamingStrategy.class.getName());
        jpaProperties.put("hibernate.format_sql", env.getProperty("spring.jpa.aps.properties.hibernate.format_sql"));
        jpaProperties.put("hibernate.dialect", env.getProperty("spring.jpa.aps.properties.hibernate.naming.aps-dialect"));
        jpaProperties.put("hibernate.current_session_context_class", "org.springframework.orm.hibernate5.SpringSessionContext");
        return jpaProperties;
    }

    @Primary
    @Bean(name = "transactionManagerAps")
    public PlatformTransactionManager transactionManagerAps(EntityManagerFactoryBuilder builder) {
        return new JpaTransactionManager(entityManagerFactoryAps(builder).getObject());
    }
}
package com.yofc.aps.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;
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.HashMap;
import java.util.Map;



@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef = "entityManagerFactoryIot",
        transactionManagerRef = "transactionManagerIot",
        basePackages = {"com.yofc.aps.dao.iot"})
public class IotConfig {


    @Autowired
    private Environment env;

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

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

    @Bean(name = "entityManagerFactoryIot")
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryAps(EntityManagerFactoryBuilder builder) {
        return builder
                .dataSource(iotDataSource)
                .properties(getVendorProperties())
                .packages("com.yofc.aps.domain.iot")
                .persistenceUnit("iot")
                .build();
    }

    private Map getVendorProperties() {
        Map iotProperties = new HashMap<>(16);
        iotProperties.put("hibernate.hbm2ddl.auto", "update");
        iotProperties.put("hibernate.show_sql", env.getProperty("spring.jpa.aps.show-sql"));
        iotProperties.put("hibernate.physical_naming_strategy", SpringPhysicalNamingStrategy.class.getName());
        iotProperties.put("hibernate.format_sql", env.getProperty("spring.jpa.aps.properties.hibernate.format_sql"));
        iotProperties.put("hibernate.dialect", env.getProperty("spring.jpa.aps.properties.hibernate.naming.aps-dialect"));
        iotProperties.put("hibernate.current_session_context_class", "org.springframework.orm.hibernate5.SpringSessionContext");
        return iotProperties;
    }


}

代码中nativeQuery用法

 @Autowired
    @Qualifier("entityManagerFactoryIot")
    private EntityManagerFactory entityManagerFactory;

启动类

@EnablePigSwagger2
@EnablePigFeignClients
@EnableFeignClients
@EnablePigResourceServer
@SpringCloudApplication
@EnableScheduling
@EntityScan
@SpringBootApplication
public class ApsApp {
    public static void main(String[] args) {
        SpringApplication.run(ApsApp.class, args);
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/340789.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号