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);
}
相关推荐
西红柿维生素3 小时前
MyBatis SqlCommand+MethodSignature源码探究
mybatis
HeyZoeHey2 天前
Mybatis执行sql流程(一)
java·sql·mybatis
青川入梦2 天前
MyBatis极速通关上篇:Spring Boot环境搭建+用户管理实战
java·开发语言·mybatis
33255_40857_280592 天前
掌握分页艺术:MyBatis与MyBatis-Plus实战指南(10年Java亲授)
java·mybatis
勿在浮沙筑高台2 天前
无法获取实体类com.example.springdemo2.entity.po.UserPO对应的表名!
java·spring boot·mybatis
柯南二号3 天前
【Java后端】MyBatis-Plus 原理解析
java·开发语言·mybatis
Easocen3 天前
Mybatis学习笔记(五)
笔记·学习·mybatis
qq_三哥啊3 天前
【IDEA】设置Debug调试时调试器不进入特定类(Spring框架、Mybatis框架)
spring·intellij-idea·mybatis
柯南二号4 天前
【Java后端】Spring Boot 集成 MyBatis-Plus 全攻略
java·spring boot·mybatis
记忆不曾留4 天前
Mybatis 源码解读-SqlSession 会话源码和Executor SQL操作执行器源码
mybatis·二级缓存·sqlsession会话·executor执行器·一级缓存localcache