zoukankan      html  css  js  c++  java
  • [Java Spring data] Paging and sorting

    package com.example.university.repo;
    
    import com.example.university.domain.Staff;
    import org.springframework.data.domain.Page;
    import org.springframework.data.domain.Pageable;
    import org.springframework.data.domain.Sort;
    import org.springframework.data.repository.PagingAndSortingRepository;
    
    public interface StaffRepository extends PagingAndSortingRepository<Staff,Integer> {
    }
    public interface CourseRepository extends CrudRepository<Course,Integer>{
    
        List<Course> findByCredits(@Param("credits") int credits);
    
        Page<Course> findByCredits(@Param("credits") int credits, Pageable pageable);
    
    }

    Usage:

        @Test
        public void pagingAndSortingQueries() {
            System.out.println("
    Find all 3-credit courses");
            courseRepository.findByCredits(3).forEach(System.out::println);
    
            System.out.println("
    Find first 4 3-credit courses, sort by credit, then course name");
            Page<Course> courses = courseRepository.findByCredits(3,
                    PageRequest.of(0, 4, Sort.Direction.ASC, "credits", "name"));
            courses.forEach(System.out::println);
    
            System.out.println("
    Find all staff members, sort alphabetically by last name");
            Sort sortByLastName = new Sort(Sort.Direction.ASC, "member.lastName");
            staffRepository.findAll(sortByLastName).forEach(System.out::println);
    
            Page<Staff> members = staffRepository.findAll(PageRequest.of(0, 5, sortByLastName));
            System.out.println("
    Total number of staff members=" + members.getTotalElements());
            System.out.println("Total number of 5-element-pages=" + members.getTotalPages());
            System.out.println("Find first 5 Staff members, sort alphabetically by last name");
            members.forEach(System.out::println);
        }
    

      

  • 相关阅读:
    表单提交
    js 设备判断(移动端pc端 安卓ios 微信)
    js 页面history.back()返回上一页,ios 不重新加载ready的解决办法
    移动端解决单机事件延迟fastclick
    定时器时间动态变化(变速)
    gulp配置(编译压缩转码自动刷新注释全)
    ios和安卓H5交互桥接
    倒计时
    PHP跨域jsonp方式
    1.Redis简介/配置文件
  • 原文地址:https://www.cnblogs.com/Answer1215/p/14153047.html
Copyright © 2011-2022 走看看