天天看点

MybatisPlus实现分页查询数据库

1、在SpringBoot项目中写一个请求接口实现分页查询 接口代码:

/**
     * 获取参数列表,分页
     *
     * @param
     * @return
     * @throws Exception
     */
    @GetMapping("/v1/zdyl/lcoModel/get/list")
    public ResultJson lcoModelGetList(HttpServletRequest request) throws Exception {
        Map<String, Object> data = new HashMap<>();
        int pageIndex = 1;
        int pageSize = 9;
        if (request.getParameter("pageIndex") != null
                && request.getParameter("pageSize") != null
                && !request.getParameter("pageIndex").equals("")
                && !request.getParameter("pageSize").equals("")) {
            pageIndex = Integer.parseInt(request.getParameter("pageIndex"));
            pageSize = Integer.parseInt(request.getParameter("pageSize"));
        }
        //条件构造器
        QueryWrapper<LcoModel> lcoModelEntityWrapper = new QueryWrapper<>();
        lcoModelEntityWrapper.orderByDesc("create_time");
        if (request.getParameter("name") != null && !request.getParameter("name").equals(""))
            lcoModelEntityWrapper.like("name", request.getParameter("name"));
        Page<LcoModel> lcoModelPage = (Page<LcoModel>) ilCoModelService.page(new Page<LcoModel>(pageIndex, pageSize), lcoModelEntityWrapper);
        data.put("total", lcoModelPage.getTotal());
        data.put("data", lcoModelPage.getRecords());
        return ResultJson.ok("获取参数列表成功!", data);
    }
           

其中的传入参数类型是:HttpServletRequest,请求的url格式是:

http://localhost:8899/v1/zdyl/lcoModel/get/list?pageIndex=2&pageSize=3

(如果请求接口中传入的参数是@PathVariable,则接口请求格式直接是斜杠后面跟参数即可,例如:

http://localhost:8899/v1/zdyl/lcoModel/get/detail/1201c5e6-4b4b-4fb8-995f-b0677c681178)

2、语句:  @ConfigurationProperties(prefix = "spring.datasource")   的意思是从 

spring.datasource 

这个目录下面读取配置。

spring:
  jackson:
    time-zone: GMT+8
    date-format: yyyy-MM-dd HH:mm:ss
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource
    jdbc-url: jdbc:mysql://shujvku.mysql.rds.aliyuncs.com:3306/dm?serverTimezone=Asia/Shanghai&useSSL=false&allowPublicKeyRetrieval=true&characterEncoding=utf-8
    username: yonghuming
    password: mima
    filters: stat
    maxActive: 20
    initialSize: 1
    maxWait: 60000
    minIdle: 1
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 300000
    validationQuery: select 'x'
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    poolPreparedStatements: true
    maxOpenPreparedStatements: 20
           

3、写完上述的接口后,发现查询的时候没有实现分页,还是所有的数据都查出来了,检查后发现没有进行mybatis-plus分页插件的配置,添加上MyBaties-Plus配置类:

package com.zdyl.devicemanagement.config;


import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

@MapperScan("com.example.demo.**.mapper")
@Configuration
public class MybatisConfig {

    /**
     * 数据源配置
     *
     * @return
     */
    @Bean
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource dataSource() {
        return DataSourceBuilder.create().build();
    }

    /**
     * mybatis-plus分页插件<br>
     * 文档:http://mp.baomidou.com<br>
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }
}
           

其中:

@MapperScan("com.example.demo.**.mapper")
           

这一行可以代替Mapper类中的@Mapper注解。