MBP复习: 分页

一、添加MBP依赖

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

二、定义实体类:

复制代码
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;
    }


}

三、定义mapper

复制代码
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> {
}

四、定义service

复制代码
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> {

}

五、定义controller

复制代码
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;

    }
}

六、配置分页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;
    }
}

也可以自定义mapper

复制代码
<?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);
}
相关推荐
信仰_2739932434 小时前
Mybatis一级缓存
java·缓存·mybatis
lunz_fly199219 小时前
【源码解读之 Mybatis】【核心篇】--第7篇:ParameterHandler参数处理机制
mybatis
lunzi_fly20 小时前
【源码解读之 Mybatis】【核心篇】--第7篇:ParameterHandler参数处理机制
mybatis
2401_837088501 天前
JedisConnectionFactory 连接池工厂类
mybatis
计算机学姐1 天前
基于微信小程序的高校班务管理系统【2026最新】
java·vue.js·spring boot·mysql·微信小程序·小程序·mybatis
梁辰兴2 天前
企业培训笔记:外卖平台后端--套餐管理模块--新建套餐信息
笔记·vue·mybatis·springboot·外卖管理系统
信仰_2739932432 天前
Mybatis-Spring重要组件介绍
java·spring·mybatis
通往曙光的路上2 天前
接口练习哈哈
mybatis
通往曙光的路上2 天前
day16_接口加强练习
mybatis
提笔了无痕3 天前
什么是Redis的缓存问题,以及如何解决
数据库·redis·后端·缓存·mybatis