spring-boot分页类使用
Posted KoMiles 少壮不努力,长大搞IT。
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring-boot分页类使用相关的知识,希望对你有一定的参考价值。
引入分页类
pom.xml中
<dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.13</version> </dependency>
公共层 - 分页DTO
pageDTO.java
package com.example.pagehelperdemo.common; import java.util.List; import lombok.AllArgsConstructor; import lombok.Data; /** * @author komiles@163.com * @date 2020-04-30 18:54 */ @Data @AllArgsConstructor public class PageDTO<T> { private Long Total; private Integer pageSize; private Integer pageNum; private List<T> list; }
服务层 - 用户服务类
UserService.java
package com.example.pagehelperdemo.service; import com.example.pagehelperdemo.common.PageDTO; import com.example.pagehelperdemo.dao.User; /** * @author komiles@163.com * @date 2020-04-30 18:49 */ public interface UserService { User getOne(Integer id); PageDTO<User> getList(Integer pageNum, Integer pageSize); }
UserServiceImpl.java 实现类
package com.example.pagehelperdemo.service.impl; import com.example.pagehelperdemo.common.PageDTO; import com.example.pagehelperdemo.dao.User; import com.example.pagehelperdemo.dao.UserExample; import com.example.pagehelperdemo.mapper.UserMapper; import com.example.pagehelperdemo.service.UserService; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import java.util.List; import javax.annotation.Resource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; /** * @author komiles@163.com * @date 2020-04-30 18:50 */ @Service public class UserServiceImpl implements UserService { @Resource private UserMapper userMapper; @Override public User getOne(Integer id) { return userMapper.selectByPrimaryKey(id); } @Override public PageDTO<User> getList(Integer pageNum, Integer pageSize) { PageHelper.startPage(pageNum,pageSize); UserExample userExample = new UserExample(); userExample.createCriteria(); List<User> userList = userMapper.selectByExample(userExample); PageInfo pageInfo = new PageInfo(userList); PageDTO result = new PageDTO( pageInfo.getTotal(), pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getList()); return result; } }
用户层 - controller
UserController.java
package com.example.pagehelperdemo.controller; import com.example.pagehelperdemo.common.PageDTO; import com.example.pagehelperdemo.dao.User; import com.example.pagehelperdemo.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; /** * @author komiles@163.com * @date 2020-04-30 18:47 */ @RequestMapping("/user") @RestController public class UserController { @Autowired private UserService userService; @GetMapping("/one") public User getOne(@RequestParam("id") Integer id){ return userService.getOne(id); } @GetMapping("/list") public PageDTO<User> getList(@RequestParam(value = "page",defaultValue = "1") Integer page, @RequestParam(value = "limit",defaultValue = "10") Integer limit){ return userService.getList(page,limit); } }
启动文件
访问地址:http://127.0.0.1:6066/user/list?page=1&limit=10
以上是关于spring-boot分页类使用的主要内容,如果未能解决你的问题,请参考以下文章