直接上代码吧
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降序分页