SpringBoot系列—5.SpringBoot 整合Mybatis-Plus分页

SpringBoot系列—1.IDEA搭建SpringBoot框架
SpringBoot系列—2.SpringBoot拦截器篇
SpringBoot系列—3.SpringBoot Redis篇
SpringBoot系列—4.SpringBoot 整合Mybatis、MP(MyBatis-Plus)
SpringBoot系列—5.SpringBoot 整合Mybatis-Plus分页

1.新建MybatisPlusPaginationConfig.java类

image.png

package com.example.demo.config;

import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MybatisPlusPaginationConfig {

    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }
}

2.控制器新增分页查询方法

package com.example.demo.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 

* 前端控制器 *

* * @author Kevin * @since 2020-02-05 */ @RestController @RequestMapping("/user") public class UserController { @Autowired private UserMapper userMapper; @RequestMapping("/queryByPage") @ResponseBody private String queryByPage(HttpServletRequest request){ int pageIndex = Integer.parseInt(request.getParameter("pageIndex")); int pageSize = Integer.parseInt(request.getParameter("pageSize")); IPage userIPage = userMapper.selectPage(new Page<>(pageIndex,pageSize),null); return userIPage.getRecords().toString(); } }

3.测试分页是否有效

image.png

image.png

你可能感兴趣的:(SpringBoot系列—5.SpringBoot 整合Mybatis-Plus分页)