本文介绍了Spring Boot:错误要求在 JpaRepository 中实现所有方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么会这样?几个月前我在 STS 的一个春季项目中工作时并没有发生这种情况.我在课堂上导入了以下内容:
Why is this happening? It didn't happen when I worked on a spring project in STS few months back.I have imported the following in the class:
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
以下是我的 pom.xml:
Following is my 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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.abcd</groupId>
<artifactId>abcd</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>abcd</name>
<description></description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</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>
推荐答案
自定义存储库必须是一个接口,并且必须从 JpaRepository
或 CrudRepository
或 PagingAndSortingRepository
或 MongoRepository
,所以通过以下方式更新:
A Custom Repository has to be an interface and this has to extend from JpaRepository
or CrudRepository
or PagingAndSortingRepository
or MongoRepository
, so update by following way:
@Repository
public interface UserRepository extends JpaRepository<User, Long> {....}
参考资料
这篇关于Spring Boot:错误要求在 JpaRepository 中实现所有方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!