Mybatis-Plus - 分页查询

目录

前言

记录下Mybatis-Plus的分页查询,Mybatis-Plus的整合在上一篇文章:SpringBoot - 整合Mybatis-Plus


环境

SpringBoot2.53 + Mybatis-Plus3.3.0


具体实现

  • 配置类
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MybatisPlusConfig {

    /**
     * 分页插件(PaginationInterceptor)
     * 不配置此项返回的是全部记录
     * @return
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }
}
  • ProductController.java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/product")
@RestController
public class ProductController {

    @Autowired
    private ProductService productService;

    /**
     * 查询Product分页
     * @param page
     * @param count
     * @return
     */
    @GetMapping("/page")
    public PageResponseVo<Product> selectProductPage(@RequestParam(required = false, defaultValue = "0") Integer page,
                                                     @RequestParam(required = false, defaultValue = "10") Integer count) {
        return productService.getProductPage(page, count);
    }
}
  • ProductService.java
import java.util.List;

public interface ProductService {
    PageResponseVo<Product> getProductPage(Integer page, Integer count);
}
  • ProductServiceImpl.java
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {

    /**
     * 获取分页数据
     * @param page
     * @param count
     * @return
     */
    @Override
    public PageResponseVo<Product> getProductPage(Integer page, Integer count) {
        Page<Product> pager = new Page<>(page, count);

        IPage<Product> paging = this.getBaseMapper().selectPage(pager, null);
        return new PageResponseVo(paging.getTotal(), paging.getRecords(), paging.getCurrent(), paging.getSize());
    }

}
  • ProductMapper.java
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.springframework.stereotype.Repository;

@Repository
public interface ProductMapper extends BaseMapper<Product> {

}
  • Product.java
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Getter;
import lombok.Setter;
import java.util.Date;

@Getter
@Setter
@TableName("product")
public class Product {

    private Integer id;

    private String title;

    @JsonIgnore
    private Date createTime;

}
  • PageResponseVo.java
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.List;

/**
 * @Description 分页数据统一返回
 * @author coisini
 * @date Sep 7, 2021
 * @Version 1.0
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class PageResponseVo<T> {

    private long total;

    private List<T> items;

    private long page;

    private long count;
}

测试

Mybatis-Plus - 分页查询


- End -
梦想是咸鱼
关注一下吧
Mybatis-Plus - 分页查询
上一篇:Spring Plugin插件系统入门


下一篇:Android Annotation使用快速入门,赶紧收藏