springboot和mybatis-plus出现的分页问题

springboot和mybatis-plus出现的分页问题

1、问题描述

service层中

    public PageInfo<Employee> getAll(Employee employee,Integer pageNum, Integer pageSize) {
    
    
        QueryWrapper queryWrapper = new QueryWrapper<>(employee);
        IPage<Employee> iPage = super.queryPageList(queryWrapper, pageNum, pageSize);
//  employee中设置属性即可实现条件查询
        return new PageInfo<Employee>
                (Long.valueOf(iPage.getTotal()).intValue() , pageNum, pageSize, iPage.getRecords());
    }

service的父类中

    /**
     * 根据条件分页查询数据列表
     *
     * @param queryWrapper
     * @param page
     * @param rows
     * @return
     */
    public IPage<T> queryPageList(QueryWrapper<T> queryWrapper, Integer page,
                                  Integer rows) {
    
    
// 获取分页数据
        return this.mapper.selectPage(new Page<T>(page, rows), queryWrapper);
    }

但是在分页查找数据时,打印出的sql语句,一直没有输入limit这样的函数

SELECT id,name,gender,birthday,idcard,wedlock,nationid,nativeplace,politicid,email,phone,address,tiptopdegree,specialty,school,picture,username,password FROM employee

2、问题解决

  • 查找相关博客之后发现,项目中没有注入PaginationInterceptor插件,无法实现分页功能。

  • 加入相关配置后运行正确

    • 
      import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
      import org.springframework.context.annotation.Bean;
      import org.springframework.context.annotation.Configuration;
      
      @Configuration
      public class MybatisPlusConfig {
              
              
      
          @Bean
          public PaginationInterceptor paginationInterceptor() {
              
              
              return new PaginationInterceptor();
          }
      }
      
      

3、运行结果

==>  Preparing: SELECT COUNT(1) FROM employee 
==> Parameters: 
<==    Columns: COUNT(1)
<==        Row: 21
==>  Preparing: SELECT id,name,gender,birthday,idcard,wedlock,nationid,nativeplace,politicid,email,phone,address,tiptopdegree,specialty,school,picture,username,password FROM employee LIMIT ?,? 
==> Parameters: 0(Long), 10(Long)
<==    Columns: id, name, gender, birthday, idcard, wedlock, nationid, nativeplace, politicid, email, phone, address, tiptopdegree, specialty, school, picture, username, password
<==        Row: 1, 江南一点雨,, 1990-01-01, 610122199001011256, 已婚, 1, 陕西, 13, laowang@qq.com, 18565558897, 深圳市南山区, 本科, 信息管理与信息系统, 深圳大学, null, tim, 123
{
    
    
  "code": 101,
  "message": "",
  "data": {
    
    
    "total": 21,
    "pageNum": 1,
    "pageSize": 10,
    "records": [
      {
    
    
        "id": "1",
        "name": "江南一点雨",
        "gender": "男",
        "birthday": "1989-12-31T16:00:00.000+0000",
        "idcard": "610122199001011256",
        "wedlock": "已婚",
        "nationid": 1,
        "nativeplace": "陕西",
        "politicid": 13,
        "email": "[email protected]",
        "phone": "18565558897",
        "address": "深圳市南山区",
        "tiptopdegree": "本科",
        "specialty": "信息管理与信息系统",
        "school": "深圳大学",
        "picture": null,
        "username": "tim",
        "password": "123"
      },
      {
    
    

猜你喜欢

转载自blog.csdn.net/qq_45372719/article/details/112576302