[toc]

当客户查询一个列表时,需要分批返回数据,可以使用mybatis-plus自带的分页插件。

1、mybatisplus配置类

该类主要进行扫描mapper层,加载配置,启用业务管理等。

package com.demo.test.config;

import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@EnableTransactionManagement
@Configuration
@MapperScan("com.demo.test.mapper")
public class MybatisPlusConfig {

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

image.png

2、编写controller层

该层主要进行封装每页大小和页数的入参,进行通过page的构造方法进行初始化page对象,以及响应回的列表进行封装。

package com.demo.test.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.demo.test.entity.ClubPo;
import com.demo.test.service.ClubService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/club")
@Slf4j
public class ClubController {

    @Autowired
    private ClubService clubService;

    @RequestMapping("/getInfo")
    public ClubPo getInfo(Integer id) {
        return clubService.getByIdaa(id);
    }

    @RequestMapping("/getAll")
    public List<ClubPo> getAll() {
        log.info("this is getAll");
        return clubService.list();
    }

    @RequestMapping("/page")
    public IPage<ClubPo> getPage(int current, int pageSize) {
        Page<ClubPo> clubPoIPage = new Page<>(current, pageSize);
        return clubService.page2(clubPoIPage);
    }
}

3、server逻辑层

ClubService 接口

package com.demo.test.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.demo.test.entity.ClubPo;

public interface ClubService extends IService<ClubPo> {
    ClubPo getByIdaa(Integer id);

    IPage<ClubPo> page2(Page<ClubPo> clubPoIPage);
}

ClubServiceImpl 实现类

package com.demo.test.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.demo.test.entity.ClubPo;
import com.demo.test.mapper.ClubMapper;
import com.demo.test.service.ClubService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
@Transactional
public class ClubServiceImpl extends ServiceImpl<ClubMapper, ClubPo> implements ClubService {

    @Resource
    private ClubMapper clubMapper;

    @Override
    public ClubPo getByIdaa(Integer id) {
        return this.clubMapper.selectByAa(id);
    }


    @Override
    public IPage<ClubPo> page2(Page<ClubPo> clubPoIPage) {
        return this.clubMapper.selectPage2(clubPoIPage);
    }
}

5、mapper层

传递分页到mapper层,之后框架会自动进行分页,但page这个在mapper层一定要写。

package com.demo.test.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.demo.test.entity.ClubPo;

public interface ClubMapper extends BaseMapper<ClubPo> {
    ClubPo selectByAa(Integer id);

    IPage<ClubPo> selectPage2(Page<ClubPo> clubPoIPage);
}

6、xxxMapper.xml

主要的sql业务代码

<?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.demo.test.mapper.ClubMapper">

    <select id="selectByAa" parameterType="java.lang.String" resultType="com.demo.test.entity.ClubPo">
        select * from t_club
    </select>

    <select id="selectPage2" resultType="com.demo.test.entity.ClubPo">
        select * from t_club
    </select>
</mapper>

7、访问

image.png

image.png
image.png

Q.E.D.


只有创造,才是真正的享受,只有拚搏,才是充实的生活。