mybatis-plus分页主要是通QueryWraper下的两个方法来实现
1.返回具体的数据对像方式
<P extends IPage<T>> P selectPage(P page, @Param(“ew”) Wrapper<T> queryWrapper);
2.返回数据map的形式
<P extends IPage<Map<String, Object>>> P selectMapsPage(P page, @Param(“ew”) Wrapper<T> queryWrapper);
3.可以在mapper类里自定义分页方法来进行
以下针对1和3的方式来进行实例。
1.在configuration类中生成如下拉截器
@Configuration
public class MybatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
}
这拦截器必须生成,否则分项功能会有问题
2.测试方法如下:
@Test
public void testQueryPage(){
QueryWrapper<User> wrapper= new QueryWrapper<User>();
wrapper.ne(“age”,200);
Page<User> page= new Page<User>(6,20,true); //这里第三个参数为false的话,不会查询满足条件的记录条数
IPage<User> iPage= userMapper.selectPage(page,wrapper);
System.out.println(“总页数:”+iPage.getPages()+”,总记录数:”+iPage.getTotal()+”,当前页:”+iPage.getCurrent());
List<User> users= iPage.getRecords();
users.forEach(System.out::println);
}
3.运行log
2022-10-08 15:56:54.930 DEBUG 13940 — [ main] c.m.s.m.UserMapper.selectPage_mpCount : ==> Preparing: SELECT COUNT(*) AS total FROM user WHERE (age <> ?)
2022-10-08 15:56:54.960 DEBUG 13940 — [ main] c.m.s.m.UserMapper.selectPage_mpCount : ==> Parameters: 200(Integer)
2022-10-08 15:56:54.983 DEBUG 13940 — [ main] c.m.s.m.UserMapper.selectPage_mpCount : <== Total: 1
2022-10-08 15:56:54.993 DEBUG 13940 — [ main] c.m.s.mapper.UserMapper.selectPage : ==> Preparing: SELECT id,name,age,email FROM user WHERE (age <> ?) LIMIT ?,?
2022-10-08 15:56:54.993 DEBUG 13940 — [ main] c.m.s.mapper.UserMapper.selectPage : ==> Parameters: 200(Integer), 100(Long), 20(Long)
2022-10-08 15:56:54.996 DEBUG 13940 — [ main] c.m.s.mapper.UserMapper.selectPage : <== Total: 4
总页数:6,总记录数:104,当前页:6
User(id=196, name=zouli196, age=226, email=zoulinlin196@163.com)
User(id=197, name=zouli197, age=227, email=zoulinlin197@163.com)
User(id=198, name=zouli198, age=228, email=zoulinlin198@163.com)
User(id=199, name=zouli199, age=229, email=zoulinlin199@163.com)
2022-10-08 15:46:35.145 DEBUG 17204 — [ main] c.m.s.mapper.UserMapper.selectUserPage : ==> Preparing: select id,name,age from user WHERE (age <> ?) LIMIT ?,?
2022-10-08 15:46:35.175 DEBUG 17204 — [ main] c.m.s.mapper.UserMapper.selectUserPage : ==> Parameters: 200(Integer), 90(Long), 10(Long)
2022-10-08 15:46:35.200 DEBUG 17204 — [ main] c.m.s.mapper.UserMapper.selectUserPage : <== Total: 10
总页数:0,总记录数:0,当前页:10
User(id=186, name=zouli186, age=216, email=null)
User(id=187, name=zouli187, age=217, email=null)
User(id=188, name=zouli188, age=218, email=null)
User(id=189, name=zouli189, age=219, email=null)
User(id=190, name=zouli190, age=220, email=null)
User(id=191, name=zouli191, age=221, email=null)
User(id=192, name=zouli192, age=222, email=null)
User(id=193, name=zouli193, age=223, email=null)
User(id=194, name=zouli194, age=224, email=null)
User(id=195, name=zouli195, age=225, email=null)
另外也可以通过自定义查询SQL的方式进行
1.在maaper类中增加方法
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mesui.service.data.User;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import java.util.List;
import java.util.Map;
public interface UserMapper extends BaseMapper<User> {
@Select(“select * from user ${ew.customSqlSegment}”)
List<User> selectAllUser(@Param(Constants.WRAPPER)Wrapper<User> wrapper);
List<User> selectUsers(@Param(Constants.WRAPPER)Wrapper<User> wrapper);
IPage<User> selectUserPage(Page<User> page,@Param(Constants.WRAPPER)Wrapper<User> wrapper);
//这里的page参数必须有,否则无法分页
}
2.在maaper xml中提供sql
<?xml version=”1.0″ encoding=”UTF-8″ ?>
<!DOCTYPE mapper PUBLIC “-//mybatis.org//DTD Mapper 3.0//EN” “http://mybatis.org/dtd/mybatis-3-mapper.dtd” >
<mapper namespace=”com.mesui.service.mapper.UserMapper”>
<select id=”selectUsers” resultType=”com.mesui.service.data.User”>
select id,name,age from user ${ew.customSqlSegment}
</select>
<select id=”selectUserPage” resultType=”com.mesui.service.data.User”>
select id,name,age from user ${ew.customSqlSegment}
</select>
</mapper>
3.测试方法
@Test
public void testSelectPage(){
QueryWrapper<User> wrapper= new QueryWrapper<User>();
wrapper.ne(“age”,200);
Page<User> page= new Page<User>(10,10,false); //这里第3个参数为false,从运行log里可以看出,没有先计算就直接进行分页
IPage<User> iPage= userMapper.selectUserPage(page,wrapper);
System.out.println(“总页数:”+iPage.getPages()+”,总记录数:”+iPage.getTotal()+”,当前页:”+iPage.getCurrent());
List<User> users= iPage.getRecords();
users.forEach(System.out::println);
}
4.运行log
2022-10-08 16:02:37.143 DEBUG 388 — [ main] c.m.s.mapper.UserMapper.selectUserPage : ==> Preparing: select id,name,age from user WHERE (age <> ?) LIMIT ?,?
2022-10-08 16:02:37.171 DEBUG 388 — [ main] c.m.s.mapper.UserMapper.selectUserPage : ==> Parameters: 200(Integer), 90(Long), 10(Long)
2022-10-08 16:02:37.197 DEBUG 388 — [ main] c.m.s.mapper.UserMapper.selectUserPage : <== Total: 10
总页数:0,总记录数:0,当前页:10
User(id=186, name=zouli186, age=216, email=null)
User(id=187, name=zouli187, age=217, email=null)
User(id=188, name=zouli188, age=218, email=null)
User(id=189, name=zouli189, age=219, email=null)
User(id=190, name=zouli190, age=220, email=null)
User(id=191, name=zouli191, age=221, email=null)
User(id=192, name=zouli192, age=222, email=null)
User(id=193, name=zouli193, age=223, email=null)
User(id=194, name=zouli194, age=224, email=null)
User(id=195, name=zouli195, age=225, email=null)