MBP レビュー: ページネーション

1. MBP 依存関係を追加する

        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.1</version>
        </dependency>

2. エンティティ クラスを定義します。

package cn.edu.tju.domain;

import com.baomidou.mybatisplus.annotation.TableName;

@TableName("student2")
public class Student {
    private int id;
    private String username;
    private int age;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }


}

3. マッパーを定義する

package cn.edu.tju.mapper;

import cn.edu.tju.domain.Student;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface StudentMapper extends BaseMapper<Student> {
}

4. サービスを定義する

package cn.edu.tju.service;

import cn.edu.tju.domain.Student;
import cn.edu.tju.mapper.StudentMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

@Service
public class StudentService extends ServiceImpl<StudentMapper, Student> {

}

5. コントローラーの定義

package cn.edu.tju.controller;


import cn.edu.tju.domain.Student;
import cn.edu.tju.service.StudentService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class StudentController {
    @Autowired
    private StudentService studentService;

    @RequestMapping("/getStudent")
    public Student getStudent(){
        Student byId = studentService.getById(1);
        return byId;
    }

    @RequestMapping("/getStudent/{page}/{size}")
    public List<Student> getStudentByPage(@PathVariable("page")int page
    , @PathVariable("size") int size){

        Page<Student>  studentPage = new Page<>(page,  size);
        Page<Student> pageList = studentService.page(studentPage);
        List<Student> records = pageList.getRecords();
        return records;

    }
}

6. ページング Bean の構成

package cn.edu.tju.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 PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }
}

マッパーをカスタマイズすることもできます

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.edu.tju.mapper.StudentMapper">
    <select id="selectStudentListByPage" resultType="cn.edu.tju.domain.Student">
        select * from student2
    </select>
</mapper>
package cn.edu.tju.mapper;

import cn.edu.tju.domain.Student;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

@Mapper
public interface StudentMapper extends BaseMapper<Student> {
    IPage<Student> selectStudentListByPage(IPage page);
}

おすすめ

転載: blog.csdn.net/amadeus_liu2/article/details/132915200