如何为Mongo数据库配置一个数据源,另一个用于spring-boot的H2数据库?

时间:2016-10-29 17:17:37

标签: mongodb spring-boot h2

如何为Mongo数据库配置一个数据源,为H2数据库配置另一个数据源? 这是我的配置mongo存储库的代码,但DataSource将它作为javax.sql.DataSource对象。

@Configuration
@EnableJpaRepositories(entityManagerFactoryRef = "domainEntityManagerFactory",
    transactionManagerRef = "domainTransactionManager", basePackageClasses = {CustomerRepositoryMongo.class})
public class CustomerConfigMongo {

@Bean
@Primary
PlatformTransactionManager domainTransactionManager() {
    return new JpaTransactionManager(domainEntityManagerFactory().getObject());
}

@Bean
@Primary
LocalContainerEntityManagerFactoryBean domainEntityManagerFactory() {

    HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
    jpaVendorAdapter.setGenerateDdl(true);

    LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();

    factoryBean.setDataSource(domainDataSource());
    factoryBean.setJpaVendorAdapter(jpaVendorAdapter);
    factoryBean.setPackagesToScan(Customer.class.getPackage().getName());
    return factoryBean;
}

@Bean
@Primary
@ConfigurationProperties(prefix = "spring.data.mongodb")
DataSource domainDataSource() {
    DataSource dataSource = DataSourceBuilder.create().build();;
    return dataSource;
}

}

0 个答案:

没有答案