1.分页插件
MyBatis Plus自带分页插件,只要简单的配置即可实现分页功能
2.默认分页
a. 添加配置类
方式一:直接添加一个配置类
package com.qcby.mybatisPlusTest.config;import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
@MapperScan("com.qcby.mybatisPlusTest.mapper") //可以将主类中的注解移到此处
public class MybatisPlusConfig {@Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor() {MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();interceptor.addInnerInterceptor(newPaginationInnerInterceptor(DbType.MYSQL));return interceptor;}}
方式二:在启动类直接设置
package com.qcby;import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;@SpringBootApplication
@MapperScan("com.qcby.mybatisPlusTest.mapper")
public class MybatisPlusTestApplication {public static void main(String[] args) {SpringApplication.run(MybatisPlusTestApplication.class, args);}@Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor() {MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();interceptor.addInnerInterceptor(newPaginationInnerInterceptor(DbType.MYSQL));return interceptor;}}
b.测试
编写测试类
package com.qcby.mybatisPlusTest;import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qcby.mybatisPlusTest.mapper.UserMapper;
import com.qcby.mybatisPlusTest.model.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;import java.util.List;@SpringBootTest
public class MybatisPlusPageTest {@Autowiredprivate UserMapper userMapper;@Testpublic void testPage(){//设置分页参数Page<User> page = new Page<>(1, 5);userMapper.selectPage(page, null);//获取分页数据List<User> list = page.getRecords();list.forEach(System.out::println);System.out.println("当前页:"+page.getCurrent());System.out.println("每页显示的条数:"+page.getSize());System.out.println("总记录数:"+page.getTotal());System.out.println("总页数:"+page.getPages());System.out.println("是否有上一页:"+page.hasPrevious());System.out.println("是否有下一页:"+page.hasNext());}
}
测试结果
3.xml自定义分页
a. UserMapper中定义接口方法
package com.qcby.mybatisPlusTest.mapper;import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qcby.mybatisPlusTest.model.User;
import org.apache.ibatis.annotations.Param;/*** BaseMapper模仿向上抽取的思想*/
public interface UserMapper extends BaseMapper<User> {/*** 根据年龄查询用户列表,分页显示* @param page 分页对象 ,xml中可以从里面进行取值 ,传递参数 Page 即自动分页 ,必须放在第一位* @param age 年龄* @return */IPage<User> selectPageVo(@Param("page") Page<User> page, @Param("age") Integer age);}
b. UserMapper.xml中编写SQL
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.qcby.mybatisPlusTest.mapper.UserMapper"><!--SQL 片段,记录基础字段--><sql id="BaseColumns">id,username,age,email</sql><!--IPage<User> selectPageVo(Page<User> page, Integer age);--><select id="selectPageVo" resultType="User">SELECT <include refid="BaseColumns"></include> FROM t_user WHERE age > #{age}</select>
</mapper>
c. 设置类型别名扫描的包
mybatis-plus:#配置类型别名所对应的包type-aliases-package: com.qcby.mybatisPlusTest.model
d. 编写测试代码
@Testpublic void testSelectPageVo(){//设置分页参数Page<User> page = new Page<>(1, 5);//查询 age>20 的记录进行分页userMapper.selectPageVo(page, 20);//获取分页数据List<User> list = page.getRecords();list.forEach(System.out::println);System.out.println("当前页:"+page.getCurrent());System.out.println("每页显示的条数:"+page.getSize());System.out.println("总记录数:"+page.getTotal());System.out.println("总页数:"+page.getPages());System.out.println("是否有上一页:"+page.hasPrevious());System.out.println("是否有下一页:"+page.hasNext());}
执行结果
测试完成!!