SpringBoot JPA实践之@Query查询之分页实现
JPA 
                                    admin 发布于:2020-05-01 22:12:00
阅读:loading
@Query注解提供了对分页的支持,通常的分页需要两个SQL,一个是查询count语句,一个是查询data语句,使用起来比较简单,直接最后一个参数传递pageable类型参数即可,至于是否使用可自行斟酌。
网络上有些资料里所述的使用特殊表达式“ORDER BY ?#{#pageable}”来实现分页,这种方式我在Oracle中也用过一次,印象中这种方式实现的分页查询在翻页至最后一页时,系统内部会打印输出一个错误信息的Log,不影响实际运行,本文中采用现时间的最新版本,下面为对应的具体实现。
/**
  * 查询分页:User类型为Entity对象
  *
  * @author chendd
  * @date 2020/5/2 21:22
  */
 public interface UserQueryPageRepository extends BaseRepository<User, String> {
 
     @Query(countQuery = "select count(*) from user" ,
            value = "select a.* from user a where 1=1" ,
            nativeQuery = true)
     Page<User> queryUsersPage(Pageable pageable);
 
 }/**
  * 查询分页
  *
  * @author chendd
  * @date 2020/5/2 21:28
  */
 public class UserQueryPageTest extends ApplicationTest {
 
     @Resource
     private UserQueryPageRepository repository;
 
     @Test
     public void testQueryPage() {
         Pageable pageable = PageRequest.of(0 , 5 , Sort.by(Sort.Order.asc("name")));
         Page<User> page = repository.queryUsersPage(pageable);
         List<User> userList = page.getContent();
         Assert.assertNotNull(userList);
     }
 
 }
跟实现二的区别是,传递pageable类型后,仍然无需考虑分页信息,且支持order by排序。

对象查询分页,包含排序,查询条件的种类列举有between and区间、等于、不等于、like模糊、大于等于、小于等于、and复杂条件 (aa or bb)等场景,参考如下代码:

注
上述许多内容已经过时和过期了,留存本篇文章仅为方便个人查看,原始文章的信息参考:
点赞
                 
            
                 
            
                 
            
                 
            
                 
            
发表评论
        
        当前回复:作者
        
    
    
    评论列表
留言区
- SpringBoot JPA实践之框架搭建
- SpringBoot JPA实践之BaseRepository
- SpringBoot JPA实践之自动生成Entity
- SpringBoot JPA实践之API介绍
- SpringBoot JPA实践之方法属性名查询
- SpringBoot JPA实践之Example对象查询
- SpringBoot JPA实践之Named查询
- SpringBoot JPA实践之@Query查询之参数传参方式
- SpringBoot JPA实践之@Query查询之动态查询条件
- SpringBoot JPA实践之@Query查询之接口结果集
- SpringBoot JPA实践之EntityManage查询返回自定义DTO
- SpringBoot JPA实践之EntityManage查询返回自定义DTO的代理实现
- SpringBoot JPA实践之Specification查询分页
- SpringBoot JPA实践之表达式自定义属性与方法
- SpringBoot JPA实践之小技巧汇总
 
                                     
                                     
                                     
                                     
                                     
                                     
                    