Java 如何对返回列表而不是Iterable的Crudepository使用findAll()
我想写一个FindAll()方法,它返回所有学生对象的列表。但是Crudepository只有Iterable findAll() 目标是让所有学生都在一个列表中,并将其传递给API控制器,这样我就可以使用http get获取所有学生 将此方法转换为List FindAll()的最佳方法是什么 在我当前的代码中,StudentService中的findAll方法提供了找到的不兼容类型:Iterable。必需:列表错误 服务Java 如何对返回列表而不是Iterable的Crudepository使用findAll(),java,spring,spring-boot,spring-mvc,Java,Spring,Spring Boot,Spring Mvc,我想写一个FindAll()方法,它返回所有学生对象的列表。但是Crudepository只有Iterable findAll() 目标是让所有学生都在一个列表中,并将其传递给API控制器,这样我就可以使用http get获取所有学生 将此方法转换为List FindAll()的最佳方法是什么 在我当前的代码中,StudentService中的findAll方法提供了找到的不兼容类型:Iterable。必需:列表错误 服务 @Service @RequiredArgsConstructor pu
@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();
}
}
@服务
@所需参数构造函数
公共类StudentServiceImpl实现StudentService{
@自动连线
私人期末考试预备课程预备课程预备课程;
//找到不兼容的类型:Iterable。必需:列表
公共列表findAll(){
return studentRepository.findAll();
}
}
API控制器
@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());
}
}
@RestController
@请求映射(“/api/v1/students”)
公共班级学生控制器{
私人期末考试预备课程预备课程预备课程;
公共学生管理员(学生职位学生职位){
this.studentRepository=studentRepository;
}
@GetMapping
公共响应findAll(){
返回ResponseEntity.ok(studentserviceinpl.findAll());
}
}
StudentRepository
public interface StudentRepository extends CrudRepository<Student, Long> {
}
公共界面StudentRepository扩展了crudepository{
}
两个选项:
findAll()
方法以返回列表-请参阅如果有许多服务将返回一个列表,我建议使用第二个选项设置您在必须执行多次操作时提取逻辑。您只需在
StudentRepository
界面中定义一个抽象方法list findAll()
。简单地说:
public interface StudentRepository extends CrudRepository<Student, Long> {
List<Student> findAll();
}
公共界面StudentRepository扩展了crudepository{
列出findAll();
}
如果让StudentRepository从JpaRepository继承,则通过返回一个列表,可以使用findAll()
方法
public interface StudentRepository extends JpaRepository<Student, Long> {
}
public interface StudentRepository扩展了JpaRepository{
}
参考资料:
对于CRUDEPository,需要使用lambda表达式才能返回列表
public List<Student> findAll() {
List<Student> students = new ArrayList<>();
studentRepository.findAll().forEach(students::add);
return students;
}
公共列表findAll(){
List students=new ArrayList();
studentRepository.findAll().forEach(学生::添加);
留学生;
}
默认列表getStudentList(){
最终列表studentList=新建LinkedList();
Iterable Iterable=findAll();
iterable.forEach(studentList::add);
返回学生名单;
}
对于尚未使用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;
}
公共列表findAllStudents(){
Iterable findAllIterable=studentRepository.findAll();
返回映射列表(findAllIterable);
}
私有列表映射列表(Iterable Iterable){
List-listOfStudents=new-ArrayList();
用于(学生:iterable){
学生名单。添加(学生);
}
返回学生名单;
}
有一种更简单的方法:
List<Student> studentsList;
studentsList = (List<Student>) studentRepository.findAll();
列出学生名单;
studentsList=(List)studentRepository.findAll();
将此添加到crudepository
:
公共界面StudentRepository扩展了crudepository{
列出findAll();
}
派对迟到,但我通常是这样做的(使用streams/collect)。这里假设CRUD存储库为DingDong
:
List<DingDong> dingdongs = StreamSupport //
.stream(repository.findAll().spliterator(), false) //
.collect(Collectors.toList());
List dingdongs=StreamSupport//
.stream(repository.findAll().spliterator(),false)//
.collect(Collectors.toList());
List<DingDong> dingdongs = StreamSupport //
.stream(repository.findAll().spliterator(), false) //
.collect(Collectors.toList());