Spring Boot 多数据库从辅助数据库中提供空值
Posted
技术标签:
【中文标题】Spring Boot 多数据库从辅助数据库中提供空值【英文标题】:Spring Boot Multiple Database giving null values from secondary database 【发布时间】:2020-12-10 22:54:58 【问题描述】:我配置了 2 个数据库,使 1 个数据库成为主数据库。我能够从主数据库中获取数据库,但辅助数据库给了我空结果。辅助数据库表的相应配置正在主模式中创建,结果为空。下面是我写的代码。
数据库配置。
@Bean
@ConfigurationProperties(prefix="com.example.studentmanagement.datasource")
public DataSource studentDataSource()
DataSource dataSource=DataSourceBuilder.create().build();
return dataSource;
@Bean
@Primary
@ConfigurationProperties(prefix="com.example.ordermanagement.datasource")
public DataSource orderDataSource()
DataSource dataSource=DataSourceBuilder.create().build();
return dataSource;
我正在使用带有 spring 数据存储库的 entitymanager 工厂来获取数据。下面是主要和次要配置。
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages ="com.example.SpringBootMultipleDbs.Customer.Repos",entityManagerFactoryRef = "orderEntityManagerFactoryBean",
transactionManagerRef = "orderTransactionManager")
public class PrimaryConfig
@Bean
@Primary
public LocalContainerEntityManagerFactoryBean orderEntityManagerFactoryBean(DataSource orderDataSource)
LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean=new LocalContainerEntityManagerFactoryBean();
localContainerEntityManagerFactoryBean.setDataSource(orderDataSource);
localContainerEntityManagerFactoryBean.setPackagesToScan("com.example.SpringBootMultipleDbs.Customer.Entities");
localContainerEntityManagerFactoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
Map<String,Object> jpaPropertiesMap=new HashMap<>();
jpaPropertiesMap.put("hibernate.dialect", org.hibernate.dialect.mysql5Dialect.class);
jpaPropertiesMap.put("hibernate.hbm2ddl.auto","update");
jpaPropertiesMap.put("hibernate.format_sql", true);
jpaPropertiesMap.put("hibernate.show_sql", true);
jpaPropertiesMap.put("hibernate.implicit_naming_strategy", org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl.class);
jpaPropertiesMap.put("hibernate.physical_naming_strategy",org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl.class);
localContainerEntityManagerFactoryBean.setJpaPropertyMap(jpaPropertiesMap);
return localContainerEntityManagerFactoryBean;
@Bean
@Primary
public JpaTransactionManager orderTransactionManager(LocalContainerEntityManagerFactoryBean orderEntityManagerFactoryBean)
JpaTransactionManager jpaTransactionManager=new JpaTransactionManager();
jpaTransactionManager.setEntityManagerFactory(orderEntityManagerFactoryBean.getObject());
return jpaTransactionManager;
辅助配置
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages ="com.example.SpringBootMultipleDbs.Department.Repos",entityManagerFactoryRef = "studentEntityManagerFactoryBean",
transactionManagerRef = "studentTransactionManager")
public class SecondaryConfig
@Bean
public LocalContainerEntityManagerFactoryBean studentEntityManagerFactoryBean(DataSource studentDataSource)
LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean=new LocalContainerEntityManagerFactoryBean();
localContainerEntityManagerFactoryBean.setDataSource(studentDataSource);
localContainerEntityManagerFactoryBean.setPackagesToScan("com.example.SpringBootMultipleDbs.Department.Entities");
localContainerEntityManagerFactoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
Map<String,Object> jpaPropertiesMap=new HashMap<>();
jpaPropertiesMap.put("hibernate.dialect", org.hibernate.dialect.MySQL5Dialect.class);
jpaPropertiesMap.put("hibernate.format_sql", true);
jpaPropertiesMap.put("hibernate.show_sql", true);
jpaPropertiesMap.put("hibernate.implicit_naming_strategy", org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl.class);
jpaPropertiesMap.put("hibernate.physical_naming_strategy",org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl.class);
localContainerEntityManagerFactoryBean.setJpaPropertyMap(jpaPropertiesMap);
return localContainerEntityManagerFactoryBean;
@Bean
public JpaTransactionManager studentTransactionManager(LocalContainerEntityManagerFactoryBean studentEntityManagerFactoryBean)
JpaTransactionManager jpaTransactionManager=new JpaTransactionManager();
jpaTransactionManager.setEntityManagerFactory(studentEntityManagerFactoryBean.getObject());
return jpaTransactionManager;
下面是 spring data jpa 存储库类,它为我获取 null 数据映射到辅助数据库配置。
public interface DepartmentRepo extends JpaRepository<Department,Integer>
public Department findByDeptId(Integer id);
application.yml 文件
com:
example:
studentmanagement:
datasource:
driverClassName: com.mysql.jdbc.Driver
jdbcUrl: jdbc:mysql://localhost:3306/student
username: root
password: root123
ordermanagement:
datasource:
driverClassName: com.mysql.jdbc.Driver
jdbcUrl: jdbc:mysql://localhost:3306/order
username: root
password: root123
server:
port: 8085
servlet:
context-path: /SpringBootDbs
【问题讨论】:
【参考方案1】:您必须将配置和配置使用者代码保存在不同的包中。 在您的情况下,因为您有 2 个 DB 用于学生管理,另一个用于订单管理,因此您还需要在代码中打包层次结构。您的包结构应如下所示。
com.example.orderManagement //OrderManagement Config和订单的所有消费者代码都应该在这里
com.example.Studentmanagement //学生管理配置和学生的所有消费者代码都在这里
【讨论】:
以上是关于Spring Boot 多数据库从辅助数据库中提供空值的主要内容,如果未能解决你的问题,请参考以下文章
Spring Boot 整合 Mybatis 实现 Druid 多数据源详解
Spring Boot中调用@Async注解的异步方法并获取返回值