有关MyBatis 和 MyBatisPlus 分页分为以下两个博客讲解: 博客一 博客二
Mapper接口import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
@Mapper
public interface CategoryMapper extends BaseMapper {
List query1(@Param("state")Integer state);
IPage query2(Page page,@Param("state")Integer state);
}
注:
- 引入的Page所属于的包为com.baomidou.mybatisplus.extension.plugins.pagination.Page
- page参数为分页对象,它必须作为方法的第一个参数。利用这个参数可以实现
自动分页
。
DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
id, `name`, `level`, `state`
select
from tb_category
where state =#{state}
select
from tb_category
where state =#{state}
测试代码
@Test
void query1(){
Page page = new Page(4,3);
List list = categoryMapper.query1(1);
list.forEach(System.out::println);
System.out.println(page); //输出的page对象中没有具体的分页数据
}
@Test
void query2(){
IPage page = new Page(4,3);
categoryMapper.query2(page,1);
System.out.println(page); //输出的page对象中有具体的分页数据
}
结论
为了让IPage对象中直接包含从数据库中查询到的分页数据,建议在定义查询方法时直接使用query2的方式。