我正在尝试使用 MySQL 和 SpringBoot 为员工创建登录名。我使用内存数据库让我的代码工作,但是当我将代码切换到 MySQL 时,它就停止工作了。这个项目是一个弗兰肯斯坦,所以我不确定我的一些组件是否可以一起工作。我不确定我的 App 类中的所有注释是否都需要...错误如下:
描述:
Field employeeRepository in io.msela.springbootstarter.employee.EmployeeService
required a bean named 'entityManagerFactory' that could not be found.
行动:
考虑在您的配置中定义一个名为 '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);
}
}
这是 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>
和属性:
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
entityManager 应该在哪里?考虑到我的项目目前非常简单(有服务、控件、员工和应用程序类),我应该如何实现它
最佳答案
从您的 EmployeeApiDataApplication
类中删除此行 @EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})//linked for MySQL
。
@SpringBootApplication
注释隐式包含 @EnableAutoConfiguration
。此外,DataSourceAutoConfiguration
对于 spring-boot 实例化 DataSource
是必需的,而 HibernateJpaAutoConfiguration
对于实例化 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
com.mysql.jdbc.Driver
依赖于 mysql:mysql-connector-java
依赖项。
还要确保 create-drop
是您在 spring.jpa.hibernate.ddl-auto
上所需的正确设置。您可以在此处找到可能的值 https://stackoverflow.com/a/1689769/ .
更新(git 检查后):
我删除了针对
EmployeeApiDataApplication
的附加注释,仅留下@SpringBootApplication
。除非您确实有理由这样做,否则您不需要在这里如此具体。我更新了
application.properties
以反射(reflect)上述更改。我从
UserRepository
中删除了方法,并更新了UserService
来处理这些更改。
我在这里创建了一个拉取请求 https://github.com/MatejaSela/SpringBootEmployeeLogin/pull/1
希望这有帮助。
关于java - SpringBoot中找不到EntityManagerFactory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45350546/