【发布时间】: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
【问题讨论】:
标签: mysql database spring-boot spring-data-jpa