Controller
import org.springframework.data.domain.Sort;
//排序列
String property = "";
switch (sorttype){//sorttype是前端传入的参数
case 1:property="name";//根据姓名排序
case 2:property="sex";//根据性别排序
case 3:property="stuno";//根据学号排序
default:
property="id";//根据id排序
}
Sort.Order order = new Sort.Order(Sort.Direction.DESC,property);
Sort sort = new Sort(order);
List<MyStudent> queryStudentSortByProperty(Sort sort);
import org.springframework.data.domain.Sort;
//排序列
String property = "";
switch (sorttype){//sorttype是前端传入的参数
case 1:property="name";//根据姓名排序
case 2:property="sex";//根据性别排序
case 3:property="stuno";//根据学号排序
default:
property="id";//根据id排序
}
Sort.Order order = new Sort.Order(Sort.Direction.DESC,property);
Sort sort = new Sort(order);
List<MyStudent> list = service.queryStudentSortByProperty(sort);
Service
@Query(value="select * from student order by ?#{#sort}" , nativeQuery = true)List<MyStudent> queryStudentSortByProperty(Sort sort);
本文介绍了一个使用Spring Data JPA实现的排序查询功能。通过前端传递不同的排序类型参数,后端可以灵活地对数据库中的学生信息进行升序或降序排列,并展示了具体的实现代码。
2494

被折叠的 条评论
为什么被折叠?



