如何在 CrudRepository 上使用 findAll() 返回 List 而不是 Iterable



我想编写一个返回所有学生对象的列表的 FindAll(( 方法。但是 CRUDRepository 只有 Iterable<>findAll((。

目标是将所有学生都放在一个列表中,并将其传递给 API 控制器,这样我就可以使用 http GET 获取所有学生。

将此方法转换为列表的最佳方法是什么<> FindAll((

在我当前的代码中,学生服务中的findAll方法为我提供了找到的不兼容类型:可迭代。必需:列表错误。

服务

@Service
@RequiredArgsConstructor
public class StudentServiceImpl implements StudentService {
    @Autowired
    private final StudentRepository studentRepository;
    //Incompatible types found: Iterable. Required: List
    public List<Student> findAll() {
        return studentRepository.findAll();
    }
}

接口控制器

@RestController
@RequestMapping("/api/v1/students")
public class StudentAPIController {
    private final StudentRepository studentRepository;
    public StudentAPIController(StudentRepository studentRepository) {
        this.studentRepository = studentRepository;
    }
    @GetMapping
    public ResponseEntity<List<Student>> findAll() {
        return ResponseEntity.ok(StudentServiceImpl.findAll());
    }
}

学生存储库

public interface StudentRepository extends CrudRepository<Student, Long> {
}

您可以简单地定义一个抽象方法List<Student> findAll()StudentRepository接口中。像这样简单的事情:

public interface StudentRepository extends CrudRepository<Student, Long> {
    List<Student> findAll();
}

如果你让 StudentRepository 从 JpaRepository 继承,你通过返回一个 List 来获得 findAll() 方法。

public interface StudentRepository extends JpaRepository<Student, Long> {
}

参考资料:

https://docs.spring.io/spring-data/jpa/docs/current/api/org/springframework/data/jpa/repository/JpaRepository.html#findAll--

对于 CrudRepository,您需要使用 lambda 表达式才能返回列表

public List<Student> findAll() {
    List<Student> students = new ArrayList<>();
    studentRepository.findAll().forEach(students::add);
    return students;
}
default List<Student> getStudentList() {
    final List<Student> studentList = new LinkedList<>();
    Iterable<Student> iterable = findAll();
    iterable.forEach(studentList::add);
    return studentList;
}

派对迟到了,但这是我通常这样做的方式(使用流/收集(。这里假设 CRUD 存储库为 DingDong

List<DingDong> dingdongs = StreamSupport //
    .stream(repository.findAll().spliterator(), false) //
    .collect(Collectors.toList());

两个选项:

  1. 从服务方法中的可迭代对象实例化一个列表,并像将迭代器转换为 ArrayList 中一样返回该列表

  2. 覆盖默认的 Spring 数据findAll()方法以返回列表 - 请参阅 https://docs.spring.io/spring-data/data-commons/docs/1.6.1.RELEASE/reference/html/repositories.html#repositories.custom-implementations

如果有许多服务将返回列表,我建议您使用第二个选项来设置您在必须提取几次时提取逻辑。

对于那些还没有使用 lambda 表达式但仍希望看到有效解决方案的人来说,这是一种旧方法:

public List<Student> findAllStudents() {
    Iterable<Student> findAllIterable = studentRepository.findAll();
    return mapToList(findAllIterable);
}
private List<Student> mapToList(Iterable<Student> iterable) {
    List<Student> listOfStudents = new ArrayList<>();
    for (Student student : iterable) {
        listOfStudents.add(student);
    }
    return listOfStudents;
}

有一个更简单的方法:

List<Student> studentsList;     
studentsList = (List<Student>) studentRepository.findAll();

CrudRepository中添加以下内容:

public interface StudentRepository extends CrudRepository<Student, Long> {
    List<Student> findAll();
}

最新更新