MyBatis-Plus 分页查询

MyBatis-Plus 分页查询

MySQL-User 数据表信息

id name age email create_time update_time
1 Jone 18 [email protected] 2022-02-22 08:56:15 2022-02-01 08:56:20
2 Jack 20 [email protected] 2022-02-16 09:00:44 2022-02-17 09:00:48
3 Tom 28 [email protected] 2022-02-10 09:00:52 2022-02-19 09:00:57
4 Sandy 21 [email protected] 2022-02-14 09:01:02 2022-02-21 09:01:06
5 Billie 24 [email protected] 2022-02-09 09:01:13 2022-02-18 09:01:19
6 YCloud 22 [email protected] 2022-02-22 09:02:19 2022-02-22 09:02:23
7 TrainingL 23 [email protected] 2022-02-01 09:02:57 2022-02-10 09:03:01
8 Demo 22 [email protected] 2022-02-04 09:03:24 2022-02-14 09:03:28
9 Geoffrey 30 [email protected] 2022-01-12 09:04:30 2022-02-10 09:04:34
10 George 27 [email protected] 2022-02-09 09:05:09 2022-02-12 09:05:12
11 William 42 [email protected] 2022-02-22 09:05:45 2022-02-22 09:05:47
12 Glen 30 [email protected] 2022-02-08 09:07:03 2022-02-28 09:07:06

1、MyBatis-Plus 查询操作

1、通过 id 主键查询记录(唯一性):T selectById(Serializable id)

//测试查询
@Test
public void testSelectById(){
    
    
    //通过id=1L主键查询记录(唯一性)
    User user = userMapper.selectById(1L);
    System.out.println(user);
}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-RCz1AkjU-1645504787904)(C:\Users\17209\AppData\Roaming\Typora\typora-user-images\image-20220222085721085.png)]

2、通过 id 集合批量查询记录:List<T> selectBatchIds(Collection idList)

//测试批量查询
@Test
public void testSelectByBatchId(){
    
    
    List<Integer> ids = Arrays.asList(1, 2, 3);
    //通过id集合查询记录
    List<User> users = userMapper.selectBatchIds(ids);
    for (User user : users) {
    
    
        System.out.println(user);
    }
}

3、按条件查询之一(使用map操作): selectByMap()

//按条件查询之一:使用map操作
@Test
public void testSelectByMap(){
    
    
    HashMap<String, Object> map = new HashMap<>();
    //自定义要查询的条件
    map.put("name", "YCloud");
    map.put("age", 22);
    //返回值是一个列表,因为可能返回多条记录
    List<User> users = userMapper.selectByMap(map);
    for (User user : users) {
    
    
        System.out.println(user);
    }
}

2、分页查询(如何使用?)

1、配置分页插件

@Configuration
public class MyBatisPlusConfig {
    
    
    @Bean
    public PaginationInterceptor paginationInterceptor() {
    
    
        return new PaginationInterceptor();
    }
}

在高版本的 SpringBoot 中,这种配置方式已经过时了,所以采用另一种写法 MybatisPlusInterceptor , 如下:

@Configuration
public class MyBatisPlusConfig {
    
    
    //分页插件——新的分页插件,旧版本PaginationInterceptor失效了
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor(){
    
    
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }
}

2、直接使用 Page 对象

方法 描述
getRecords() 查询当前页的数据记录,返回类型是列表
getTotal() 查的记录的总数
getSize() 页面大小
getPages() 总页数
hasPrevious() 当前页是否有上一页
hasNext() 当前页是否有下一页
getCurrent() 返回当前页码
//测试分页查询
@Test
public void testPage(){
    
    
    //参数一:当前页,参数二:页面大小
    //使用了分页插件之后,所有的分页操作也变得简单
    Page<User> page = new Page<>(1, 5);
    userMapper.selectPage(page, null);

    List<User> records = page.getRecords();
    for (User user : records) {
    
    
        System.out.println(user);
    }
    System.out.println("记录总数:" + page.getTotal());
    System.out.println("每一页的大小:" + page.getSize());
    System.out.println("是否有上页:" + page.hasPrevious());
    System.out.println("当前页:" + page.getCurrent());
    System.out.println("总页数:" + page.getPages());
    System.out.println("是否有下页:" + page.hasNext());
}

控制台打印日志:

==>  Preparing: SELECT COUNT(*) FROM user
==> Parameters: 
<==    Columns: COUNT(*)
<==        Row: 12
<==      Total: 1
==>  Preparing: SELECT id,name,age,email,create_time,update_time FROM user LIMIT ?
==> Parameters: 5(Long)
<==    Columns: id, name, age, email, create_time, update_time
<==        Row: 1, Jone, 18, [email protected], 2022-02-22 08:56:15, 2022-02-01 08:56:20
<==        Row: 2, Jack, 20, [email protected], 2022-02-16 09:00:44, 2022-02-17 09:00:48
<==        Row: 3, Tom, 28, [email protected], 2022-02-10 09:00:52, 2022-02-19 09:00:57
<==        Row: 4, Sandy, 21, [email protected], 2022-02-14 09:01:02, 2022-02-21 09:01:06
<==        Row: 5, Billie, 24, [email protected], 2022-02-09 09:01:13, 2022-02-18 09:01:19
<==      Total: 5

可以看到分页插件的底层也是通过数据库 limit 查询来做分页的。

猜你喜欢

转载自blog.csdn.net/qq_41775769/article/details/123065592