MybatisPuls分页查询

MybatisPuls分页查询

基于这几篇博客修改

[整合MybatisPlus测试]

[MybatisPlus自动填充时间]

[MybatisPlus乐观锁]

配置分页插件MybatisPlusConfig

package com.xiang.config;

import com.baomidou.mybatisplus.extension.plugins.OptimisticLockerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Created by IntelliJ IDEA.
 * User: xiang
 * Date: 2021/10/23 0:07
 */

@Configuration
@MapperScan("com.xiang.mapper")
//添加扫描注解,写入mapper的路径,否则会找不到接口并报错
public class MybatisPlusConfig {
    //乐观锁插件
    @Bean
    public OptimisticLockerInterceptor OptimisticLockerInterceptor(){
        return new OptimisticLockerInterceptor();
    }

    //分页插件
    @Bean
    public PaginationInterceptor paginationInterceptor(){
        return new PaginationInterceptor();
    }
}

测试类

package com.xiang;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiang.mapper.UserMapper;
import com.xiang.pojo.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;

/**
 * Created by IntelliJ IDEA.
 * User: xiang
 * Date: 2021/10/23 1:18
 */
@SpringBootTest
public class PageTest {
    @Autowired
    UserMapper userMapper;
    /**
     * MybatisPlus 实现分页
     */

    @Test
    void  page(){
        //1.创建Page对象,传入两个参数:当前页和每页显示记录数
        Page<User> userPage = new Page<>(1, 4);
        //2.将分页查询到的所有数据封装到Page对象中
        userMapper.selectPage(userPage,null);
        //3.通过page对象获取分页数据
        System.out.println(userPage.getCurrent());//当前页码
        List<User> list = userPage.getRecords();////每页数据List集合
        for (User user : list) {
            System.out.println(user);
        }
        System.out.println("/*********************/");
        System.out.println(userPage.getSize());//每页显示记录数
        System.out.println(userPage.getTotal());//总记录数
        System.out.println(userPage.getPages());//总页数
        System.out.println(userPage.hasNext());//是否有下一页
        System.out.println(userPage.hasPrevious());//是否有上一页
    }
}

运行结果

JsqlParserCountOptimize sql=SELECT  id,username,sex,age,birthday,create_time,update_time,version  FROM user
==>  Preparing: SELECT COUNT(1) FROM user
==> Parameters: 
<==    Columns: COUNT(1)
<==        Row: 14
==>  Preparing: SELECT id,username,sex,age,birthday,create_time,update_time,version FROM user LIMIT ?
==> Parameters: 4(Long)
<==    Columns: id, username, sex, age, birthday, create_time, update_time, version
<==        Row: 1, xiang, 男, 18, 2021-10-03, null, null, null
<==        Row: 559, 小向, 男, 18, 2021-10-04, null, null, null
<==        Row: 602, admin, 女, 18, 2021-10-20, null, null, null
<==        Row: 603, testbox, 女, 18, 2021-10-02, null, null, null
<==      Total: 4
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@6ae62c7e]
1
User(id=1, username=xiang, sex=男, age=18, birthday=Sun Oct 03 00:00:00 CST 2021, createTime=null, updateTime=null, version=0)
User(id=559, username=小向, sex=男, age=18, birthday=Mon Oct 04 00:00:00 CST 2021, createTime=null, updateTime=null, version=0)
User(id=602, username=admin, sex=女, age=18, birthday=Wed Oct 20 00:00:00 CST 2021, createTime=null, updateTime=null, version=0)
User(id=603, username=testbox, sex=女, age=18, birthday=Sat Oct 02 00:00:00 CST 2021, createTime=null, updateTime=null, version=0)
/*********************/
4
14
4
true
false

上一篇:【趣味设计模式系列】之【访问者模式】


下一篇:设计模式-Decorator模式