我正在尝试在我的spring应用程序中使用2 DB。从第一个读取数据,然后从第二个保存数据。问题是我的第二个数据库没有构建。
application.properties
fromDB.datasource.url=jdbc:h2:file:D:/test1/db1
fromDB.datasource.username=sa
fromDB.datasource.password=
fromDB.datasource.platform=h2
fromDB.datasource.driverClassName=org.h2.Driver
toDB.datasource.url=jdbc:h2:file:D:/test2/db2
toDB.datasource.username=sa
toDB.datasource.password=
toDB.datasource.platform=h2
toDB.datasource.driverClassName=org.h2.Driver
###
# H2 Settings
###
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
spring.h2.console.settings.trace=true
spring.h2.console.settings.web-allow-others=true
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
###
# Hibernate Settings
###
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.properties.hibernate.show_sql=true
spring.jpa.properties.hibernate.use_sql_comments=false
spring.jpa.properties.hibernate.format_sql=true
spring.datasource.initialization-mode=always
spring.datasource.initialize=true
spring.jpa.generate-ddl=true
配置类
@Configuration
@EnableJpaRepositories
(
basePackages = "leadTime.service",
entityManagerFactoryRef = "toEntityManager",
transactionManagerRef = "toTransactionManager"
)
@EnableTransactionManagement
public class OracleToDbConfig {
@Autowired
private Environment env;
@Bean(name = "datasourceTo")
@ConfigurationProperties(prefix = "toDB.datasource")
public DataSource dataSourceTo() {
return DataSourceBuilder.create().build();
}
@Qualifier("datasourceTo")
@PersistenceContext(unitName = "toEm")
@Bean(name = "toEntityManager")
public LocalContainerEntityManagerFactoryBean toEntityManager() {
LocalContainerEntityManagerFactoryBean em
= new LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSourceTo());
em.setPackagesToScan(
new String[]{"leadTime.to"});
HibernateJpaVendorAdapter vendorAdapter
= new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
HashMap<String, Object> properties = new HashMap<>();
properties.put("hibernate.hbm2ddl.auto",
env.getProperty("hibernate.hbm2ddl.auto"));
properties.put("hibernate.dialect",
env.getProperty("hibernate.dialect"));
em.setJpaPropertyMap(properties);
return em;
}
@Bean(name = "toTransactionManager")
public PlatformTransactionManager toTransactionManager() {
JpaTransactionManager transactionManager
= new JpaTransactionManager();
transactionManager.setEntityManagerFactory(
toEntityManager().getObject());
return transactionManager;
}
}
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories
(
basePackages = "leadTime.service",
entityManagerFactoryRef = "fromEntityManager",
transactionManagerRef = "fromTransactionManager"
)
public class OracleFromDbConfig {
@Autowired
private Environment env;
@Primary
@Bean(name = "datasourceFrom")
@ConfigurationProperties(prefix = "fromDB.datasource")
public DataSource dataSourceFrom() {
return DataSourceBuilder.create().build();
}
@PersistenceContext(unitName = "fromEm")
@Bean(name = "fromEntityManager")
@Qualifier("datasourceFrom")
@Primary
public LocalContainerEntityManagerFactoryBean fromEntityManager() {
LocalContainerEntityManagerFactoryBean em
= new LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSourceFrom());
em.setPackagesToScan(
new String[]{"leadTime.from"});
HibernateJpaVendorAdapter vendorAdapter
= new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
HashMap<String, Object> properties = new HashMap<>();
properties.put("hibernate.hbm2ddl.auto",
env.getProperty("hibernate.hbm2ddl.auto"));
properties.put("hibernate.dialect",
env.getProperty("hibernate.dialect"));
em.setJpaPropertyMap(properties);
return em;
}
@Primary
@Bean(name = "fromTransactionManager")
public PlatformTransactionManager fromTransactionManager() {
JpaTransactionManager transactionManager
= new JpaTransactionManager();
transactionManager.setEntityManagerFactory(
fromEntityManager().getObject());
return transactionManager;
}
}
MainApp
@EnableJpaRepositories("leadTime.service")
@EntityScan(basePackageClasses = {
ToRepository.class,
FromRepository.class
})
@SpringBootApplication
public class LeadTimeApplication implements CommandLineRunner {
private final ToRepository toRepository;
private final FromRepository fromRepository;
@Qualifier("datasourceTo")
@Autowired
DataSource dataSourceTo;
@Qualifier("datasourceFrom")
@Autowired
DataSource dataSourceFrom;
private Logger LOG = LoggerFactory.getLogger("LeadTimeApplication");
@Autowired
public LeadTimeApplication(ToRepository toRepository, FromRepository
fromRepository) {
this.toRepository = toRepository;
this.fromRepository = fromRepository;
}
public static void main(String[] args) {
SpringApplication.run(LeadTimeApplication.class, args);
}
@Transactional("fromTransactionManager")
public void insertDataIntoFromDB() throws SQLException {
TestDataFrom testDataFrom = new TestDataFrom();
testDataFrom.setId(1);
testDataFrom.setName("Test");
fromRepository.save(testDataFrom);
dataSourceFrom.getConnection().commit();
}
@Transactional("toTransactionManager")
public void insertDataIntoToDB() throws SQLException {
TestDataTo testDataTo = new TestDataTo();
testDataTo.setId(1);
testDataTo.setName("Tst");
toRepository.save(testDataTo);
dataSourceTo.getConnection().commit();
}
@Override
public void run(String... args) throws Exception {
insertDataIntoFromDB();
insertDataIntoToDB();
TestDataFrom dataFrom = fromRepository.findOne(1);
Iterable<TestDataFrom> allT = fromRepository.findAll();
LOG.info("TestDataFrom with ID 1: {}", dataFrom.getName());
TestDataTo dataTo = toRepository.findOne(1);
Iterable<TestDataTo> all = toRepository.findAll();
LOG.info("TestDataTo with ID 1: {}", dataTo.getName());
}
}
我在这里搜索(示例摘自该链接之一) multiple datasources in Spring Boot application http://xantorohara.blogspot.com/2013/11/spring-boot-jdbc-with-multiple.html Spring Boot Multiple Datasource Spring Boot, Spring Data JPA with multiple DataSources等等..有很多链接和示例,但我找不到解决方案。 也许我理解错了@Primary和@Qualifier的想法 我试图排除DataSourceAutoConfiguration.class,但仍然没有。
更新:两个数据库都创建了,但是我在保存两个数据库时都遇到了问题,我设法将两个实体仅保存在一个数据库中。 出现以下错误:
Parameter 0 of constructor in leadTime.LeadTimeApplication
required a bean named 'entityManagerFactory' that could not be found.
我尝试遵循:http://roufid.com/spring-boot-multiple-databases-configuration/和其他许多艺术,但是我从一个错误变成了另一个错误。