该配置适用于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;
}
}
1条答案
按热度按时间kgsdhlau1#
尝试使用Environment类进行属性的拾取,并在DriverManagerDataSource中设置值。标准JDBC DataSource接口的简单实现,通过bean属性配置普通的旧JDBC DriverManager,并从每个getConnection调用返回一个新的Connection。
这个类的一个缺点-不提供连接池功能,所以不推荐用于生产环境。为此,您可以集成Hikari Pooling机制,这是Spring-Data的默认DB池机制。