一、前言

    之前有写过一个Vue前端分页的文章,但是如果数据量大,就需要用后端分页来做了,那就使用Mybatis-Plus写一个后端查询的例子。

二、准备工作

    建库,建表。表结构如下:

三、开始开发

    创建一个SpringBoot的项目,如果不会创建,请参考这个的连接:https://blog.csdn.net/weixin_40877388/article/details/98877309

 3.1pom文件终引入jar包

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.19</version>
</dependency>
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.3.2</version>
</dependency>
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.16.10</version>
</dependency>

3.2开始开发

实体类

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import lombok.Data;

@TableName("student")
@Data
public class Student extends Model<Student> {
    @TableId(value = "s_id", type = IdType.AUTO)
    private Integer sId;
    private String sNumber;
    private String sName;
    private String sAge;
}

mybatis-plus分页配置

@Configuration
public class MybatisPlusConfig {

    /**
     * 分页插件
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
         PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        // 设置请求的页面大于最大页后操作, true调回到首页,false 继续请求  默认false
        // paginationInterceptor.setOverflow(false);
        // 设置最大单页限制数量,默认 500 条,-1 不受限制
        // paginationInterceptor.setLimit(500);
        // 开启 count 的 join 优化,只针对部分 left join
        //paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true));
        return paginationInterceptor;
    }

}

controller

    @RequestMapping("/getListByPage")
    @ResponseBody
    public IPage<Student> getListByPage(StudentDTO studentDTO){
        //限制一下pageNum和pageSize不能为空,代码省略
        //实例化分页工具
        IPage<Student> page = new Page<>(studentDTO.getPageNum(), studentDTO.getPageSize());
        //实例化条件构造器
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        if(ObjectUtil.isNotNull(studentDTO.getSName())){
            queryWrapper.like("s_name",studentDTO.getSName());
        }
        if(ObjectUtil.isNotNull(studentDTO.getSNumber())){
            queryWrapper.like("s_number",studentDTO.getSNumber());
        }
        if(ObjectUtil.isNotNull(studentDTO.getSAge())){
            queryWrapper.like("s_age",studentDTO.getSAge());
        }
        //调用page方法
        IPage<Student> pageList = iStudentService.page(page, queryWrapper);
        return pageList;
    }
  

StudentDTO是做什么的呢?接收一些分页条件和查询条件

import lombok.Data;

@Data
public class StudentDTO {
    private String sNumber;
    private String sName;
    private String sAge;
    /**
     * 分页页码
     */
    private Integer pageNum;
    /**
     * 分页页面大小
     */
    private Integer pageSize;
}

3.3postMan测试

结果:

{
    "records": [//集合数据
        {
            "sname": "哈哈",
            "snumber": "202000001",
            "sage": "9",
            "sid": 1
        },
        {
            "sname": "嘿嘿",
            "snumber": "202000002",
            "sage": "9",
            "sid": 2
        },
        {
            "sname": "呵呵",
            "snumber": "202000003",
            "sage": "7",
            "sid": 3
        },
        {
            "sname": "吼吼",
            "snumber": "202000004",
            "sage": "8",
            "sid": 4
        },
        {
            "sname": "嘎嘎",
            "snumber": "202000004",
            "sage": "9",
            "sid": 5
        }
    ],
    "total": 7,  //总数
    "size": 5,  //当前查询出数据的数量大小
    "current": 1,  //当前页数
    "orders": [],
    "optimizeCountSql": true,
    "hitCount": false,
    "searchCount": true,
    "pages": 2  //一共可以分几页
}

postMan中的参数还可以加StudentDTO的其他字段作为查询条件。

Logo

前往低代码交流专区

更多推荐