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

An article to understand Linux disks and disk partitions

Preface All hardware devices in the Linux system ...

How to encapsulate the table component of Vue Element

When encapsulating Vue components, I will still u...

Native JS to implement login box email prompt

This article shares a native JS implementation of...

MYSQL updatexml() function error injection analysis

First, understand the updatexml() function UPDATE...

How to start the spring-boot project using the built-in linux system in win10

1. Install the built-in Linux subsystem of win10 ...

jQuery realizes the picture following effect

This article shares the specific code of jQuery t...

jQuery plugin to implement accordion secondary menu

This article uses a jQuery plug-in to create an a...

HTML Tutorial: HTML horizontal line segment

<br />This tag can display a horizontal line...

Vue component library ElementUI realizes the paging effect of table list

ElementUI implements the table list paging effect...

Vue+thinkphp5.1+axios to realize file upload

This article shares with you how to use thinkphp5...