如果是两个jpa数据源,怎么配置不上呢?

来源:6-6 实例2-JPA-DB.链式事务管理器

他门说这就是人生

2019-12-18

首先,我的目录结构是这样的:

http://img1.sycdn.imooc.com/szimg/5dfa3362096981df04420446.jpg

然后,我的配置是这样的:

@Configuration
public class DbConfig {

    @Primary
    @Bean("db1EntityManager")
    public LocalContainerEntityManagerFactoryBean db1EntityManager() {
        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        vendorAdapter.setGenerateDdl(true);

        LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();

        Map<String, String> map = new HashMap<>();
        map.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");

        em.setJpaPropertyMap(map);

        em.setDataSource(db1DataSource());
        em.setPackagesToScan("com.gaojingsi.transaction.muiltidatasourcetransactiondemo.entity.db1");
        em.setJpaVendorAdapter(vendorAdapter);

        return em;
    }

    @Primary
    @Bean(name = "db1DataSource")
    @ConfigurationProperties(prefix = "spring.datasource.db1")
    public DataSource db1DataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean("db2EntityManager")
    public LocalContainerEntityManagerFactoryBean db2EntityManager() {
        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        vendorAdapter.setGenerateDdl(true);

        LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();

        Map<String, String> map = new HashMap<>();
        map.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");

        em.setJpaPropertyMap(map);

        em.setDataSource(db2DataSource());
        em.setPackagesToScan("com.gaojingsi.transaction.muiltidatasourcetransactiondemo.entity.db2");
        em.setJpaVendorAdapter(vendorAdapter);

        return em;
    }

    @Bean(name = "db2DataSource")
    @ConfigurationProperties(prefix = "spring.datasource.db2")
    public DataSource db2DataSource() {
        return DataSourceBuilder.create().build();
    }

    @Primary
    @Bean
    public PlatformTransactionManager transactionManager() {
        JpaTransactionManager jpaTransactionManager1 = new JpaTransactionManager();
        jpaTransactionManager1.setEntityManagerFactory(db1EntityManager().getObject());
        JpaTransactionManager jpaTransactionManager2 = new JpaTransactionManager();
        jpaTransactionManager2.setEntityManagerFactory(db2EntityManager().getObject());
        ChainedTransactionManager chainedTransactionManager = new ChainedTransactionManager(jpaTransactionManager1, jpaTransactionManager2);
        return chainedTransactionManager;
    }

}

然后启动就报错:

***************************
APPLICATION FAILED TO START
***************************

Description:

Field db1UserDAO in com.gaojingsi.transaction.muiltidatasourcetransactiondemo.service.impl.UserServiceImpl required a bean named 'entityManagerFactory' that could not be found.

The injection point has the following annotations:
	- @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean named 'entityManagerFactory' in your configuration.

请问老师,还要做什么配置吗?

写回答

1回答

大漠风

2020-01-06

使用spring data配置多个数据源,比较好的方式是通过包名将不同的数据库相关的模块分开,然后,你的`DbConfig`也需要分成两个配置类,分别配置两个数据源的jpa配置,通过`@EnableJpaRepositories`的标签来控制不同的包名下的DAO类要使用哪个jpa的配置。

具体的方法,我找了一个链接,你可以参考一下:

https://www.baeldung.com/spring-data-jpa-multiple-databases

0
0

分布式事务实践,从原理到实例,解决数据一致性

掌握分布式事务实现技术,是架构师必备技能。

1149 学习 · 153 问题

查看课程