Home  >  Article  >  Java  >  What are the paging implementation methods of MyBatis in Java?

What are the paging implementation methods of MyBatis in Java?

PHPz
PHPzforward
2023-05-07 08:04:16685browse

Preface

Paging is a hurdle we cannot get around in development! When the amount of your data is large, it is unrealistic to check all the data at once, so we usually query in pages, which reduces the pressure on the server and improves speed and efficiency! It also reduces the pressure on front-end rendering!

Note: Since the maximum integer allowed by java is 2147483647, the maximum integer that limit can use is also 2147483647. Retrieving a large amount of data at one time may cause memory overflow, so be careful when querying big data. use!

1. Limit paging

Syntax:

limit ${startPos},${pageSize}

In actual projects, we usually add a judgment of empty or null, as follows :

<if test="startPos!=null and pageSize!=null">
    limit ${startPos},${pageSize}
</if>

Business layer code:

<select id="getUserInfo1" parameterType="map" resultType="dayu">
    select * from user
    <if test="startPos!=null and pageSize!=null">
        limit ${startPos},${pageSize}
    </if>
</select>
List<User> getUserInfo1(Map<String,Object> map);
@Test
 public void selectUser() {
     SqlSession session = MybatisUtils.getSession();
     UserMapper mapper = session.getMapper(UserMapper.class);
     //这里塞值
      Map<String,Object> parms = new HashMap<>();
      parms.put("startPos","0");
      parms.put("pageSize","5");
     List<User> users = mapper.getUserInfo1(parms);
     for (User map: users){
         System.out.println(map);
    }
     session.close();
}

Execution result:

What are the paging implementation methods of MyBatis in Java?

Input 0, when 10:

What are the paging implementation methods of MyBatis in Java?

Summary:

  • limit 0,10;

  • 0 means starting from the 0th piece of data

  • 10 means checking 10 pieces of data

  • When you reach the second page, the limit is 10,10;

2. RowBounds paging (not recommended)

RowBounds helps us omit the limit content, we only need to pay attention to paging at the business layer! No need to pass in specified data!

However, this belongs to logical paging, that is, the SQL query actually queries all the data, which is only paging at the business layer. It takes up more memory, and the data is not updated in time, and there may be a certain lag! Not recommended!

RowBounds object has 2 properties, offset and limit.

  • offset:Starting number of rows

  • ##limit:Number of required data rows

Therefore, the data taken out is: starting from the offset 1 line, take the limit line

Business layer code:

@Test
public void selectUserRowBounds() {
    SqlSession session = MybatisUtils.getSession();
    UserMapper mapper = session.getMapper(UserMapper.class);
    // List<User> users = session.selectList("com.dy.mapper.UserMapper.getUserInfoRowBounds",null,new RowBounds(0, 5));
    List<User> users = mapper.getUserInfoRowBounds(new RowBounds(0,5));
    for (User map: users){
        System.out.println(map);
    }
    session.close();
}
List<User> getUserInfoRowBounds(RowBounds rowBounds);
<select id="getUserInfoRowBounds" resultType="dayu">
   select * from user
</select>

Execute and check the results:

What are the paging implementation methods of MyBatis in Java?

3. Mybatis_PageHelper paging plug-in

Introduce the jar package:

<dependency>
   <groupId>com.github.pagehelper</groupId>
   <artifactId>pagehelper</artifactId>
   <version>5.1.7</version>
</dependency>

Configure MyBatis core configuration file:

<plugins>
    <plugin interceptor="com.github.pagehelper.PageInterceptor" />
</plugins>

Business layer code:

@Test
public void selectUserPageHelper() {
    SqlSession session = MybatisUtils.getSession();
    UserMapper mapper = session.getMapper(UserMapper.class);
    //第二种,Mapper接口方式的调用,推荐这种使用方式。
    PageHelper.startPage(1, 3);
    List<User> list = mapper.getUserInfo();
    //用PageInfo将包装起来
    PageInfo page = new PageInfo(list);
    for (User map: list){
        System.out.println(map);
    }
    System.out.println("page:---"+page);
    session.close();
}

Execution result:

What are the paging implementation methods of MyBatis in Java?

Summary:

PageHelper is still very easy to use, and it is also a physical paging!

In fact, we generally use the second type more: Mapper interface method call

@Test
public void selectUserPageHelper() {
    SqlSession session = MybatisUtils.getSession();
    UserMapper mapper = session.getMapper(UserMapper.class);
    //第二种,Mapper接口方式的调用,推荐这种使用方式。
    PageHelper.startPage(1, 3);
    List<User> list = mapper.getUserInfo();
    //用PageInfo将包装起来
    PageInfo page = new PageInfo(list);
    for (User map: list){
        System.out.println(map);
    }
    System.out.println("page:---"+page);
    session.close();
}

Extension:

//获取第1页,10条内容,默认查询总数count
PageHelper.startPage(1, 10);
List<User> list = userMapper.selectAll();
//用PageInfo对结果进行包装
PageInfo page = new PageInfo(list);
//测试PageInfo全部属性
//PageInfo包含了非常全面的分页属性
assertEquals(1, page.getPageNum());
assertEquals(10, page.getPageSize());
assertEquals(1, page.getStartRow());
assertEquals(10, page.getEndRow());
assertEquals(183, page.getTotal());
assertEquals(19, page.getPages());
assertEquals(1, page.getFirstPage());
assertEquals(8, page.getLastPage());
assertEquals(true, page.isFirstPage());
assertEquals(false, page.isLastPage());
assertEquals(false, page.isHasPreviousPage());
assertEquals(true, page.isHasNextPage());

The above is the detailed content of What are the paging implementation methods of MyBatis in Java?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete