mybatis-plus分页插件使⽤
1.准备⼯作
mybatis-plus分页插件需要先进⾏配置
@Configuration
public class MyBatisPlusConfig {
/**
*  mybatis-plus分页插件
*/
@Bean
public PaginationInterceptor paginationInterceptor() {
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
return paginationInterceptor;
}
}
根据版本不同,配置⽅式可能稍有不同,分页的原理就是通过ibatis提供的sql进⾏sql拼接,如果不配置分页将失效,变成普通的列表查询如果系统中有多个分页插件例如 pagehelper 等,如果未使⽤⾃动配置,或冲突的话,也需要进⾏⼿动配置
2.使⽤
测试使⽤到的类和数据库定义如下
学⽣信息类
@Data
@TableName("student")
public class Student {
@TableId(type = IdType.AUTO)
private Integer id;
private String name;
private Integer age;
private Integer sex;
private Integer classId;
}
班级信息类
@Data
@TableName("class")
public class Class {
@TableId(type = IdType.AUTO)
private Integer id;
private String name;
private Integer grade;
private Integer classIndex;
}
数据库定义
create table student
(
id      int auto_increment
primary key,
name    varchar(255) null,
age      int null,
sex      int(1)      null,
class_id int null
);
create table class
(
id          int auto_increment
primary key,
name        varchar(255) null,
grade      int null,
class_index int null
);
基础使⽤,使⽤BaseMapper 提供的分页⽅法
Page<Student> studentPage = new Page<Student>(1L, 2L);
Page<Student> studentPage1 = studentMapper.selectPage(studentPage, null);
studentPage 和 studentPage1 是完全相同的对象,在Page<T> 类中就保存有分页和查询结果,selectPage⽅法定义如下
/**
* 根据 entity 条件,查询全部记录(并翻页)
*
* @param page        分页查询条件(可以为 RowBounds.DEFAULT)
* @param queryWrapper 实体对象封装操作类(可以为 null)
*/
<E extends IPage<T>> E selectPage(E page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
可以通过 queryWrapper拼接⾃定义参数
对于⼀些复杂查询,连表或者⾃定义xml等使⽤
mapper接⼝定义
Page<StudentVO> selectStudentInfoPage(Page<StudentVO> studentVOPage, @Param("sex") Integer sex);
⾃定义xml如下
<select id="selectStudentInfoPage" resultType="com.school.beans.vo.StudentVO">
select s.id as id,
s.name as name,
s.age as age,
s.sex as sex,
s.class_id as classId,
c.name as className,
c.class_index as classIndex
from student s join class c on s.class_id = c.id
<where>
<if test="sex !=null">
s.sex = #{sex}
</if>
</where>
</select>
调⽤
Page<StudentVO> studentVOPage = new Page<>(1L, 2L);
Page<StudentVO> studentVOPage1 = studentMapper.selectStudentInfoPage(studentVOPage, 0);
结果
打印sql
==>  Preparing: SELECT COUNT(1) FROM student s JOIN class c ON s.class_id = c.id WHERE s.sex = ?
==> Parameters: 0(Integer)
<==    Columns: COUNT(1)
<==        Row: 4
==>  Preparing: select s.id as id, s.name as name, s.age as age, s.sex as sex, s.class_id as classId, c.name as className, c.grade as grade, c.class_index as classIndex from student s join class c on s.class_id = c.id WHERE s.sex ==> Parameters: 0(Integer), 0(Long), 2(Long)
<==    Columns: id, name, age, sex, classId, className, grade, classIndex
<==        Row: 1, 喵帕斯, 5, 0, 1, ⼀年级⼀班, 1, 1
<==        Row: 2, ⼀条萤, 8, 0, 4, ⼆年级⼆班, 2, 2
<==      Total: 2
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@5cbe95b1]
这种⽅式需要⼿动多传⼊⼀个page参数,如果⾃定义参数较多的情况下可能会⽐较⿇烦.其实mybatis
使⽤⼀个参数进⾏分页
mapper定义
Page<StudentVO> selectStudentInfoPage2(StudentPageDTO studentPageDTO);
xml与上⼀种⽅法类似,只是这⾥只传⼊⼀个参数
<select id="selectStudentInfoPage2" resultType="com.school.beans.vo.StudentVO">
select s.id as id,
s.name as name,
s.age as age,
s.sex as sex,
s.class_id as classId,
c.name as className,
c.class_index as classIndex
from student s join class c on s.class_id = c.id
<where>
<if test="sex !=null">
s.sex = #{sex}
</if>
</where>
</select>
调⽤⽅法
StudentPageDTO studentPageDTO = new StudentPageDTO(0);
Page<StudentVO> studentVOPage2 = studentMapper.selectStudentInfoPage2(studentPageDTO);
打印sql
==>  Preparing: SELECT COUNT(1) FROM student s JOIN class c ON s.class_id = c.id WHERE s.sex = ?
==> Parameters: 0(Integer)
<==    Columns: COUNT(1)
<==        Row: 4
==>  Preparing: select s.id as id, s.name as name, s.age as age, s.sex as sex, s.class_id as classId, c.name as className, c.grade as grade, c.class_index as classIndex from student s join class c on s.class_id = c.id WHERE s.sex ==> Parameters: 0(Integer), 0(Long), 10(Long)分页查询插件
<==    Columns: id, name, age, sex, classId, className, grade, classIndex
<==        Row: 1, 喵帕斯, 5, 0, 1, ⼀年级⼀班, 1, 1
<==        Row: 2, ⼀条萤, 8, 0, 4, ⼆年级⼆班, 2, 2
<==        Row: 3, 夏海, 9, 0, 5, ⼆年级三班, 2, 3
<==        Row: 4, 鞠, 10, 0, 10, 五年级⼀班, 5, 1
<==      Total: 4
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@118fbaf0]
结果
可以看到参数和返回结果是同⼀对象,可以使⽤该⽅式进⾏分页的原因很简单,参数DTO继承了分页对象
@Data
@AllArgsConstructor
public class StudentPageDTO extends Page {
private Integer sex;
}
本质上和第⼀种分页没有区别
总结三种分页本质上都是⼀样的,通过拦截分页参数,拼接参数

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。