在 SpringBoot 中找不到 EntityManagerFactory

Ask*_*ker 0 java mysql spring-boot

我正在尝试使用 MySQL 和 SpringBoot 为员工创建登录名。我使用内存数据库使我的代码工作,但是一旦我将代码切换到 MySQL,它就停止工作。这个项目是一个科学怪人,所以我不确定我的一些组件是否可以一起工作。我不确定是否需要我的 App 类中的所有注释...错误如下:

描述:

Field employeeRepository in io.msela.springbootstarter.employee.EmployeeService 
required a bean named 'entityManagerFactory' that could not be found.
Run Code Online (Sandbox Code Playgroud)

行动:

考虑定义一个'entityManagerFactory'在您的配置中命名的 bean 。

这是我的App课程,它运行整个过程:

package io.msela;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

import io.msela.springbootstarter.employee.EmployeeRepository;

@SpringBootApplication
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class}) //added for MySQL
@ComponentScan({"io.msela"})
@EntityScan("io.msela.springbootstarter")
@EnableJpaRepositories(basePackageClasses = EmployeeRepository.class)
public class EmployeeApiDataApplication {

    public static void main(String[] args) {
        SpringApplication.run(EmployeeApiDataApplication.class, args);
    }
}
Run Code Online (Sandbox Code Playgroud)

这是 XML 文件和属性文件:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>io.msela</groupId>
    <artifactId>course-api-data</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>course-api-data</name>
    <description>Course API with Spring Data</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
        </dependency>

        <!-- <dependency>
            <groupId>org.apache.derby</groupId>
            <artifactId>derby</artifactId>
            <scope>runtime</scope>
        </dependency> -->


        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>
Run Code Online (Sandbox Code Playgroud)

和属性:

spring.jpa.hibernate.ddl-auto=create-drop
spring.datasource.url=jdbc:mysql://localhost:3306/db_example
spring.datasource.username=springuser
spring.datasource.password=ThePassword
spring.jpa.database-platform = org.hiberante.dialetct.MySQLSDialect
spring.datasource.driver-class-name = com.mysql.jdbc.Driver
Run Code Online (Sandbox Code Playgroud)

entityManager 应该在哪里?考虑到我的项目目前非常简单(有服务、控制、员工和应用程序类),我应该怎么做

rya*_*049 5

@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class}) //added for MySQL从您的EmployeeApiDataApplication班级中删除此行。

@SpringBootApplication注释隐含包括@EnableAutoConfiguration。也DataSourceAutoConfiguration有必要春季启动实例化DataSourceHibernateJpaAutoConfiguration需要来实例化EntityManagerFactory

看起来您已经包含了mysql:mysql-connector-java依赖项,这很好。如果最近添加了项目,请确保对项目进行了 Maven 更新,以便将其添加到类路径中。

你的属性应该是这样的

spring.jpa.hibernate.ddl-auto=create-drop
spring.datasource.url=jdbc:mysql://localhost:3306/db_example
spring.datasource.username=springuser
spring.datasource.password=ThePassword
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect
Run Code Online (Sandbox Code Playgroud)

com.mysql.jdbc.Driver取决于mysql:mysql-connector-java依赖。

还要确保create-drop是您想要的正确设置spring.jpa.hibernate.ddl-auto。您可以在/sf/answers/118283861/找到可能的值。

更新(经过 git 检查):

  1. 我删除了反对EmployeeApiDataApplication只留下的附加注释@SpringBootApplication。除非您确实有理由,否则您在这里不需要如此多的特殊性。

  2. 我更新application.properties以反映上述更改。

  3. 我删除了这些方法UserRepository并进行了更新UserService以处理这些更改。

我在这里创建了一个拉取请求https://github.com/MatejaSela/SpringBootEmployeeLogin/pull/1

希望这可以帮助。