org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [com/itnull/xunwu/config/JPAConfig.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is java.lang.IllegalArgumentException: jdbcUrl is required with driverClassName.
问题原因:数据源配置项未获取,多数都是配置问题。
@Bean
@ConfigurationProperties(prefix="spring.datasource")
public DataSource dataSource() {
// return DataSourceBuilder.create().driverClassName("com.mysql.cj.jdbc.Driver")
// .password("root")
// .username("root")
// .url("jdbc:mysql://localhost:3306/learn?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=GMT&useSSL=false")
// .build();
return DataSourceBuilder.create().build();
}
本文探讨了在Spring Boot应用中配置JPA时常见的错误,具体分析了一个关于数据源配置缺失导致EntityManagerFactory初始化失败的问题。通过示例代码展示了正确的数据源配置方式,强调了在使用DataSourceBuilder创建数据源时,正确设置数据库连接参数的重要性。
1221

被折叠的 条评论
为什么被折叠?



