在spring中使用datajpa实现分页查询功能
在spring中使用 data jpa实现分页查询功能?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

10年积累的网站设计制作、网站设计经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先网站制作后付款的网站建设流程,更有登封免费网站建设让你可以放心的选择与我们合作。
最近项目上用就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, JpaSpecificationExecutor { @Query("select c from CameraInfoPO c where c.deviceInfo.id = ?1") public List 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 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 findCameraInfoByPage(Pageable pageable, Integer isEnabled) {
//CriteriaBuilder,用来构建CritiaQuery的构建器对象
CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
//CriteriaQuery,它包含着查询语句的条件各个部分,比如:select 、from、where、group by、order by等
CriteriaQuery criteriaQuery = criteriaBuilder.createQuery(CameraInfoPO.class);
//查询根,用于获取查询实例的属性,通过CriteriaQuery的from方法获取
Root rootFrom = criteriaQuery.from(CameraInfoPO.class);
//查询条件
List predicates = new ArrayList();
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 createQuery = em.createQuery(criteriaQuery);
//分页参数
Integer pageSize = pageable.getPageSize();
Integer pageNo = pageable.getPageNumber();
//计数查询结果条数
TypedQuery createCountQuery = em.createQuery(criteriaQuery);
// 实际查询返回分页对象
int startIndex = pageSize * pageNo;
createQuery.setFirstResult(startIndex);
createQuery.setMaxResults(pageable.getPageSize());
Page pageRst =
new PageImpl(createQuery.getResultList(), pageable, createCountQuery.getResultList().size());
return pageRst;
}
//制造查询条件结果(建议存放map)
private Specification getWhereClause(final Integer isEnabled) {
return new Specification() {
public Predicate toPredicate(Root 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 findCameraInfoByPageForJpa(Pageable pageable, Integer isEnabled) {
Specification spec = getWhereClause(isEnabled);
Page pageRst = cameraInfoRepo.findAll(spec, pageable);
return pageRst;
}
}
还有另外一种就更简单了,如果只是根据表里面的一个或者几个字段,可以直接写jpa实现,不用复杂
Pageable pageable = new PageRequest(1, 1); Pagepage = cameraInfoRepo.findByIsEnabled(1, pageable);
关于在spring中使用 data jpa实现分页查询功能问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联行业资讯频道了解更多相关知识。
当前题目:在spring中使用datajpa实现分页查询功能
本文地址:http://www.jxjierui.cn/article/jspchj.html


咨询
建站咨询
