SpringBoot 1.5.7多数据源 sqlserver

pom.xml 文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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: /

  

posted @   qukaige  阅读(552)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
点击右上角即可分享
微信分享提示