利用JpaRepository的方法和Service进行组合实现带条件的分页查询

创建Repository

创建一个RoomRepository继承JpaRepository用来实现方法查询,定义一个findAllByBuildingidEqualsAndFloorEquals方法,来根据楼栋id和楼层id进行条件查询,参数再传入一个Pageable用来实现分页。

package com.kaygb.hgnuman.repository;

import com.kaygb.hgnuman.entity.Room;
import org.springframework.boot.autoconfigure.data.web.SpringDataWebProperties;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * kaygb
 *
 * @InterFaceName: RoomRepository
 * @Description: 增删改查
 * @Author: kaygb
 * @Date: 2021/5/10 12:12
 * @Blog: https://blog.kaygb.com/
 */
@Repository
public interface RoomRepository extends JpaRepository<Room,Integer> {

    List<Room> findAllByBuildingidEqualsAndFloorEquals(Integer bid, Integer fid, Pageable pageable);
}

创建Service

创建一个RoomService接口,用来对findAllByBuildingidEqualsAndFloorEquals方法查出来的数据进行处理

/**
 * @InterfaceName: RoomService
 * @Description:
 * @Author: kaygb
 * @Date: 2021/07/2021/7/25 15:39
 * @Blog: https://blog.kaygb.com/
 */


public interface RoomService {
   List<Room> getPageByBidAndFid(int pageNo, int pageSize, Integer bdid, Integer floor);
   List<Room> getPage(int pageNo, int pageSize);
}

Alt+Enter一下,实现此接口

/*
 * Copyright (c) 2021.  Kaygb
 * All rights reserved.
 * Site: https://www.kaygb.com/
 * Github: https://github.com/kaygb/
 */

package com.kaygb.hgnuman.service.impl;

import com.kaygb.hgnuman.entity.Building;
import com.kaygb.hgnuman.entity.Room;
import com.kaygb.hgnuman.repository.BuildingRepository;
import com.kaygb.hgnuman.repository.RoomRepository;
import com.kaygb.hgnuman.service.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName: RoomServiceImpl
 * @Description:
 * @Author: kaygb
 * @Date: 2021/07/2021/7/25 15:40
 * @Blog: https://blog.kaygb.com/
 */

@Service("RoomService")
public class RoomServiceImpl implements RoomService {
   @Autowired
   RoomRepository roomRepository;
   @Override
   public List<Room> getPageByBidAndFid(int pageNo, int pageSize, Integer bdid, Integer floor) {

         Pageable pageable =  PageRequest.of(pageNo - 1,pageSize); // pageable查询条件
         // 通过findAll查询pageable条件,并将Page中content的内容返回给ulist
         return roomRepository.findAllByBuildingidEqualsAndFloorEquals(bdid,floor,pageable); // 返回list

   }

   @Override
   public List<Room> getPage(int pageNo, int pageSize) {
      Pageable pageable =  PageRequest.of(pageNo - 1,pageSize); // pageable查询条件
      // 通过findAll查询pageable条件,并将Page中content的内容返回给ulist
      return roomRepository.findAll(pageable).getContent(); // 返回list
   }
}

其中一个是带条件的分页查询,另一个则是不带条件的分页查询。

注意

  1. 在创建Repository的时候一定不要忘记了@Repository注解
  2. 在实现Service的ServiceImpl中不要忘记了使用@Service注解指定Service
微信公众号
最后修改:2021 年 07 月 25 日 08 : 51 PM
如果您觉得我的文章有帮助,请随意赞赏!