dao层
接口TypeRepository 继承 JpaRepository
@Repository public interface TypeRepository extends JpaRepository<Type,Long> { }
service层
-PagelistType(Pageable pageable);
public interface TypeService { Type saveType(Type type); Type getType(Long id); Page<Type>listType(Pageable pageable);//分页查询,返回Page对象; Type updateType(Long id,Type type); Type getTypeByName(String name); List<Type>listType(); List<Type>listTypeTop(Integer size); void deleteType(Long id); }
使用typeRepository.findAll()方法,pageable作为参数
@Transactional//事务 @Override public Page<Type> listType(Pageable pageable) { return typeRepository.findAll(pageable);//分页查询 }
controller层
@Autowired private TypeService typeService; //Pageable,springboot实现分页的方法;@PageableDefault默认指定参数,size:每页默认的条数,sort:排序 @GetMapping("/types") public String types(@PageableDefault(size = 3,sort = {"id"},direction = Sort.Direction.DESC) Pageable pageable, Model model){ model.addAttribute("page",typeService.listType(pageable));//把查到的pageable,返回给前端。 return "/admin/types"; }