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);
}
相关推荐
弗拉唐5 小时前
springBoot,mp,ssm整合案例
java·spring boot·mybatis
凌冰_6 小时前
IDEA2023 SpringBoot整合MyBatis(三)
spring boot·后端·mybatis
Elaine2023918 小时前
零碎04 MybatisPlus自定义模版生成代码
java·spring·mybatis
一二小选手8 小时前
【MyBatis】全局配置文件—mybatis.xml 创建xml模板
xml·java·mybatis
刘大浪9 小时前
后端数据增删改查基于Springboot+mybatis mysql 时间根据当时时间自动填充,数据库连接查询不一致,mysql数据库连接不好用
数据库·spring boot·mybatis
蓝染-惣右介11 小时前
【MyBatisPlus·最新教程】包含多个改造案例,常用注解、条件构造器、代码生成、静态工具、类型处理器、分页插件、自动填充字段
java·数据库·tomcat·mybatis
武子康12 小时前
Java-07 深入浅出 MyBatis - 一对多模型 SqlMapConfig 与 Mapper 详细讲解测试
java·开发语言·数据库·sql·mybatis·springboot
一二小选手12 小时前
【Mybatis】@Param注解 resultMap手动映射
java·mybatis
郑祎亦15 小时前
Spring Boot 项目 myblog 整理
spring boot·后端·java-ee·maven·mybatis