MyBatisPlus--分页插件
MyBatisPlus自带分页插件
MyBatis Plus自带分页插件,只要简单的配置即可实现分页功能
添加配置类或在启动类中配置分页插件
创建配置类方式:
package com.qcby.mybatisPlus.config;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MybatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor(){
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
//添加分页插件
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
}
启动类中配置分页插件方式(推荐):
@SpringBootApplication
@MapperScan("com.qcby.mybatisPlus.mapper")
public class MybatisPlusdemo01 {
public static void main(String[] args) {
SpringApplication.run(MybatisPlusdemo01.class, args);
}
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor(){
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
//添加分页插件
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
}
测试
package com.qcby.mybatisPlus;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qcby.mybatisPlus.mapper.UserMapper;
import com.qcby.mybatisPlus.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 {
@Autowired
private UserMapper userMapper;
@Test
public 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("MyBatisPlus自带分页插件");
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());
}
}
自定义分页
在UserMapper中定义接口方法
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);
}
UserMapper.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.qcby.mybatisPlus.mapper.UserMapper">
<!-- 定义基础字段 SQL 片段 -->
<sql id="BaseColumns">
uid, username, age, email
</sql>
<!-- 自定义分页查询 -->
<!--IPage<User> selectPageVo(Page<User> page, Integer age);-->
<select id="selectPageVo" resultType="com.qcby.mybatisPlus.model.User">
SELECT
<include refid="BaseColumns"></include>
FROM t_user
WHERE age > #{age}
</select>
</mapper>
测试
@Test
public void testSelectPageVo() {
//设置分页参数
Page<User> page = new Page<>(1, 5);
userMapper.selectPageVo(page,20);
//获取分页数据
List<User> list = page.getRecords();
list.forEach(System.out::println);
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());
}
原文地址:https://blog.csdn.net/jlihan/article/details/145182117
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!