注解@Query进行自动分页查询


直接传自带的实体类Pageable,并设置参数即可,JPA会自动识别并解析分页SQL,如此来看,JPA还是考虑得挺周全的

参数直接传Pageable

@Query("SELECT DISTINCT c FROM Customer c JOIN c.partyCerts p where p.certNum=?")
	public List<Customer> findCustomerListByNum(String certNum,Pageable pageable);

分页并自定义反参

@Query("select new ResultInfo(r.displayName,r.result.id,r.result.createdDate,r.result.createdBy.displayName) from ResultInfo r where r.result.id not in ?1 and r.secUser.id<>?2 and r.createdBy.id<>?3 and r.creator=?4")
    Page<ResultInfo> findToBeSharedResultInfoeListTwo(List<String>beSharedIdList,
                                                      String secUserId,
                                                      String createdById,
                                                      Boolean creator,
                                                      Pageable pageable);
public ResultInfo(String displayName,String resultId,Date resultCreateDate,String resultCreatedByDisplayName) {
		this.setDisplayName(displayName);
		Result result = new Result();
		result.setCreatedDate(resultCreateDate);
		SecUser secUser = new SecUser();
		secUser.setDisplayName(resultCreatedByDisplayName);
		result.setCreatedBy(secUser);
		result.setId(resultId);
		this.result=result;
	}

在这里插入图片描述
当反参类型为Page类型时,@Query中有个属性是countQuery,此属性的作用是定义查询总数的sql,如果不使用此属性,总数sql由框架自己生成

Paging query needs to have a Pageable parameter!

如下:当接口参数类型用Page时,入参必须有Pageable类型参数

@Query("select new ResultInfo(r.displayName,r.result.id,r.result.createdDate,r.result.createdBy.displayName) from ResultInfo r where r.secUser.id<>?1 and r.createdBy.id<>?2 and r.creator=?3")
    Page<ResultInfo> findToBeSharedResultInfoeList(String secUserId,
                                                   String createdById,
                                                   Boolean creator);

Cannot use native queries with dynamic sorting and/or pagination in method

如下:当使用原生本地sql时,不能通过传Pageable参数实现分页,必须通过sql方式方式分页,例如mysql使用limit

@Query(value = "select r.* from VBAP3_RESULT_INFO r " +
            "left join VBAP3_SEC_USER secUser on r.sec_user_id=secUser.id " +
            "left join VBAP3_SEC_USER creatUser on r.created_by_id=creatUser.id " +
            "where secUser.id<>?1 and creatUser.id<>?2 and r.creator=?3",nativeQuery = true)
    List<ResultInfo> testOne(String secUserId,String createdById,Boolean creator,Pageable pageable);


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM