码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2020-07-14 16:41:39      阅读:79      评论:0      收藏:0      [点我收藏+]

标签:查询条件   from   dom   int   信息   path   ace   Pageable   data   

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 的分页以及条件查询

标签:查询条件   from   dom   int   信息   path   ace   Pageable   data   

原文地址:https://www.cnblogs.com/wyf-love-dch/p/13299520.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!