最近在使用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));
}
}