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日

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

BigNiu 

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值