gpt4 book ai didi

java - 如何使用 Spring Boot 配置 Hibernate

转载 作者:行者123 更新时间:2023-12-01 17:56:10 27 4
gpt4 key购买 nike

使用 hibernate 配置时出现以下错误

Parameter 0 of constructor in org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration required a bean of type 'javax.sql.DataSource' that could not be found.
- Bean method 'dataSource' not loaded because @ConditionalOnProperty (spring.datasource.jndi-name) did not find property 'jndi-name'
- Bean method 'dataSource' not loaded because @ConditionalOnBean (types: org.springframework.boot.jta.XADataSourceWrapper; SearchStrategy: all) did not find any beans

行动:

考虑重新审视上述条件或在配置中定义“javax.sql.DataSource”类型的 bean。

这是我正在使用的 Application.java 文件

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
@SpringBootApplication
@EnableAutoConfiguration(exclude={ DataSourceAutoConfiguration.class,
DataSourceTransactionManagerAutoConfiguration.class,JpaRepositoriesAutoConfiguration.class})
public class Application {
public static void main(String[] args) throws Throwable {

SpringApplication.run(Application.class, args);
}
}

属性文件 -

spring.jpa.hibernate.ddl-auto=create-drop
spring.datasource.url=jdbc:oracle:thin:@localhost:1521:ORCL
spring.datasource.username=system
spring.datasource.password=****
spring.datasource.driver-class-oracle.jdbc.driver.OracleDriver
spring.datasource.testWhileIdle = true
spring.datasource.validationQuery = SELECT 1

spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto = update
spring.jpa.hibernate.naming-strategy =
org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

最佳答案

您已排除了 DataSourceAutoConfiguration,这意味着您必须为 DataSource 手动指定 @Bean。由于排除,application.properties 中的 DataSource 配置将不会被加载。

关于java - 如何使用 Spring Boot 配置 Hibernate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44612344/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com