如何使用spring boot配置hibernate

时间:2017-06-18 06:29:07

标签: java spring hibernate spring-boot spring-data-jpa

使用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

1 个答案:

答案 0 :(得分:4)

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