欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring Data JPA 分页

程序员文章站 2022-04-25 11:45:53
...

最近在使用Spring Boot 做项目时遇到了关于Spring Data JPA的分页问题,在下面这位大兄弟的博客中有代码实现

LINK: 博客链接

DAO接口:

package com.demo.sell.repository;

import com.demo.sell.entity.OrderMaster;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;

public interface OrderMasterRepository extends JpaRepository<OrderMaster,String> {
    Page<OrderMaster> findByBuyerOpenid(String buyerOpenid,Pageable pageable);
}

 

单元测试:

package com.demo.sell.repository;

import com.demo.sell.entity.OrderMaster;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Arrays;
import java.util.List;

import static org.junit.Assert.*;

@SpringBootTest
@RunWith(SpringRunner.class)
public class OrderMasterRepositoryTest {
    @Autowired
    private OrderMasterRepository orderMasterRepository;
    @Test
    public void findByBuyerOpenid() {
        Page<OrderMaster> byBuyerOpenid = orderMasterRepository.findByBuyerOpenid("001", new PageRequest(0, 5));
        List<OrderMaster> content = byBuyerOpenid.getContent();
        System.out.println(Arrays.asList(content));
    }
}