简体   繁体   English

Spring存储库实现问题

[英]Spring Repository Library Implementation Problem

I'm using Spring data JDBC library to implement my DAO. 我正在使用Spring数据JDBC库来实现我的DAO。 I"m very new to Spring Data Repository. Below is my error encounter. 我是Spring Data Repository的新手。以下是我遇到的错误。

@Repository
public class UserRepository extends CrudRepository<User, Integer> {}

Error : The type CrudRepository cannot be the superclass of UserRepository; 错误 :类型CrudRepository不能是UserRepository的超类。 a superclass must be a class 超类必须是一个类

I'm using Spring Framework version 5.1.3 and spring-data-jdbc 1.0.3. 我正在使用Spring Framework版本5.1.3和spring-data-jdbc 1.0.3。

The CrudRepository provides sophisticated CRUD functionality for the entity class that is being managed. CrudRepository为正在管理的实体类提供复杂的CRUD功能。

 public interface CrudRepository<T, ID extends Serializable>
        extends Repository<T, ID> {

    <S extends T> S save(S entity);
    T findOne(ID primaryKey);
    Iterable<T> findAll();
    Long count();                                                                                                                   
    void delete(T entity);                                                                                                  
    boolean exists(ID primaryKey)
}
  • Saves the given entity. 保存给定的实体。
  • Returns the entity identified by the given id. 返回由给定ID标识的实体。
  • Returns all entities. 返回所有实体。
  • Returns the number of entities. 返回实体数。
  • Deletes the given entity. 删除给定的实体。
  • Indicates whether an entity with the given id exists. 指示是否存在具有给定id的实体。

If you are looking for a simple query to get all data from you database just use Spring CrudRepository : 如果您正在寻找一个简单的查询来从数据库中获取所有数据,请使用Spring CrudRepository

@Repository
public interface UserRepository extends CrudRepository<User, Integer> {
}

To get your data just use the method findAll already present in CrudRepository : 要获取数据,只需使用CrudRepository已经存在的findAll方法:

Iterable<User> users = userRepository.findAll();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM