Mybatis-Plus for pagination condition query

Table of contents

1. Add dependencies

2. Configure the paging interceptor

3. Conditional query (MVC architecture example)

(2) Data transfer object DTO

(3) Mapper interface

(4)Mapper.xml

(5) Service layer

(6) Controller layer

(7) SQL creation

4. Example results

(1) Take the result detected when the price is 3000.0 as an example

 (2) When no conditions are added, all queries are defaulted


1. Add dependencies

        <!-- MyBatis-Plus -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.3.3</version>
        </dependency>

Note: The version is preferably 3.4 or higher 

2. Configure the paging interceptor

import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MybatisPlusConfig {

    /**
     * 分页插件
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();

        // 分页拦截器
        PaginationInnerInterceptor innerInterceptor = new PaginationInnerInterceptor();
        // 设置请求页数大于最大页时的操作:true 调回到首页,false 继续请求,默认为 false
        innerInterceptor.setOverflow(false);
        // 设置单页的限制数量,-1 表示不限制
        innerInterceptor.setMaxLimit(500L);

        interceptor.addInnerInterceptor(innerInterceptor);
        return interceptor;
    }

}

3. Conditional query (MVC architecture example)

(1) Entity class Product

import com.baomidou.mybatisplus.annotation.TableField;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;

import java.io.Serializable;

@AllArgsConstructor
@NoArgsConstructor
public class Product implements Serializable {


    private Integer pid;

    private String pname;

    private Double price;

    @TableField(value = "category_id")
    private String categoryId;

    private static final long serialVersionUID = 1L;

    public Integer getPid() {
        return pid;
    }

    public void setPid(Integer pid) {
        this.pid = pid;
    }

    public String getPname() {
        return pname;
    }

    public void setPname(String pname) {
        this.pname = pname == null ? null : pname.trim();
    }

    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }

    public String getCategoryId() {
        return categoryId;
    }

    public void setCategoryId(String categoryId) {
        this.categoryId = categoryId == null ? null : categoryId.trim();
    }
}

(2) Data transfer object DTO

        DTO can be understood as a collection of attributes used to encapsulate conditional queries

        Here, the following three attributes are used as conditions to query


import lombok.Data;

@Data
public class ProductDTO {

    private Integer pid;

    private String pname;

    private Double price;

}

(3) Mapper interface

Note that you must inherit the BaseMapper interface provided by Mybatis-plus

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zhan.springboot.springbootmybatis.pojo.DTO.ProductDTO;
import com.zhan.springboot.springbootmybatis.pojo.Product;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;


@Mapper
public interface ProductMapper extends BaseMapper<Product> {
    IPage<Product> selectProduct(IPage<Product> page, @Param("productDTO")ProductDTO productDTO);
}

(4)Mapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zhan.springboot.springbootmybatis.mapper.ProductMapper">

  <select id="selectProduct" resultType="com.zhan.springboot.springbootmybatis.pojo.Product">
      select *
      from springboot.product
      <where>
        <if test="productDTO.pid != null and productDTO.pid != ''">pid=#{productDTO.pid}</if>
        <if test="productDTO.pname != null and productDTO.pname != ''">And pname=#{productDTO.pname}</if>
        <if test="productDTO.price != null and productDTO.price != ''">And price=#{productDTO.price}</if>
      </where>
  </select>

The conditional query here can be customized, such as adding fuzzy query 

(5) Service layer

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zhan.springboot.springbootmybatis.pojo.DTO.ProductDTO;
import com.zhan.springboot.springbootmybatis.pojo.Product;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

@Mapper
public interface ProductService {

    IPage<Product> selectProduct(int pageNum, int pageSize, @Param("productDTO")ProductDTO productDTO);
}
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhan.springboot.springbootmybatis.mapper.ProductMapper;
import com.zhan.springboot.springbootmybatis.pojo.DTO.ProductDTO;
import com.zhan.springboot.springbootmybatis.pojo.Product;
import com.zhan.springboot.springbootmybatis.service.ProductService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper productMapper;


    @Override
    public IPage<Product> selectProduct(int pageNum, int pageSize, @Param("productDTO")ProductDTO productDTO) {
        Page<Product> page = new Page<>(pageNum, pageSize);
        // 非空判断
        if (productDTO == null) {
            productDTO = new ProductDTO();
        }
        return productMapper.selectProduct(page,productDTO);
    }
}

(6) Controller layer

import com.zhan.springboot.springbootmybatis.Common.AjaxResult;
import com.zhan.springboot.springbootmybatis.pojo.DTO.ProductDTO;
import com.zhan.springboot.springbootmybatis.pojo.Product;
import com.zhan.springboot.springbootmybatis.service.ProductService;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@ResponseBody
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private ProductService productService;

    @PostMapping
    public AjaxResult selectProduct(@RequestParam(defaultValue = "1")int pageNum, @RequestParam(defaultValue = "5")int pageSize, @RequestBody(required = false)ProductDTO productDTO){

        return AjaxResult.success(productService.selectProduct(pageNum,pageSize,productDTO));

    }

}

Note that the AjaxResult here is a unified package to return the result set, which can be customized, or you can refer to my previous blog. 

(7) SQL creation

create table product
(
    pid         int auto_increment
        primary key,
    pname       varchar(20) not null,
    price       double      null,
    category_id varchar(20) null
);

Custom add data here

4. Example results

(1) Take the result detected when the price is 3000.0 as an example

 (2) When no conditions are added, all queries are defaulted

Guess you like

Origin blog.csdn.net/Kristabo/article/details/131857079