spring data jpa分页查询示例代码
最近项目上用就hibernate+springdatajpa,一开始感觉还不错,但是随着对业务的复杂,要求处理一些复杂的sql,就顺便研究了下,把结果记录下,以便日后查看。用到Specification,需要继承JpaSpecificationExecutor接口。(下面代码有的分页从0开始作为第一页,有的从1开始作为作为第一页,我也忘记,请自己测试)
DAO层:
importjava.util.List; importorg.springframework.data.domain.Page; importorg.springframework.data.domain.Pageable; importorg.springframework.data.jpa.repository.JpaRepository; importorg.springframework.data.jpa.repository.JpaSpecificationExecutor; importorg.springframework.data.jpa.repository.Modifying; importorg.springframework.data.jpa.repository.Query; importorg.springframework.stereotype.Repository; @Repository publicinterfaceCameraInfoRepoextendsJpaRepository,JpaSpecificationExecutor { @Query("selectcfromCameraInfoPOcwherec.deviceInfo.id=?1") publicList findCamerasByDeviceId(StringlistDeviceInfoId); //更新之后不清空缓存,在一个事务里查询到旧数据(hibernate) @Modifying @Query(value="updateCameraInfoPOcsetc.isEnabled=1wherec.id=?1") publicvoidupdateEnabled(StringcameraId); //更新之后清空缓存,不保留旧对象(hibernate) @Modifying(clearAutomatically=true) @Query(value="updateCameraInfoPOcsetc.isEnabled=0wherec.id=?1") publicvoidupdateUnEnabled(StringcameraId); //带条件的分页查询 publicPage findByIsEnabled(IntegerisEnabled,Pageablepageable); }
DAO实现层
importjava.util.ArrayList; importjava.util.List; importjavax.annotation.Resource; importjavax.persistence.EntityManager; importjavax.persistence.PersistenceContext; importjavax.persistence.TypedQuery; importjavax.persistence.criteria.CriteriaBuilder; importjavax.persistence.criteria.CriteriaQuery; importjavax.persistence.criteria.Predicate; importjavax.persistence.criteria.Root; importorg.springframework.data.domain.Page; importorg.springframework.data.domain.PageImpl; importorg.springframework.data.domain.Pageable; importorg.springframework.data.jpa.domain.Specification; importorg.springframework.stereotype.Repository; @Repository publicclassCameraInfoRepoImpl{ @PersistenceContext privateEntityManagerem; @Resource privateCameraInfoRepocameraInfoRepo; publicPagefindCameraInfoByPage(Pageablepageable,IntegerisEnabled){ //CriteriaBuilder,用来构建CritiaQuery的构建器对象 CriteriaBuildercriteriaBuilder=em.getCriteriaBuilder(); //CriteriaQuery,它包含着查询语句的条件各个部分,比如:select、from、where、groupby、orderby等 CriteriaQuery criteriaQuery=criteriaBuilder.createQuery(CameraInfoPO.class); //查询根,用于获取查询实例的属性,通过CriteriaQuery的from方法获取 Root rootFrom=criteriaQuery.from(CameraInfoPO.class); //查询条件 List predicates=newArrayList (); if(null!=isEnabled){ Predicatepredicate=criteriaBuilder.equal(rootFrom.get("isEnabled").as(Integer.class),isEnabled); predicates.add(predicate); } //格式化参数 criteriaQuery.where(criteriaBuilder.and(predicates.toArray(newPredicate[predicates.size()]))); //默认按照id排序(从小到大) criteriaQuery.orderBy(criteriaBuilder.asc(rootFrom.get("id"))); //SQL查询对象 TypedQuery createQuery=em.createQuery(criteriaQuery); //分页参数 IntegerpageSize=pageable.getPageSize(); IntegerpageNo=pageable.getPageNumber(); //计数查询结果条数 TypedQuery createCountQuery=em.createQuery(criteriaQuery); //实际查询返回分页对象 intstartIndex=pageSize*pageNo; createQuery.setFirstResult(startIndex); createQuery.setMaxResults(pageable.getPageSize()); Page pageRst= newPageImpl (createQuery.getResultList(),pageable,createCountQuery.getResultList().size()); returnpageRst; } //制造查询条件结果(建议存放map) privateSpecification getWhereClause(finalIntegerisEnabled){ returnnewSpecification (){ publicPredicatetoPredicate(Root r,CriteriaQuery>q,CriteriaBuildercb){ Predicatepredicate=cb.conjunction(); if(null!=isEnabled){ predicate=cb.equal(r.get("isEnabled").as(Integer.class),isEnabled); } returnpredicate; } }; } //单表根据查询条件的分页 publicPage findCameraInfoByPageForJpa(Pageablepageable,IntegerisEnabled){ Specification spec=getWhereClause(isEnabled); Page pageRst=cameraInfoRepo.findAll(spec,pageable); returnpageRst; } }
还有另外一种就更简单了,如果只是根据表里面的一个或者几个字段,可以直接写jpa实现,不用复杂
Pageablepageable=newPageRequest(1,1); Pagepage=cameraInfoRepo.findByIsEnabled(1,pageable);
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。