spring data jpa分页查询示例代码
程序员文章站
2024-03-04 23:03:00
最近项目上用就hibernate+spring data jpa,一开始感觉还不错,但是随着对业务的复杂,要求处理一些复杂的sql,就顺便研究了下,把结果记录下,以便日后查...
最近项目上用就hibernate+spring data jpa,一开始感觉还不错,但是随着对业务的复杂,要求处理一些复杂的sql,就顺便研究了下,把结果记录下,以便日后查看。用到specification,需要继承jpaspecificationexecutor接口。(下面代码有的分页从0开始作为第一页,有的从1开始作为作为第一页,我也忘记,请自己测试)
dao层:
import java.util.list; import org.springframework.data.domain.page; import org.springframework.data.domain.pageable; import org.springframework.data.jpa.repository.jparepository; import org.springframework.data.jpa.repository.jpaspecificationexecutor; import org.springframework.data.jpa.repository.modifying; import org.springframework.data.jpa.repository.query; import org.springframework.stereotype.repository; @repository public interface camerainforepo extends jparepository<camerainfopo, string>, jpaspecificationexecutor<camerainfopo> { @query("select c from camerainfopo c where c.deviceinfo.id = ?1") public list<camerainfopo> findcamerasbydeviceid(string listdeviceinfoid); //更新之后不清空缓存,在一个事务里查询到旧数据(hibernate) @modifying @query(value = "update camerainfopo c set c.isenabled = 1 where c.id = ?1") public void updateenabled(string cameraid); //更新之后清空缓存,不保留旧对象(hibernate) @modifying(clearautomatically = true) @query(value = "update camerainfopo c set c.isenabled = 0 where c.id = ?1") public void updateunenabled(string cameraid); //带条件的分页查询 public page<camerainfopo> findbyisenabled(integer isenabled, pageable pageable); }
dao实现层
import java.util.arraylist; import java.util.list; import javax.annotation.resource; import javax.persistence.entitymanager; import javax.persistence.persistencecontext; import javax.persistence.typedquery; import javax.persistence.criteria.criteriabuilder; import javax.persistence.criteria.criteriaquery; import javax.persistence.criteria.predicate; import javax.persistence.criteria.root; import org.springframework.data.domain.page; import org.springframework.data.domain.pageimpl; import org.springframework.data.domain.pageable; import org.springframework.data.jpa.domain.specification; import org.springframework.stereotype.repository; @repository public class camerainforepoimpl { @persistencecontext private entitymanager em; @resource private camerainforepo camerainforepo; public page<camerainfopo> findcamerainfobypage(pageable pageable, integer isenabled) { //criteriabuilder,用来构建critiaquery的构建器对象 criteriabuilder criteriabuilder = em.getcriteriabuilder(); //criteriaquery,它包含着查询语句的条件各个部分,比如:select 、from、where、group by、order by等 criteriaquery<camerainfopo> criteriaquery = criteriabuilder.createquery(camerainfopo.class); //查询根,用于获取查询实例的属性,通过criteriaquery的from方法获取 root<camerainfopo> rootfrom = criteriaquery.from(camerainfopo.class); //查询条件 list<predicate> predicates = new arraylist<predicate>(); if (null != isenabled) { predicate predicate = criteriabuilder.equal(rootfrom.get("isenabled").as(integer.class), isenabled); predicates.add(predicate); } //格式化参数 criteriaquery.where(criteriabuilder.and(predicates.toarray(new predicate[predicates.size()]))); //默认按照id排序(从小到大) criteriaquery.orderby(criteriabuilder.asc(rootfrom.get("id"))); //sql查询对象 typedquery<camerainfopo> createquery = em.createquery(criteriaquery); //分页参数 integer pagesize = pageable.getpagesize(); integer pageno = pageable.getpagenumber(); //计数查询结果条数 typedquery<camerainfopo> createcountquery = em.createquery(criteriaquery); // 实际查询返回分页对象 int startindex = pagesize * pageno; createquery.setfirstresult(startindex); createquery.setmaxresults(pageable.getpagesize()); page<camerainfopo> pagerst = new pageimpl<camerainfopo>(createquery.getresultlist(), pageable, createcountquery.getresultlist().size()); return pagerst; } //制造查询条件结果(建议存放map) private specification<camerainfopo> getwhereclause(final integer isenabled) { return new specification<camerainfopo>() { public predicate topredicate(root<camerainfopo> r, criteriaquery<?> q, criteriabuilder cb) { predicate predicate = cb.conjunction(); if (null != isenabled) { predicate = cb.equal(r.get("isenabled").as(integer.class), isenabled); } return predicate; } }; } //单表根据查询条件的分页 public page<camerainfopo> findcamerainfobypageforjpa(pageable pageable, integer isenabled) { specification<camerainfopo> spec = getwhereclause(isenabled); page<camerainfopo> pagerst = camerainforepo.findall(spec, pageable); return pagerst; } }
还有另外一种就更简单了,如果只是根据表里面的一个或者几个字段,可以直接写jpa实现,不用复杂
pageable pageable = new pagerequest(1, 1); page<camerainfopo> page = camerainforepo.findbyisenabled(1, pageable);
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。