SpringBoot整合SpringDataJpa 之 PagingAndSortingRepository

PagingAndSortingRepository接口继承了CrudRepository接口,提供了分页与排序的操作。

示例:

准备工作:

实体类:

@Entity
@Table(name = "tb_dept")
public class Dept {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer deptno;

    @Column
    private String dname;

    @Column
    private String loc;

    //……getter/setter、默认构造方法、全参构造方法
}

第一步:创建Maven项目,添加依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
</dependency>

第二步:修改application.yml:

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/db_test?useSSL=false&serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=UTF8&autoReconnect=true&failOverReadOnly=false
    username: root
    password: root
    driver-class-name: com.mysql.cj.jdbc.Driver
  jpa:
    hibernate:
      ddl-auto: update
    properties:
      hibernate:
        enable_lazy_load_no_trans: true
    show-sql: true

第三步:编写接口DeptDao.java:

public interface DeptDao extends PagingAndSortingRepository<Dept, Integer> {

}

第四步:提供测试代码:

@RunWith(SpringRunner.class)
@SpringBootTest
@EnableAutoConfiguration
public class DeptDaoTest {
    @Autowired
    private DeptDao deptDao;

    @Test
    public void testPagingAndSortingRepositorySort() {
        //Order	定义了排序规则
        Sort.Order order = new Sort.Order(Sort.Direction.DESC, "id");
        //Sort对象封装了排序规则
        Sort sort = Sort.by(Sort.Direction.DESC, "loc");
        List<Dept> list = (List<Dept>) deptDao.findAll(sort);
        for (Dept dept : list) {
            System.out.println(dept);
        }
    }

    @Test
    public void testPagingAndSortingRepositoryPaging() {
        //Pageable:封装了分页的参数,当前页,煤业显示的条数。注意:它的当前页是从0开始
        //PageRequest(page,size):page表示当前页,size表示每页显示多少条
        Pageable pageable = PageRequest.of(1, 2);
        Page<Dept> page = deptDao.findAll(pageable);
        System.out.println("数据的总条数:" + page.getTotalElements());
        System.out.println("总页数:" + page.getTotalPages());
        List<Dept> list = page.getContent();
        for (Dept dept : list) {
            System.out.println(dept);
        }
    }

    @Test
    public void testPagingAndSortingRepositorySortAndPaging() {
        Sort sort = Sort.by(Sort.Direction.DESC, "deptno");
        Pageable pageable = PageRequest.of(0, 2, sort);
        Page<Dept> page = deptDao.findAll(pageable);
        System.out.println("数据的总条数:" + page.getTotalElements());
        System.out.println("总页数:" + page.getTotalPages());
        List<Dept> list = page.getContent();
        for (Dept dept : list) {
            System.out.println(dept);
        }
    }
}

你可能感兴趣的:(#,SpringBoot,#,SpringData)