因此,我有两个类 User
和 Car
的作业,分别具有 OneToMany 关系。
我的任务是在后端作为/cars 和/users 根端点实现搜索和排序。
示例:- GET /users?find=Teet&sort=name:asc
我该怎么做,您能详细说明我的步骤吗? 我目前找不到任何正确的解释:(
附注我正在扩展 JpaRepository 以实现基本的 CRUD 操作。
谢谢您! :)
最佳答案
如果您使用 Spring JPA,那么您可以将数据排序为
Page<Passenger> page = repository.findAll(PageRequest.of(0, 1, Sort.by(Sort.Direction.ASC, "seatNumber")));
对于 hibernate 标准,您可以使用
Criteria c = session.createCriteria(Cat.class);
c.createAlias("mother.kind", "motherKind");
c.addOrder(Order.asc("motherKind.value"));
return c.list();
关于Java spring 排序和过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57652208/