Spring Boot MySQL REST错误-需要一个名为“entityManagerFactory”/“emf”的bean,但找不到该bean

Spring Boot MySQL REST错误-需要一个名为“entityManagerFactory”/“emf”的bean,但找不到该bean,mysql,rest,spring-boot,Mysql,Rest,Spring Boot,我正在尝试使用SpringBootMySQL创建一个非常简单的REST服务。但无法绕过错误。我做了很多搜索,大多数结果告诉我,下面的代码足以构建一个可以执行CRUD操作的基本REST服务 我一直在犯错误 *************************** APPLICATION FAILED TO START *************************** Description: Field repo in org.bluedolphin.spring.data.mysql.Act

我正在尝试使用SpringBootMySQL创建一个非常简单的REST服务。但无法绕过错误。我做了很多搜索,大多数结果告诉我,下面的代码足以构建一个可以执行CRUD操作的基本REST服务

我一直在犯错误

***************************
APPLICATION FAILED TO START
***************************
Description:
Field repo in org.bluedolphin.spring.data.mysql.ActorController required a bean named 'entityManagerFactory' that could not be found.
Action:
Consider defining a bean named 'entityManagerFactory' in your configuration.
一旦我加上这一行

@EnableJpaRepositories(basePackages="org.bluedolphin.spring.data.mysql", entityManagerFactoryRef="emf")
在文件SpringDataMysqlApplication中,我开始得到以下错误

***************************
APPLICATION FAILED TO START
***************************
Description:
Field repo in org.bluedolphin.spring.data.mysql.ActorController required a bean named 'emf' that could not be found.
Action:
Consider defining a bean named 'emf' in your configuration.
整个代码如下所示

应用程序属性

#Server details
server.port=8180
# Replace with your connection string
spring.datasource.url=jdbc:mysql://localhost:3307/sakila
# Replace with your credentials
spring.datasource.username=root
spring.datasource.password=password
spring.datasource.driverClassName=com.mysql.jdbc.Driver
SpringDataMysqlApplication

@SpringBootApplication
@EnableJpaRepositories(basePackages="org.bluedolphin.spring.data.mysql", entityManagerFactoryRef="emf")
public class SpringDataMysqlApplication {

    private static final Logger log = LoggerFactory.getLogger(SpringDataMysqlApplication.class);

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

动作还原

public interface ActorRepository extends CrudRepository<Actor, Long> {

}
聚甲醛


错误消息是不言自明的,您没有定义实体管理器工厂bean。您可以在spring配置xml文件中定义它,如下所示

<bean id="emf"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceUnitName" value="DataPersistencePersistenceUnit" />
    <property name="dataSource" ref="yourDataSource" />
</bean>

我没有spring配置文件,更喜欢使用注释。尝试按建议添加@EnableJpaRepositories批注,但出现相同错误:看看
<?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>org.bluedolphin.spring.data.mysql</groupId>
    <artifactId>spring-data-mysql</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>spring-data-mysql</name>
    <description>spring-data-mysql</description>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.BUILD-SNAPSHOT</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-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</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>
    <repositories>
        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </pluginRepository>
        <pluginRepository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>
</project>
<bean id="emf"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceUnitName" value="DataPersistencePersistenceUnit" />
    <property name="dataSource" ref="yourDataSource" />
</bean>
@EnableJpaRepositories(basePackages="org.bluedolphin.spring.data.mysql")