mybatisPlus paging configuration operation

Table of contents

supported database

paging plugin

Custom pagination plugin


Paging operations are very common in actual development, and we can see the effect of paging in various platforms and websites.

For example: pagination effect of Jingdong Mall

supported database

  • mysql,oracle,db2,h2,hsql,sqlite,postgresql,sqlserver,Phoenix,Gauss ,clickhouse,Sybase,OceanBase,Firebird,cubrid,goldilocks,csiidb,informix,TDengine,redshift

  • Dameng Database, Xugu Database, Renmin University Jincang Database, Nanda General (Huaku) Database, Nanda General Database, Shentong Database, Hangao Database, Youxuan Database

How do we configure paging in MybatisPlus? here we think

How to implement the query statement in MybatisPlus, we can implement the query statement in two ways

[1] Realize conditional query through the method provided by MybatisPlus

[2] Realize the query by customizing the SQL statement

Next, we will demonstrate how to implement these two paging methods

paging plugin

In most scenarios, if our SQL is not so complicated, we can directly implement the query through the method provided by MybatisPlus. In this case, we can achieve the paging effect by configuring the paging plug-in

The essence of paging is to set up an interceptor, intercept SQL through the interceptor, and achieve the effect of paging by adding the limit keyword at the end of the SQL statement

Next, look at the configuration steps

[1] Specify the paging plug-in of a specific database through the configuration class. Because the dialects of different databases are different, the specific paging statements generated will also be different. Here we specify the database as Mysql database

@Configuration
public class MybatisPlusConfig {
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }
}

 DbType.MYSQL: specifies the pagination of the mysql database

【2】Realize pagination query effect

@Test
void selectPage(){
    //1.创建QueryWrapper对象
    LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
    //2.创建分页查询对象,指定当前页和每页显示条数
    IPage<User> page = new Page<>(1,3);
    //3.执行分页查询
    userMapper.selectPage(page, lambdaQueryWrapper);
    //4.查看分页查询的结果
    System.out.println("当前页码值:"+page.getCurrent());
    System.out.println("每页显示数:"+page.getSize());
    System.out.println("总页数:"+page.getPages());
    System.out.println("总条数:"+page.getTotal());
    System.out.println("当前页数据:"+page.getRecords());
}

Custom pagination plugin

In some scenarios, we need to customize SQL statements to query. Next, let's demonstrate the paging operation of custom SQL

[1] Provide query statements in the UserMapper.xml mapping configuration file

<?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.powernode.mapper.UserMapper">

     <select id="selectByName" resultType="com.powernode.domain.User">
        select * from powershop_user where name = #{name}
     </select>

</mapper>

 [2] Provide the corresponding method in the Mapper interface, in which the IPage object is passed in as a parameter

@Mapper
public interface UserMapper extends BaseMapper<User> {
       IPage<User> selectByName(IPage<User> page, String name);
}
  • Parameter page: Perform an automatic pagination for the query result set. In other words, specify how to display the query result set. For example: the returned result set should display the data of page 1, and display 10 rows per page —> new Page(1,10)
  • Write an ordinary list query: what kind of data needs to be displayed, you only need to give me a list. The parameter page will be paginated according to your rules.
  • Inherit Page to implement your own paging object: to display the next line, previous line, etc., you need to write a paging object rule yourself.

 【3】Realize pagination query effect

@Test
void selectPage2(){
    //1.创建分页查询对象,指定当前页和每页显示条数
    IPage<User> page = new Page<>(1,2);
    //2.执行分页查询
    userMapper.selectByName(page,"Mary");
    //3.查看分页查询的结果
    System.out.println("当前页码值:"+page.getCurrent());
    System.out.println("每页显示数:"+page.getSize());
    System.out.println("总页数:"+page.getPages());
    System.out.println("总条数:"+page.getTotal());
    System.out.println("当前页数据:"+page.getRecords());
}

The IPage object is a built-in paging object in MyBatis-Plus, and the properties that may be used in the calling interface are as follows

  • records: the list object queried
  • pages: the total number of pages after pagination
  • total: the total number of entries
  • current: current page number
  • size: the number of items per page

Guess you like

Origin blog.csdn.net/m0_62436868/article/details/131902312