Controller中的字段需要一个名为“entityManagerFactory”的bean,该bean无法找到

问题描述 投票:0回答:1

我简单地用尽了代码的错误。我用Google搜索并搜索了几个小时,没有任何建议/标记的解决方案适合我。我只是想不通为什么找不到这个entityManagerFactory类。

我使用MySQL作为后端数据库

项目的树结构:

├───.gradle
│   ├───4.3
│   │   ├───fileChanges
│   │   ├───fileContent
│   │   ├───fileHashes
│   │   └───taskHistory
│   └───buildOutputCleanup
├───.settings
├───bin
│   └───com
│        ├───controller
│        └───model
├───build
│   ├───classes
│   │   └───java
│   │       ├───main
│   │       │   └───com
│   │       │        ├───controller
│   │       │        └───model
│   │       └───test
│   ├───libs
│   ├───reports
│   │   └───tests
│   │       └───test
│   │           ├───classes
│   │           ├───css
│   │           ├───js
│   │           └───packages
│   ├───resources
│   │   └───main
│   ├───test-results
│   │   └───test
│   │       └───binary
│   └───tmp
│       ├───bootJar
│       ├───compileJava
│       ├───compileTestJava
│       └───jar
├───gradle
│   └───wrapper
└───src
    ├───main
    │   ├───java
    │   │   └───com
    │   │        ├───controller
    │   │        └───model
    │   └───resources
    └───test
        └───java

Application.java位于:src / main / java / com /

控制器在/ src / main / java / com / controller /下

AccountRepository和Account位于/ src / main / java / com / model下

主要课程:

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
    import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

    @SpringBootApplication
    @EnableJpaAuditing
    @EnableJpaRepositories
    public class Application {

        // Start application
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    }

AdminAccount类:

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@Entity
@Table(name = "ccs_account_admin")
@EntityListeners(AuditingEntityListener.class)
@JsonIgnoreProperties(value = {"createdAt", "updatedAt"}, allowGetters = true)
public class AdminAccount implements Serializable {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int id;

    @Column(name = "account_id")
    private int account_id; 

    @Column(name = "first_name")
    private String first_name;

    @Column(name = "last_name")
    private String last_name;

    @Column(name = "email")
    private String email;

    @Column(name = "password")
    private String password;

    public AdminAccount AdminAccount (String email, String password) {
        return this;
    }

    @Override
    public String toString() {
        return "Account [id=" + id + ", account_id=" + account_id + ", first_name=" + first_name + ", last_name="
                + last_name + ", email=" + email + ", password=" + password + "]";
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getAccount_id() {
        return account_id;
    }

    public void setAccount_id(int account_id) {
        this.account_id = account_id;
    }

    public String getFirst_name() {
        return first_name;
    }

    public void setFirst_name(String first_name) {
        this.first_name = first_name;
    }

    public String getLast_name() {
        return last_name;
    }

    public void setLast_name(String last_name) {
        this.last_name = last_name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

AdminAccountRepo:

@Repository
public interface AdminAccountRepository  extends JpaRepository<AdminAccount, Integer> {

    @Query("Select * from ccs_account_admin where email=:email and password=:password")
    AdminAccount getAccount(@Param("email") String email, @Param("password") String password);   
}

控制器:

@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private AdminAccountRepository adminAccountRepository; <--The problem happens here

    @GetMapping("/account") 
    public AdminAccount account(
            @RequestParam(value="email", required = true) String email,
            @RequestParam(value="password", required = true) String password)
    {       
        System.out.println("email:" + email);
        System.out.println("pass:" + password);
        AdminAccount account = adminAccountRepository.getAccount(email, password);      
        System.out.println("account: " + account);;
        return account;     
    }
}

摇篮

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:2.0.3.RELEASE")
    }
}

// Apply the java-library plugin to add support for Java Library
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

sourceCompatibility = 1.8
targetCompatibility = 1.8

// In this section you declare where to find the dependencies of your project
repositories {
    // Use jcenter for resolving your dependencies.
    // You can declare any Maven/Ivy/file repository here.
    jcenter()
    mavenCentral()

}

dependencies {
    // This dependency is exported to consumers, that is to say found on their compile classpath.
    //api 'org.apache.commons:commons-math3:3.6.1'

    // This dependency is used internally, and not exposed to consumers on their own compile classpath.
    //implementation 'com.google.guava:guava:23.0'

    // Use JUnit test framework
    testImplementation 'junit:junit:4.12'    
    // https://mvnrepository.com/artifact/junit/junit
    testCompile group: 'junit', name: 'junit', version: '4.12'   
    // https://mvnrepository.com/artifact/org.postgresql/postgresql
    compile group: 'org.postgresql', name: 'postgresql', version: '9.4.1212'
    // https://mvnrepository.com/artifact/mysql/mysql-connector-java
    compile group: 'mysql', name: 'mysql-connector-java', version: '8.0.12' 
    // https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-jdbc
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-jdbc', version: '2.0.4.RELEASE'
    // https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa', version: '2.0.4.RELEASE'
    // https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '2.0.4.RELEASE'

}

我尝试过不同的Annotations,删除.m2目录,重建整个事物,不同的依赖关系和各种其他东西......无济于事

编辑:

application.properties:

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

##PostgreSQL Configuration
#spring.jpa.hibernate.ddl-auto=none
#spring.datasource.url=jdbc:postgresql://185.83.216.7:5432/orderlyq
#spring.datasource.username=postgres
#spring.datasource.password=postgres
#
## Disable feature detection by this undocumented parameter. Check the org.hibernate.engine.jdbc.internal.JdbcServiceImpl.configure method for more details.
#spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults = false
#
## Because detection is disabled you have to set correct dialect by hand.
#spring.jpa.database-platform=org.hibernate.dialect.PostgreSQL9Dialect


#MySQL Configuration
spring.jpa.hibernate.ddl-auto=none
spring.datasource.url=jdbc:myql://127.0.0.1:3306/cc_stats
spring.datasource.username=root
spring.datasource.password=password

错误信息:

Description:

Field adminAccountRepository in com.controller.LoginController required a bean named 'entityManagerFactory' that could not be found.


Action:

Consider defining a bean named 'entityManagerFactory' in your configuration.
java spring spring-boot jpa gradle
1个回答
1
投票

您已经排除了Spring Boot的DataSource自动配置,并且似乎没有手动配置DataSource bean。 JPA需要一个DataSource,所以没有一个,JPA将不会自动配置。因此,没有EntityManagerFactory bean可用,因此你看到的失败。

要解决这个问题,你需要一个DataSource bean。获取一个的最简单方法是删除禁用自动配置的配置:

spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
© www.soinside.com 2019 - 2024. All rights reserved.