zoukankan      html  css  js  c++  java
  • SpringBoot 1.5.7多数据源 sqlserver

    pom.xml 文件

    <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </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>
            <dependency>
                <groupId>net.sourceforge.jtds</groupId>
                <artifactId>jtds</artifactId>
                <version>1.3.1</version>
            </dependency>
            <!-- SpringBoot整合JPA -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-jpa</artifactId>
            </dependency>
    

    DataSourceConfig.java

    package ccpiteco.net.chaxun.configuration;
    
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    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.java

    package ccpiteco.net.chaxun.configuration;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    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= { "ccpiteco.net.chaxun.zcoasnet" }) //设置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(zCoasNetDataSource))
                    .packages(  "ccpiteco.net.chaxun.zcoasnet") //设置实体类所在位置
                    .persistenceUnit("zCoasNetPersistenceUnit")
                    .build();
        }
    
        @Autowired
        private JpaProperties jpaProperties;
    
        private Map<String, String> getVendorProperties(DataSource dataSource) {
            return jpaProperties.getHibernateProperties(dataSource);
        }
        @Primary
        @Bean(name = "transactionManagerZCoasNet")
        public PlatformTransactionManager transactionManagerZCoasNet(EntityManagerFactoryBuilder builder) {
            return new JpaTransactionManager(entityManagerFactoryZCoasNet(builder).getObject());
        }
    
    }
    

      zForm6Config.java

    package ccpiteco.net.chaxun.configuration;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    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= {"ccpiteco.net.chaxun.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(zForm6DataSource))
                    .packages("ccpiteco.net.chaxun.zform6") //设置实体类所在位置
                    .persistenceUnit("zForm6PersistenceUnit")
                    .build();
        }
    
        @Autowired
        private JpaProperties jpaProperties;
    
        private Map<String, String> getVendorProperties(DataSource dataSource) {
            return jpaProperties.getHibernateProperties(dataSource);
        }
    
        @Bean(name = "transactionManagerZForm6")
        public PlatformTransactionManager transactionManagerZForm6(EntityManagerFactoryBuilder builder) {
            return new JpaTransactionManager(entityManagerFactoryZForm6(builder).getObject());
        }
    
    }
    

      application.yml

    spring:
    #  profiles:
    #    active: dev
    #    active: prod
      datasource:
        zCoasNet:
          driver-class-name: net.sourceforge.jtds.jdbc.Driver
          url: jdbc:jtds:sqlserver://数据库连接url; DatabaseName=数据库
          username: root
          password: root
        zForm6:
          driver-class-name: net.sourceforge.jtds.jdbc.Driver
          url: jdbc:jtds:sqlserver://数据库连接url; DatabaseName=数据库
          username: root
          password: root
      jpa:
        hibernate:
          naming:
            physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
        database: default
        databasePlatform: org.hibernate.dialect.SQLServer2012Dialect
    #logging:
    #  level:
    #    ROOT: INFO
    #    ROOT: ERROR
    
    server:
      port: 8888
      tomcat:
        uri-encoding: UTF-8
      servlet:
        context-path: /
    

      

  • 相关阅读:
    行列式学习笔记
    二项式反演学习笔记
    【AtCoder】ARC096(C
    【LOJ】#2127. 「HAOI2015」按位或
    [ACM] POJ 1218 THE DRUNK JAILER (关灯问题)
    lua的弱弱引用表
    西班牙式软件团队
    【DRP】採用dom4j完毕XML文件导入数据库
    基于Linux平台病毒Wirenet.c解析
    【剑指offer】异或去重
  • 原文地址:https://www.cnblogs.com/412013cl/p/9978815.html
Copyright © 2011-2022 走看看