分页插件

一、分页插件

MyBatis Plus自带分页插件,只要简单的配置即可实现分页功能

1、添加配置类

创建config包,创建MybatisPlusConfig类

package com.atguigu.mybatisplus.config;
@Configuration
@MapperScan("com.atguigu.mybatisplus.mapper") //可以将主类中的注解移到此处
public class MybatisPlusConfig {
}

2、添加分页插件

配置类中添加@Bean配置

@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}

3、测试分页

创建类InterceptorTests

package com.atguigu.mybatisplus;
@SpringBootTest
public class InterceptorTests {
@Resource
private UserMapper userMapper;
@Test
public void testSelectPage(){
//创建分页参数
Page<User> pageParam = new Page<>(1,5);
//执行分页查询
userMapper.selectPage(pageParam, null);
//查看分页参数的成员
System.out.println(pageParam);
}
}

二、XML自定义分页

1、UserMapper中定义接口方法

/**
* 查询 : 根据年龄查询用户列表,分页显示
*
* @param page 分页对象,xml中可以从里面进行取值,传递参数 Page 即自动分页,必须放在第一位
* @param age 年龄
* @return 分页对象
*/
IPage<User> selectPageByPage(Page<?> page, Integer age);

2、定义XML

<select id="selectPageByPage" resultType="com.atguigu.mybatisplus.entity.User">
SELECT <include refid="Base_Column_List"/> FROM user WHERE age > #{age}
</select>

3、测试

@Test
public void testSelectPageVo(){
Page<User> pageParam = new Page<>(1,5);
userMapper.selectPageByPage(pageParam, 18);
List<User> users = pageParam.getRecords();
users.forEach(System.out::println);
}
posted @   Lz_蚂蚱  阅读(115)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起