EntityManagerFactoryBuilder

EntityManagerFactoryBuilder

在一个Spring Boot应用程序中,我试图建立多个数据库连接。我已经开始构建主数据源,但是在mySqlEntityManagerFactory方法上遇到以下错误。

无法自动接线。没有EntityManagerFactoryBuilder的bean

import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.PersistenceContext;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

@Configuration
@Transactional
@EnableTransactionManagement
@EnableJpaRepositories(
        basePackages = "digital.sheppard.dao",
        entityManagerFactoryRef = "entityManager",
        transactionManagerRef = "transactionManager")
public class PrimaryDBConfig {

    @Bean(name="dataSource")
    @Primary
    @ConfigurationProperties(prefix = "primary.datasource.mysql")
    public DataSource mysqlDataSource() {
        return DataSourceBuilder.create().build();
    }

    @PersistenceContext(unitName = "primary")
    @Primary
    @Bean(name = "entityManager")
    public LocalContainerEntityManagerFactoryBean mySqlEntityManagerFactory(EntityManagerFactoryBuilder builder) {
        return builder.dataSource(mysqlDataSource()).persistenceUnit("primary").properties(jpaProperties())
                .packages("digital.sheppard.model").build();
    }

    private Map<String, Object> jpaProperties() {
        Map<String, Object> props = new HashMap<String, Object>();
        props.put("hibernte.ejb.naming_strategy", "org.hibernate.cfg.ImprovedNamingStrategy");
        props.put("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");

        return props;
    }

}

我将如何自动连接EntityManagerFactoryBuilder?

我正在尝试遵循此博客上的代码https://raymondhlee.wordpress.com/2015/10/31/configuring-multiple-jpa-entity-managers-in-spring-boot/

这是主要的应用程序类(如果有帮助的话)
@Configuration
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
@ComponentScan
public class Application extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

最佳答案

我认为您应该删除此代码

@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})

07-27 13:17