springboot 2.1.4 多数据源配置

1. pox文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>qiye</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>qiye</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>mssql-jdbc</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.16</version>
        </dependency>
        <!--<dependency>
            <groupId>net.sourceforge.jtds</groupId>
            <artifactId>jtds</artifactId>
        </dependency>-->
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

 2.application.yml

spring:
#  profiles:
#    active: dev
#    active: prod
  datasource:
    zcoasnet:
      driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
      jdbc-url: jdbc:sqlserver://xxx.com:3433; DatabaseName=zCoasNet
      username: root
      password: root
    zform6:
      driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
      jdbc-url: jdbc:sqlserver://xxx.com:3433; DatabaseName=ZoomBackOffice
      username: root
      password: root
  jpa:
    hibernate:
      naming:
        physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
    database: default
    databasePlatform: org.hibernate.dialect.SQLServer2012Dialect
server:
  port: 8888
  tomcat:
    uri-encoding: UTF-8
  servlet:
    context-path: /

 3.config配置

package com.example.qiye.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 DataSourceConfig{
    @Primary
    @Bean(name = "zCoasNetDataSource")
    @Qualifier("zCoasNetDataSource")
    @ConfigurationProperties(prefix="spring.datasource.zcoasnet")
    public DataSource zCoasNetDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "zForm6DataSource")
    @Qualifier("zForm6DataSource")
    @ConfigurationProperties(prefix="spring.datasource.zform6")
    public DataSource zForm6DataSource() {
        return DataSourceBuilder.create().build();
    }
}

 zCoasNetConfig

package com.example.qiye.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateProperties;
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;

import javax.persistence.EntityManager;
import javax.sql.DataSource;
import java.util.Map;
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef="entityManagerFactoryZCoasNet",
        transactionManagerRef="transactionManagerZCoasNet",
        basePackages= { "com.example.qiye.zcoas" }) //设置Repository所在位置

public class zCoasNetConfig {

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

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

    @Primary
    @Bean(name = "entityManagerFactoryZCoasNet")
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryZCoasNet (EntityManagerFactoryBuilder builder) {
        return builder
                .dataSource(zCoasNetDataSource)
                .properties(getVendorProperties())
                .packages(  "com.example.qiye.zcoas") //设置实体类所在位置
                .persistenceUnit("zCoasNetPersistenceUnit")
                .build();
    }

    @Autowired
    private JpaProperties jpaProperties;
    @Autowired HibernateProperties hibernateProperties;

    /*private Map<String, String> getVendorProperties(DataSource dataSource) {
        return jpaProperties.getHibernateProperties(dataSource);
    }*/

    private Map<String, Object> getVendorProperties() {
        return hibernateProperties.determineHibernateProperties(jpaProperties.getProperties(), new HibernateSettings());
    }
    @Primary
    @Bean(name = "transactionManagerZCoasNet")
    public PlatformTransactionManager transactionManagerZCoasNet(EntityManagerFactoryBuilder builder) {
        return new JpaTransactionManager(entityManagerFactoryZCoasNet(builder).getObject());
    }

}

  zForm6Config

package com.example.qiye.config;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateProperties;
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="entityManagerFactoryZForm6",
        transactionManagerRef="transactionManagerZForm6",
        basePackages= {"com.example.qiye.zform6"}) //设置Repository所在位置

public class zForm6Config {

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

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

    @Bean(name = "entityManagerFactoryZForm6")
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryZForm6 (EntityManagerFactoryBuilder builder) {
        return builder
                .dataSource(zForm6DataSource)
                .properties(getVendorProperties())
                .packages("com.example.qiye.zform6") //设置实体类所在位置
                .persistenceUnit("zForm6PersistenceUnit")
                .build();
    }

    @Autowired
    private JpaProperties jpaProperties;
    @Autowired HibernateProperties hibernateProperties;

   /* private Map<String, String> getVendorProperties(DataSource dataSource) {
        return jpaProperties.getHibernateProperties(dataSource);
    }*/
    private Map<String, Object> getVendorProperties() {
        return hibernateProperties.determineHibernateProperties(jpaProperties.getProperties(), new HibernateSettings());
    }

    @Bean(name = "transactionManagerZForm6")
    public PlatformTransactionManager transactionManagerZForm6(EntityManagerFactoryBuilder builder) {
        return new JpaTransactionManager(entityManagerFactoryZForm6(builder).getObject());
    }

}

 和1.5.7 区别: 
  1. yml 要规范命名,不能有大写字符

  2.不通数据源下的实体类,必须单独分开来放, 不然是读取不到第二个数据源的表的, 我被坑了半天。

  3. jpa的获取方式 

猜你喜欢

转载自www.cnblogs.com/412013cl/p/10870083.html