无法解析类型 org.springframework.data.repository.Repository。它是从所需的 .class 文件中间接引用的

The type org.springframework.data.repository.Repository cannot be resolved. It is indirectly referenced from required .class files

我是 spring Boot 的新手,当我按照 Spring 指南-使用 MySQL

访问数据时遇到了这个问题

我的 STS IDE 出现错误,我不知道到底发生了什么。 在src/main/java/UserRepository.java

 package hello;

 import org.springframework.data.repository.CrudRepository;

 import hello.User;



  public interface UserRepository extends CrudRepository<User, Long> {

}

STS 确实警告我这个错误,我忽略它直到我 运行 Cmd: mvn spring-boot 所以我回来查看警告,它是:

The type org.springframework.data.repository.Repository cannot be 
resolved. It is indirectly referenced from required .class files

非常感谢您的帮助!

我按照推荐使用 Maven 构建项目,Pom.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>org.springframework</groupId>
    <artifactId>gs-mysql-data</artifactId>
    <version>0.1.0</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.RELEASE</version>
    </parent>

    <dependencies>
        
    
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        
        <!-- JPA Data (We are going to use Repositories, Entities, Hibernate, etc...) -->
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        
        <!-- Use MySQL Connector-J -->
        
        <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>

    <properties>
        <java.version>1.8</java.version>
    </properties>

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

对代码应用一些 spring 引导标准

  1. 将您的启动应用程序放在包的顶部,在您的情况下,您的 BootApplication 应该在 hello 包中。如下图

    @SpringBootApplication
    public class BootApplication extends SpringBootServletInitializer {
    
    public static void main(String args[]) {
    SpringApplication.run(BootApplication.class, args);
    }
    }
    
  2. 现在将您的存储库保存在 hello 的 sub-package 中。在你的情况下,它应该在 hello.repository

现在尝试 运行 使用 BootApplication。

老兄,它不起作用anyway.This 异常是由于CrudRepository.The eclipse 无法解决这个class.My 项目很简单,我从[=44 得到这个项目=] website.its指南--使用MySQL

访问数据

现在我的项目与 [gs-accessing-data-mysql-complete]

完全相同

Spring官网认为是成功的项目,让培训师[gs-accessing-data-mysql-initial]对照[gs-accessing-data-mysql-complete] .Howerver,即使是后者也无法构建successfully.My 项目结构:

 src
     +main
        +java
          +hello
              --Application.java
              --MainController.java
              --User.java
              --UserRepository.java

还有日志信息,我最好给你看一下摘要: 1.[ERROR] 无法在项目 gs-mysql-data 上执行目标 org.springframework.boot:spring-boot-maven-plugin:2.0.0.RELEASE:运行 (default-cli): An 运行ning 时出现异常。 null:InvocationTargetException:创建名称为 'mainController' 的 bean 在文件 [D:\workbunch\Spring Workbunch\gs-accessing-data-mysql-initial\target\classes\hello\MainController.class] 中定义时出错:合并 bean 定义的 Post-processing 失败;嵌套异常是 java.lang.IllegalStateException:无法从 ClassLoader [java.net.URLClassLoader@6424f2f4] 内省 Class [hello.MainController]:org/springframework/data/repository/CrudRepository:org.springframework.data.repository.CrudRepository -> [求助1]