jpa SpringBoot 3 -创建在类路径资源中定义的名为“entityManagerFactory”的bean时出错

3zwtqj6y  于 2023-05-07  发布在  Spring
关注(0)|答案(1)|浏览(252)

该配置适用于SpringBoot 2。实体管理器工厂未启动
同时看到这些错误
无法生成Hibernate SessionFactory;嵌套异常为java.lang.IllegalArgumentException:需要dataSource或dataSourceClassName或jdbcUrl。
原因:java.lang.IllegalArgumentException:需要dataSource或dataSourceClassName或jdbcUrl。

@Configuration
@EnableJpaRepositories(basePackages = { "com.repository"} )
public class DataSourceConfiguration {

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

    @Bean(name="jdbcTemplate")
    public JdbcTemplate dwJdbcTemplate(@Qualifier("dataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

    @Primary
    @Bean(name = "entityManagerFactory")
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(@Qualifier("dataSource") DataSource dataSource) throws URISyntaxException {
        LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
        entityManagerFactoryBean.setDataSource(dataSource);
        entityManagerFactoryBean.setPackagesToScan("com.entity");
        entityManagerFactoryBean.setJpaVendorAdapter(jpaVendorAdapter());

        return entityManagerFactoryBean;
    }

    @Bean
    public JpaVendorAdapter jpaVendorAdapter() {
        HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter();
        hibernateJpaVendorAdapter.setShowSql(false);
        hibernateJpaVendorAdapter.setGenerateDdl(true);
        hibernateJpaVendorAdapter.setDatabase(Database.MYSQL);
        return hibernateJpaVendorAdapter;
    }

    @Primary
    @Bean(name = "transactionManager")
    public PlatformTransactionManager transactionManager(
            @Qualifier("entityManagerFactory") EntityManagerFactory
                    entityManagerFactory
    ) {
        JpaTransactionManager transactionManager=new JpaTransactionManager(entityManagerFactory);

        return transactionManager;
    }
}
kgsdhlau

kgsdhlau1#

尝试使用Environment类进行属性的拾取,并在DriverManagerDataSource中设置值。标准JDBC DataSource接口的简单实现,通过bean属性配置普通的旧JDBC DriverManager,并从每个getConnection调用返回一个新的Connection。
这个类的一个缺点-不提供连接池功能,所以不推荐用于生产环境。为此,您可以集成Hikari Pooling机制,这是Spring-Data的默认DB池机制。

@Configuration
@EnableJpaRepositories(basePackages = {
    "com.xxx", "com.yyy", "com.zzz" }, entityManagerFactoryRef = "dbEntityManager", transactionManagerRef = "dbTransactionManager")
public class DataSourceConfiguration {

@Autowired
private Environment env;

@Bean(name = "datasource")
@Primary
public DataSource dbDataSource() {
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setDriverClassName(env.getProperty("spring.datasource.driver-class-name"));
    dataSource.setUrl(env.getProperty("spring.datasource.url"));
    dataSource.setUsername(env.getProperty("spring.datasource.username"));
    dataSource.setPassword(env.getProperty("spring.datasource.password"));
    return dataSource;
}

/**
* Implemented Hikari Pool mechanism
* @Bean(name = "datasource")
* @Primary
* public DataSource dbDataSource() {
*       final HikariConfig config = new HikariConfig();
*       final HikariDataSource dataSource;
*       config.setPoolName("HikariPool");
*       config.setJdbcUrl(env.getProperty("spring.datasource.url") ); 
*       config.setUsername(env.getProperty("spring.datasource.username") );
*       config.setPassword(env.getProperty("spring.datasource.password"));
*       config.setDriverClassName(env.getProperty("spring.datasource.driver-class-name"));
*       config.setMaximumPoolSize(env.getProperty("spring.datasource.hikari.maximumPoolSize"));
*       config.setMaxLifetime(840000);
*       dataSource = new HikariDataSource(config);
* }
*/

@Primary
@Bean(name = "dbTransactionManager")
public PlatformTransactionManager dbTransactionManager() {
    JpaTransactionManager transactionManager = new JpaTransactionManager();
    transactionManager.setEntityManagerFactory(dbEntityManager().getObject());
    return transactionManager;
    }
}

@Primary
@Bean(name = "dbEntityManager")
public LocalContainerEntityManagerFactoryBean dbEntityManager() {
    LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
    em.setDataSource(dbDataSource());
    em.setPackagesToScan(
            new String[] { "com.xxx", "com.yyy", "com.zzz" });
    em.setPersistenceUnitName("dbEntityManager");
    HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
    em.setJpaVendorAdapter(vendorAdapter);
    return em;
}

@Bean(name = "jdbcTemplate")
public JdbcTemplate jdbcTemplate1(@Qualifier("datasource") DataSource ds) {
    return new JdbcTemplate(ds);
}

相关问题