Mybatis paging plug-in pageHelper detailed explanation and simple example

Mybatis paging plug-in pageHelper detailed explanation and simple example

Mybatis paging plug-in pageHelper detailed explanation and simple example

Working framework Spring Springmvc Mybatis3

First, you must introduce Maven dependencies to use the paging plug-in. Add the following to pom.xml

<!-- Pagination Helper -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>3.7.5</version>
</dependency>

Secondly, you need to add configuration to the configuration file. There are two ways

1. Create a new mybatis-config.xml file with the following content

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
 PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
 "http://mybatis.org/dtd/mybatis-3-config.dtd">

 <configuration>
 <!-- Pagination Helper -->
 <plugins>
  <!-- com.github.pagehelper is the package name of the PageHelper class-->
  <plugin interceptor="com.github.pagehelper.PageHelper">
  <!-- Database Dialect -->
    <property name="dialect" value="MySQL"/>
    <!-- When set to true, using RowBounds paging will perform a count query to find the total number -->
    <property name="rowBoundsWithCount" value="true"/>
  </plugin>
</plugins>
 </configuration>

Add a bean property in spring-mybatis.xml

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />

Load the global configuration file

<property name="configLocation" value="classpath:mybatis-config.xml"></property>

Configure mapper scanning to find all mapper.xml mapping files.

<property name="mapperLocations" value="classpath:com/lyitong/mapping/*.xml"></property>

Note: If your mybatis-config.xml configuration file has the following alias configuration enabled:

<typeAliases>
    <!-- The unqualified class name of javabean with the first letter of the class in lowercase is used as its alias (in fact, the alias is not case-sensitive). You can also add the @Alias ​​annotation to the JavaBean to define an alias, for example: @Alias(student) -->
    <package name="com.lyt.usermanage.mapper"/>
  </typeAliases>

Then your spring and mybatis integration files must add corresponding properties, otherwise the mybatis configuration file will fail to load and report an exception, as follows:

 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <!-- Load the global configuration file -->
    <property name="configLocation" value="classpath:mybatis/mybatis-config.xml"></property>
    <!-- Configure mapper scanning to find all mapper.xml mapping files. -->
    <property name="mapperLocations" value="classpath:com/lyt/usermanage/mapper/*.xml"></property>
    <!-- Configuration type alias -->
    <property name="typeAliasesPackage" value="classpath:com/lyt/usermanage/pojo/*"></property>
  </bean>

Compared with the above configuration, we have one more step here

    <property name="typeAliasesPackage" value="classpath:com/lyt/usermanage/pojo/*"></property>

When configuring, pay attention to the unification of the properties of the mybatis configuration file and the spring-mybatis integration file.

2. The above configuration is completed, the second method below

Configure the following properties directly in spring-mybatis.xml

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations" value="classpath:com/lyitong/mapping/*.xml"></property>

<!-- pageHelper paging plugin-->
<property name="plugins">
  <array>
   <bean class="com.github.pagehelper.PageHelper">
    <property name="properties">
     <value>
      dialect=mysql
      rowBoundsWithCount=true
     </value>
    </property>
   </bean>
  </array>
</property>
</bean>

After the configuration file is loaded, it can be used directly. The specific usage code is as follows:

PageHelper.startPage(Integer.parseInt(currentPage), Integer.parseInt(pageSize));
  List<LytBbsTz> publishTz = bbsTzDao.getPublishTz(userId);
  PageInfo<LytBbsTz> info = new PageInfo<LytBbsTz>(publishTz);
  map.put("status", 1);
  map.put("tzList", info.getList());
  return map;

The parameters that need to be passed in the frontend are the current page and the number of pages to be displayed. Of course, the number of pages to be displayed can also be specified in the backend. Generally, it is best to add the default configuration when receiving parameters as follows:

@RequestParam(defaultValue="1",value="currentPage")String currentPage, @RequestParam(defaultValue="10",value="pageSize")String pageSize

This is the default page and number of items that it displays when the received parameter is an empty string. You can set this yourself.

The above is a simple application of pageHelper

Thank you for reading, I hope it can help you, thank you for your support of this site!

You may also be interested in:
  • Detailed explanation of the use of Mybatis paging plug-in PageHelper
  • Problems and solutions for invalid paging plugin PageHelper in SpringBoot project
  • SpringMvc+Mybatis+Pagehelper paging details
  • Paging issues when implementing one-to-many queries with PageHelper plugin
  • Spring Boot+Mybatis+Druid+PageHelper implements multi-data source and paging method
  • Springboot integrates pagehelper paging function
  • Configuration and simple usage of Mybatis paging plug-in PageHelper (recommended)
  • SpringBoot integrates MyBatis's paging plug-in PageHelper example code
  • Use the mybatis plugin PageHelper to achieve paging effect
  • Use PageHelper plug-in to implement Service layer paging

<<:  Vue imitates ElementUI's form example code

>>:  Detailed steps to install Docker 1.8 on CentOS 7

Recommend

VUE implements token login verification

This article example shares the specific code of ...

MySQL 8.0.18 installation and configuration method graphic tutorial

This article records the installation and configu...

How to use JSZip compression in CocosCreator

CocosCreator version: 2.4.2 Practical project app...

W3C Tutorial (15): W3C SMIL Activities

SMIL adds support for timing and media synchroniz...

About Vue virtual dom problem

Table of contents 1. What is virtual dom? 2. Why ...

Teach you how to build a react+antd project from scratch

The previous articles were all my own learning lo...

Docker container orchestration implementation process analysis

In actual development or production environments,...

Implementation of setting fixed IP when starting docker container

Network type after docker installation [root@insu...

MySQL group query optimization method

MySQL handles GROUP BY and DISTINCT queries simil...

JS canvas realizes the functions of drawing board and signature board

This article shares the specific code of JS canva...

A brief discussion on several ways to pass parameters in react routing

The first parameter passing method is dynamic rou...

How to design a web page? How to create a web page?

When it comes to understanding web design, many p...