spring data jpa 的分页以及条件查询

Posted 坠落凡尘的魔鬼

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring data jpa 的分页以及条件查询相关的知识,希望对你有一定的参考价值。

spring data jpa的分页以及条件查询:

1.分页是主要
  import org.springframework.data.domain.Page;
  import org.springframework.data.domain.PageRequest;
2.条件查询主要是:
  import org.springframework.data.domain.Sort;
   import org.springframework.data.jpa.domain.Specification;

直接写一个例子:
 NetWorkInfo netWorkInfo = new NetWorkInfo();
        netWorkInfo.setWdName(query);
        netWorkInfo.setWdCode(query);
        PageRequest pageable = PageRequest.of(page - 1, size, Sort.Direction.DESC, "id");  //分页信息
        //查询条件组装
        Specification<NetWorkInfo> spec = new Specification<NetWorkInfo>() {
            @Override
            public Predicate toPredicate(Root<NetWorkInfo> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Path<String> name = root.get("wdName");
                Predicate p1 = cb.like(name, "%" + netWorkInfo.getWdName() + "%");
                Path<String> code = root.get("wdCode");
                Predicate p2 = cb.like(code, "%" + netWorkInfo.getWdCode() + "%");
                List<Predicate> listAnd=new ArrayList<>(); //组装and语句
                listAnd.add(p1);
                listAnd.add(p2);
                Predicate predicateAnd = cb.and(listAnd.toArray(new Predicate[listAnd.size()])); //AND查询加入查询条件
                return query.where(predicateOr).getRestriction();
            }
        };
        Page<NetWorkInfo> pageList = netWorkInfoDao.findAll(spec, pageable);

 

另外一种可以这样写:
     NetWorkInfo netWorkInfo = new NetWorkInfo();
        netWorkInfo.setWdName(query);
        netWorkInfo.setWdCode(query);
        PageRequest pageable = PageRequest.of(page - 1, size, Sort.Direction.DESC, "id");  //分页信息
        //查询条件组装
        Specification<NetWorkInfo> spec = new Specification<NetWorkInfo>() {
            @Override
            public Predicate toPredicate(Root<NetWorkInfo> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Path<String> name = root.get("wdName");
                Predicate p1 = cb.like(name, "%" + netWorkInfo.getWdName() + "%");
                Path<String> code = root.get("wdCode");
                Predicate p2 = cb.like(code, "%" + netWorkInfo.getWdCode() + "%");
                return cb.and(p1,p2);
            }
        };
        Page<NetWorkInfo> pageList = netWorkInfoDao.findAll(spec, pageable);

 

dao层数据实现:

package com.example.react.dao;



import com.example.react.model.NetWorkInfo;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;


@Repository
public interface NetWorkInfoDao extends JpaRepository<NetWorkInfo,Long> {

    @Query(value = "select * from network_info where wd_name like  %?1%  order by id desc ", nativeQuery = true)
    List<NetWorkInfo> getModelByLikeQuery(String query);

    @Query(value = "select * from network_info where wd_name = ?1", nativeQuery = true)
    List<NetWorkInfo> getModelByName(String name);


    @Query(value = "select * from network_info where wd_name = ?1  and  id !=?2", nativeQuery = true)
    List<NetWorkInfo> getModelByNameAndNotId(String name,long id);

    @Query(value = "select * from network_info where id = ?1", nativeQuery = true)
    List<NetWorkInfo> getModelById(Integer id);

    @Query(value = "select * from network_info where id = ?1", nativeQuery = true)
    NetWorkInfo getModelId(Integer id);
    @Query(value = "select * from network_info order by id  ", nativeQuery = true)
    List<NetWorkInfo> getModelList();


    Page<NetWorkInfo> findAll(Specification<NetWorkInfo> spec, Pageable pageable);
}

 

注意:只是写了关键的数据



以上是关于spring data jpa 的分页以及条件查询的主要内容,如果未能解决你的问题,请参考以下文章

序列化表单为json对象,datagrid带额外参提交一次查询 后台用Spring data JPA 实现带条件的分页查询

Spring Data JPA 中的分页(限制和偏移)

在Spring Boot中使用Spring-data-jpa实现分页查询(转)

Spring Data 系列学习Spring Data JPA 自定义查询,分页,排序,条件查询

Spring Data 系列学习Spring Data JPA 自定义查询,分页,排序,条件查询

Spring Data JPA中的分页问题