SpringBoot使用Mybatis+pagehelper实现分页

1.添加相关依赖

首先,我们需要在 pom.xml 文件中添加分页插件依赖包。

  • pom.xml
    <dependency>
        <groupId>com.github.pagehelper</groupId>
        <artifactId>pagehelper-spring-boot-starter</artifactId>
        <version>1.2.5</version>
    </dependency>
    

2.在Service层使用

在进行需要分页的数据查询请调用,我这里使用的是mybatis-generater自动生成的Example进行数据查询

	//自动注入Dao层
	@Autowired(required = false)
    private UserMapper userMapper;

	/**
     * 分页获取用户列表
     *
     * @param page 页数
     * @param limit 每页数量
     * @return
     */
    @Override
    public List<User> findAll(int page, int limit) {
        UserExample example = new UserExample();
        //这里进行调用分页插件
        PageHelper.startPage(page,limit);
        
        List<User> userList = userMapper.selectByExample(example);
        if(EmptyUtils.isEmpty(userList))
        {
            throw new UserServiceException("暂无数据");
        }
        for(User user:userList)
        {
            user.setPassword("");
            user.setPayPassword("");
        }
        return userList;
    }

3.Controller层调用Service层

	//自动注入
	@Autowired
    private UserService userService;

    /**
     * 获取用户列表
     * @param page 页数
     * @param limit 每页数量
     * @return
     */
    @GetMapping("/list")
    public JsonResult list(@RequestParam("page") int page, @RequestParam("limit") int limit){
    	//获取用户数据列表
        List<User> userList = userService.findAll(page, limit,phone,userId,userName,startTime,endTime,grade);
        //获取用户总数
        int allUserCount = userService.findAllUserCount();
        //自己封装的一个分页返回对象
        return new JsonResultPage(userList,allUserCount);
    }

访问接口地址 localhost:8088/admin/api/user/list?page=1&limit=2
最终返回数据

	{
    "code": 0,
    "msg": "success",
    "data": [
        {
            "id": 1,
            "nickname": "l1234569",
            "username": "l1234569",
            "password": "",
            "payPassword": "",
            "accountAddress": null,
            "phone": "13800138000",
            "invitiCode": "2J36WK",
            "usdtMoney": 1000,
            "balanceMoney": 9.84,
            "grade": "v0",
            "teamNum": 1,
            "totalProfit": 0,
            "pid": 0,
            "pids": "0",
            "token": "f3177a4d025842dea1e0b9a26c4b4fe1",
            "status": 1,
            "time": "2020-04-14 18:19:39",
            "lastTime": "2020-04-18 19:56:21",
            "ip": "192.168.101.106"
        },
        {
            "id": 2,
            "nickname": "l1234570",
            "username": "l1234570",
            "password": "",
            "payPassword": "",
            "accountAddress": "123abc",
            "phone": "13800138000",
            "invitiCode": "QGD427",
            "usdtMoney": 1001,
            "balanceMoney": 7.4,
            "grade": "v0",
            "teamNum": 0,
            "totalProfit": 0,
            "pid": 1,
            "pids": "0,1",
            "token": "a244504712a9420c8656fc163acbab51",
            "status": 1,
            "time": "2020-04-14 18:27:22",
            "lastTime": "2020-04-18 14:33:39",
            "ip": "192.168.101.118"
        }
    ],
    "total": 9
}

成功返回分页数据

博主:Now大牛
QQ : 201309512
发布日期:2020年4月19日

发布了23 篇原创文章 · 获赞 24 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/qq_35445306/article/details/105616602