spring CrudRepository 无法解析为类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42396706/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CrudRepository cannot be resolved to a type
提问by Carrm
I'm working on a Spring Boot project (generated by Spring Initializr), using Maven. I want to create a CrudRepository, but I'm getting the error "CrudRepository cannot be resolved to a type" and the package org.springframework.data.repository does not contain the CrudRepository class. I tried to follow a bunch of tutorials to understand what is wrong and I didn't find anything. My POM looks right to me, I don't have any build failure when I run maven clean and package goals, I tried to update the project and download sources, but nothing works. Eclipse can't find the CrudRepository class.
我正在使用 Maven 处理 Spring Boot 项目(由 Spring Initializr 生成)。我想创建一个 CrudRepository,但我收到错误“CrudRepository 无法解析为类型”,并且包 org.springframework.data.repository 不包含 CrudRepository 类。我试图按照一堆教程来了解出了什么问题,但我什么也没找到。我的 POM 看起来不错,当我运行 maven clean 和 package 目标时,我没有任何构建失败,我尝试更新项目并下载源代码,但没有任何效果。Eclipse 找不到 CrudRepository 类。
Here is my POM file:
这是我的 POM 文件:
<?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>com.myquickstart</groupId>
<artifactId>SpringQuickstart</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>SpringQuickstart</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.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</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
And my repository:
还有我的存储库:
package com.myquickstart.repositories;
import org.springframework.stereotype.Repository;
import org.springframework.data.repository.CrudRepository;
import com.myquickstart.entities.Movie;
@Repository
public interface MovieRepository extends CrudRepository<Movie, Long> {
}
Thanks for your help
谢谢你的帮助
Edit:
编辑:
I've updated the project with the "Maven => Update project" in Eclipse. After that, I got this error in the "Problems" view:
我已经在 Eclipse 中使用“Maven => Update project”更新了项目。之后,我在“问题”视图中收到此错误:
C:/Users/carrm/.m2/repository/org/hibernate/hibernate-core/5.0.11.Final/hibernate-core-5.0.11.Final.jar' in project 'SpringQuickstart' cannot be read or is not a valid ZIP file
I'm not sure to know how to solve this/what is the link with my CrudRepository problem.
我不确定如何解决这个问题/我的 CrudRepository 问题的链接是什么。
采纳答案by Carrm
It looks like it was an issue with Maven libraries. I deleted the whole content in .m2/repositoryand ran Maven > Update projectin Eclipse, so that Maven had to download the whole content again. No more error after this!
看起来这是 Maven 库的问题。我删除了整个内容.m2/repository并Maven > Update project在 Eclipse中运行,因此 Maven 不得不再次下载整个内容。此后不再出错!
Edit
编辑
As pointed out by user3578953, executing maven-clean does the same thing that I did by deleting the whole m2 repository content. I didn't know much about Maven when I first asked this question, but this is obviously a better way to solve the issue.
正如user3578953所指出的,执行 maven-clean 与我通过删除整个 m2 存储库内容所做的相同。当我第一次问这个问题时,我对 Maven 了解不多,但这显然是解决问题的更好方法。
回答by Pinaki Mukherjee
P.S - I know the original answer used interface not class. But as we can't ask same question again. The answer is for beginner who may missed the interface part.
PS - 我知道原来的答案使用的接口不是类。但因为我们不能再问同样的问题。答案适用于可能错过界面部分的初学者。
We generally get this type of error when we create the repository as class instead of interface. CrudRepository is an interface, you most probably extend it using an interface only:
当我们将存储库创建为类而不是接口时,我们通常会遇到这种类型的错误。CrudRepository 是一个接口,你很可能只使用一个接口来扩展它:
Wrong (Some times when we create the repository we create a class by mistake ):
错误(有时当我们创建存储库时,我们错误地创建了一个类):
public class MovieRepository extends CrudRepository<Movie, Long>
Error: CrudRepository cannot be resolved to a type
错误:无法将 CrudRepository 解析为类型
Just changed it to interface:
只是将其更改为interface:
public interface MovieRepository extends CrudRepository<Movie, Long>
回答by user3578953
check mavendependencies for spring-data-jpa-...RELEASE.jar if it contains CrudRepository.class under org.springframework.data.jpa.repository package.
检查 spring-data-jpa-...RELEASE.jar 的 mavendependencies 是否包含 org.springframework.data.jpa.repository 包下的 CrudRepository.class。
If the class file is there, just maven->update project and run->maven-clean.
如果类文件在那里,只需 maven->update project 和 run->maven-clean。
If the class file is not there, just search the package in mvnrepository.com.
如果类文件不存在,只需在 mvnrepository.com 中搜索包。
回答by Alex Efimov
All seems nice with your dependencies, please try next variants:
您的依赖项看起来一切都很好,请尝试下一个变体:
- build project with Maven from command line(
$ mvn spring-boot:run) - In Eclipse IDE:
right click on project-Maven-update project
- 从命令行使用 Maven 构建项目(
$ mvn spring-boot:run) - 在 Eclipse IDE 中:
right click on project-Maven-update project
回答by Zach Pedigo
If you are having this problem, check if the org.springframework.data.repository.CrudRepository import is working. If it is then this answer probably won't be the solution. If it is not working and the "data" part is highlighted, then check your pom.xml and if you don't have it already, add the following dependency to it:
如果您遇到此问题,请检查 org.springframework.data.repository.CrudRepository 导入是否正常工作。如果是,那么这个答案可能不是解决方案。如果它不起作用并且“数据”部分突出显示,请检查您的 pom.xml,如果您还没有它,请向其中添加以下依赖项:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
After adding the dependency, delete your local .m2 directory and build from the command-line. You may also want to reimport the maven project to your workspace.
添加依赖项后,删除本地 .m2 目录并从命令行构建。您可能还想将 Maven 项目重新导入到您的工作区。
回答by samivic
i had the same problem, just delete all .m2 content and re-import Maven. it works!
我遇到了同样的问题,只需删除所有 .m2 内容并重新导入 Maven。有用!
回答by zee
CrudRepositoryis an interface not a class. Replace extendswith implements.
CrudRepository是一个接口而不是一个类。替换extends为implements。


