mybatis-plus 分页插件使用

mybatis-plus 分页插件使用

将插件配置入Interceptor

package org.train.springboot_train.config;

import com.baomidou.mybatisplus.annotation.DbType;
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();
//        interceptor.addInnerInterceptor(new IllegalSQLInnerInterceptor());
        // 如果配置多个插件, 切记分页最后添加
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }
}

Controller

⚠️ 主要注意IPage的使用

package org.train.springboot_train.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.train.springboot_train.entity.Consumer;
import org.train.springboot_train.service.ConsumerService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping(path = {"/consumer"})
public class ConsumerController {
    @Resource
    private ConsumerService consumerService;

    @GetMapping
    public ResponseEntity findById(@RequestParam(value = "id") String id,
                                   @RequestParam(value = "currentPage",required = false,defaultValue = "1") Integer currentPage,
                                   @RequestParam(value = "pageSize",required = false,defaultValue = "10") Integer pageSize){
		// 构造Page实例
        Page<Consumer> page = new Page(currentPage,pageSize);
        IPage<Consumer> resource = consumerService.findById(page, id);
        return new ResponseEntity(res, HttpStatus.OK);
    }

Service

接口

package org.train.springboot_train.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
import org.train.springboot_train.entity.Consumer;

public interface ConsumerService extends IService<Consumer> {

    IPage<Consumer> findById(IPage<Consumer> page, String id);
}

实现类

package org.train.springboot_train.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.train.springboot_train.entity.Consumer;
import org.train.springboot_train.mapper.ConsumerMapper;

@Service
public class ConsumerServiceImpl extends ServiceImpl<ConsumerMapper, Consumer> implements ConsumerService {
    private ConsumerMapper consumerMapper;
	// 构造器注入Bean
    @Autowired
    public ConsumerServiceImpl(ConsumerMapper consumerMapper){ this.consumerMapper = consumerMapper; }
    
    @Override
    public IPage<Consumer> findById(IPage<Consumer> page, String id) {
        return consumerMapper.findById(page, id);
    }
}

Mapper

interface

@Mapper
public interface ConsumerMapper extends BaseMapper<Consumer> {
    IPage<Consumer> findById(IPage page, @Param(value = "id") String id);
}

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="org.train.springboot_train.mapper.ConsumerMapper">
    <!--org.train.springboot_train.entity.Consumer-->
    <select id="findById" resultType="consumer">
        select id,name,age,sex from consumer where id = #{id}
    </select>
</mapper>

总结:IPage插件已经帮开发者自动实现了分页效果,所以mapper.xml中不需要使用limit进行分页,也不需要使用IPage参数。

posted @   勤匠  阅读(35)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示