springboot 2.1.4 多数据源配置
1. pox文件
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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | <? 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
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 | 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配置
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 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
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 62 63 64 65 66 67 68 | 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
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 62 63 64 65 66 | 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的获取方式
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .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 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义