Springboot+hibernate+jpa的分页,最简单分页

直接上代码吧

  public Page<T> findAll(@NotNull Pageable pageable) {
        return getBaseDAO().findAll(pageable);
    }

    public <S extends T> Page<S> findAll(@NotNull Example<S> example, @NotNull Pageable pageble) {
        return getBaseDAO().findAll(example, pageble);
    }

 

service

package com.oneinlet.service;

import com.oneinlet.dao.HistoryCustomerDao;
import com.oneinlet.entity.HistoryCustomer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Service;

import javax.validation.constraints.NotNull;

@Service
public class HistoryCustomerService extends BaseService<HistoryCustomer,Integer> {
    private final HistoryCustomerDao historyCustomerDao;

    @Autowired
    public HistoryCustomerService(HistoryCustomerDao historyCustomerDao) {
        this.historyCustomerDao = historyCustomerDao;
    }

    @Override
    protected JpaRepository<HistoryCustomer, Integer> getBaseDAO() {
        return historyCustomerDao;
    }

   /* public Page<HistoryCustomer> findAll(@NotNull Pageable pageable) {
        return getBaseDAO().findAll(pageable);
    }*/



}

contorller

package com.oneinlet.controller;

import com.oneinlet.entity.HistoryCustomer;
import com.oneinlet.entity.HotelGateway;
import com.oneinlet.service.HistoryCustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;



@RestController
@RequestMapping("/customer")
public class HistoryCustomerController extends BaseController{
    @Autowired
    private HistoryCustomerService historyCustomerService;

    @RequestMapping("/selectCustomer")
    public Object selectCustomer(){
       return setOKResult(historyCustomerService.findAll());

}
    @RequestMapping(value = "/getPagCustomer",method = RequestMethod.GET)
    public Object getPagCustomer(@RequestParam("pag") int pag,
                                 @RequestParam("size") int size,@RequestParam("roomnu") Integer roomnu){
        Sort sort = new Sort(Sort.Direction.DESC,"logID");
        Pageable pageable = new PageRequest(pag-1,size,sort);
        HistoryCustomer historyCustomer=new HistoryCustomer();
        if(roomnu==0){
            return setOKResult(historyCustomerService.findAll(pageable));
        }
        historyCustomer.setRoomNumber(roomnu);
        return setOKResult(historyCustomerService.findAll(Example.of(historyCustomer),pageable));
        }

}

 

 最简单的一个根据id降序分页

posted @   码海兴辰  阅读(27)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
点击右上角即可分享
微信分享提示