Spring JPA-找不到实体管理工厂,为什么?

时间:2018-12-27 21:26:37

标签: spring spring-boot gradle spring-data-jpa

请告诉我如何克服它。 我在构建时使用Gradle时遇到错误:

Description:
Field userRepository in com.fitness.api.controller.UserController required a bean named 'entityManagerFactory' that could not be found.

Action:
Consider defining a bean named 'entityManagerFactory' in your configuration.

应用文件:

@EnableJpaRepositories("com.test.api.repository")
@SpringBootApplication
public class ApiApplication {

    public static void main(String[] args) {
        SpringApplication.run(ApiApplication.class, args);
    }
}

application.properties

spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration

## Spring DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties)
# MySQL properties
spring.datasource.url = jdbc:mysql://localhost:3306/test?useSSL=false
spring.datasource.username = root
spring.datasource.password = 1234

## Hibernate Properties
# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect

# Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto = update

build.gradle

buildscript {
    ext {
        springBootVersion = '2.1.0.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

型号

@Entity
@Table(name = "user")
@EntityListeners(AuditingEntityListener.class)
@JsonIgnoreProperties(value = {"createdAt", "updatedAt"},
        allowGetters = true)
public class User implements Serializable {
}

似乎可以上课,但不能建立项目。 很长一段时间我都无法解决这个问题,也许有人知道解决方法。

1 个答案:

答案 0 :(得分:0)

如果您排除DataSourceAutoConfiguration类,则必须手动设置DataSource bean。

spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration