我正在尝试使用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


实体管理器应该在哪里?考虑到我的项目目前非常简单,我该如何做(具有服务,控件,员工和应用程序类)

最佳答案

@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class}) //added for MySQL类中删除此行EmployeeApiDataApplication

@SpringBootApplication注释隐式包括@EnableAutoConfiguration。弹簧引导实例化DataSourceAutoConfiguration时还需要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以反映上面指出的更改。
我从UserRepository中删除​​了方法,并更新了UserService以处理这些更改。


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

希望这可以帮助。

07-24 09:38
查看更多