美文网首页
13.Mybatis分页查询

13.Mybatis分页查询

作者: 吕小凯 | 来源:发表于2020-05-04 14:19 被阅读0次

1.demo-core模块下添加依赖

        <!--分页依赖-->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.5</version>
        </dependency>

2.demo-admin模块下application.yml添加分页插件设置

#pagehelper
pagehelper:
  helper-dialect: mysql
  reasonable: true
  support-methods-arguments: true
  params: count=countSql

3.demo-admin编写分页工具类MybatisPageHelper

package com.lvxk.demo.admin.helper;

import com.demo.core.page.PageRequest;
import com.demo.core.page.PageResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import tk.mybatis.mapper.common.BaseMapper;

import java.util.List;

/**
 * MybatisPageHelper分页查询工具类
 * Description: <br/>
 * date: 2020/5/5 13:59<br/>
 *
 * @author lvxk<br />
 * @since JDK 1.8
 */
public class MybatisPageHelper {

    public static PageResult findPage(PageRequest pageRequest, BaseMapper<?> baseMapper){
        PageHelper.startPage(pageRequest.getPageNum(),pageRequest.getPageSize());
        List list = baseMapper.selectAll();
        PageInfo<?> pageInfo = new PageInfo<>(list);
        PageResult pageResult = new PageResult();
        pageResult.setContent(pageInfo.getList());
        pageResult.setPageNum(pageInfo.getPageNum());
        pageResult.setPageSize(pageInfo.getPageSize());
        pageResult.setTotalPages(pageInfo.getPageSize());
        pageResult.setTotalSize(pageInfo.getTotal());
        return pageResult;
    }
}

controller service写好相应代码
service

 /**
     * 分页查询
     * @return
     */
    PageResult findPage(PageRequest pageRequest);

impl

 @Override
    public PageResult findPage(PageRequest pageRequest) {
        return MybatisPageHelper.findPage(pageRequest,sysUserMapper);
    }

controller

@PostMapping("findpage")
    @ApiOperation(value = "分页查询")
    public PageResult findPage(@RequestBody PageRequest pageRequest){
        return sysUserService.findPage(pageRequest);
    }

swagger测试显示成功即可

相关文章

网友评论

      本文标题:13.Mybatis分页查询

      本文链接:https://www.haomeiwen.com/subject/vpdmghtx.html