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);
}
相关推荐
设计师小聂!14 小时前
尚庭公寓----------分页查询
java·开发语言·spring·maven·mybatis
秋秋棠20 小时前
MyBatis延迟加载(Lazy Loading)之“关联查询”深度解析与实践
java·mybatis
秋秋棠1 天前
MyBatis级联查询深度解析:一对多关联实战指南
jvm·tomcat·mybatis
hello 早上好2 天前
MyBatis 动态 SQL、#{}与 ${}区别、与 Hibernate区别、延迟加载、优势、XML映射关系
sql·mybatis·hibernate
我命由我123452 天前
Spring Boot - Spring Boot 集成 MyBatis 分页实现 手写 SQL 分页
java·spring boot·后端·sql·spring·java-ee·mybatis
艺杯羹2 天前
MyBatis 之分页四式传参与聚合、主键操作全解
java·开发语言·maven·mybatis
设计师小聂!2 天前
尚庭公寓-----day1 业务功能实现
java·ide·spring·maven·mybatis
郑州吴彦祖7722 天前
Mybatis的SQL编写—XML方式
java·sql·spring·mybatis
Raners_2 天前
【Java代码审计(2)】MyBatis XML 注入审计
xml·java·安全·网络安全·mybatis
别来无恙1493 天前
Spring Boot + MyBatis 实现用户登录功能详解(基础)
spring boot·后端·mybatis