MyBatis-Plus paging query

MyBatis-Plus paging query

MySQL-User data table information

id name age email create_time update_time
1 Our 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 query operation

1. Query records by id primary key (uniqueness):T selectById(Serializable id)

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

[External link image transfer failed, the origin site may have anti-leech mechanism, it is recommended to save the image and upload it directly (img-RCz1AkjU-1645504787904) (C:\Users\17209\AppData\Roaming\Typora\typora-user-images\ image-20220222085721085.png)]

2. Batch query records through id collection: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. Query one of the conditions (using the map operation):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. Paging query (how to use?)

1. Configure the paging plugin

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

In the high version of SpringBoot, this configuration method is outdated, so another way of writing MybatisPlusInterceptor is used , as follows:

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

2. Use the Page object directly

method describe
getRecords() Query the data records of the current page, the return type is a list
getTotal() total number of records checked
getSize() page size
getPages() total pages
hasPrevious() Whether the current page has a previous page
hasNext() Whether the current page has a next page
getCurrent() Returns the current page number
//测试分页查询
@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());
}

Console print log:

==>  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

It can be seen that the bottom layer of the paging plugin also performs paging through database limit query.

Guess you like

Origin blog.csdn.net/qq_41775769/article/details/123065592