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降序分页
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统