Home Database Mysql Tutorial Detailed explanation of mybatis paging plug-in pageHelper instance

Detailed explanation of mybatis paging plug-in pageHelper instance

Jan 27, 2018 pm 02:54 PM
mybatis pagehelper Example

The paging plug-in pageHelper is also a very important plug-in. This article mainly introduces the detailed explanation and simple examples of the mybatis paging plug-in pageHelper. Friends who need it can refer to it. I hope it can help everyone.

Mybatis paging plug-in pageHelper detailed explanation and simple example

Working framework spring springmvc mybatis3

First of all, you must use the paging plug-in first Introduce maven dependencies and add the following in pom.xml


<!-- 分页助手 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>3.7.5</version>
</dependency>
Copy after login

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

1 , the content of the new mybatis-config.xml is as follows


<?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>
 <!-- 分页助手 -->
 <plugins>
  <!-- com.github.pagehelper为PageHelper类所在包名 -->
  <plugin interceptor="com.github.pagehelper.PageHelper">
  <!-- 数据库方言 -->
    <property name="dialect" value="MySQL"/>
    <!-- 设置为true时,使用RowBounds分页会进行count查询 会去查询出总数 -->
    <property name="rowBoundsWithCount" value="true"/>
  </plugin>
</plugins>
 </configuration>
Copy after login

Add a bean attribute in spring-mybatis.xml


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

Load the global configuration file


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

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


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

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


<typeAliases>
    <!-- javabean 的首字母小写的非限定类名来作为它的别名(其实别名是不去分大小写的)。也可在javabean 加上注解@Alias 来自定义别名, 例如: @Alias(student) -->
    <package name="com.lyt.usermanage.mapper"/>
  </typeAliases>
Copy after login

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


 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <!-- 加载全局的配置文件 -->
    <property name="configLocation" value="classpath:mybatis/mybatis-config.xml"></property>
    <!-- 配置mapper的扫描,找到所有的mapper.xml映射文件。 -->
    <property name="mapperLocations" value="classpath:com/lyt/usermanage/mapper/*.xml"></property>
    <!-- 配置类型别名 -->
    <property name="typeAliasesPackage" value="classpath:com/lyt/usermanage/pojo/*"></property>
  </bean>
Copy after login

Compared to the above We have one more step in the configuration here


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

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

2. The above operation 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>



  
   
    
     
      dialect=mysql
      rowBoundsWithCount=true
     
    
   
  

Copy after login

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;
Copy after login

The parameters that need to be passed in to the front desk are the current page and page Display number. Of course, the page display number can also be specified in the background. 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
Copy after login

This is if the received parameter is an empty string When it displays the page and number of items by default, you can define this yourself

The above is a simple application of pageHelper

Related recommendations:

SpringMvc+Mybatis+Pagehelper Detailed explanation of paging

bootstrap paginator paging plug-in usage method

jQuery Pagination paging plug-in detailed explanation

The above is the detailed content of Detailed explanation of mybatis paging plug-in pageHelper instance. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1660
14
PHP Tutorial
1260
29
C# Tutorial
1233
24
Detailed explanation of the Set tag function in MyBatis dynamic SQL tags Detailed explanation of the Set tag function in MyBatis dynamic SQL tags Feb 26, 2024 pm 07:48 PM

Interpretation of MyBatis dynamic SQL tags: Detailed explanation of Set tag usage MyBatis is an excellent persistence layer framework. It provides a wealth of dynamic SQL tags and can flexibly construct database operation statements. Among them, the Set tag is used to generate the SET clause in the UPDATE statement, which is very commonly used in update operations. This article will explain in detail the usage of the Set tag in MyBatis and demonstrate its functionality through specific code examples. What is Set tag Set tag is used in MyBati

Detailed explanation of MyBatis cache mechanism: understand the cache storage principle in one article Detailed explanation of MyBatis cache mechanism: understand the cache storage principle in one article Feb 23, 2024 pm 04:09 PM

Detailed explanation of MyBatis caching mechanism: One article to understand the principle of cache storage Introduction When using MyBatis for database access, caching is a very important mechanism, which can effectively reduce access to the database and improve system performance. This article will introduce the caching mechanism of MyBatis in detail, including cache classification, storage principles and specific code examples. 1. Cache classification MyBatis cache is mainly divided into two types: first-level cache and second-level cache. The first-level cache is a SqlSession-level cache. When

Detailed explanation of MyBatis first-level cache: How to improve data access efficiency? Detailed explanation of MyBatis first-level cache: How to improve data access efficiency? Feb 23, 2024 pm 08:13 PM

Detailed explanation of MyBatis first-level cache: How to improve data access efficiency? During the development process, efficient data access has always been one of the focuses of programmers. For persistence layer frameworks like MyBatis, caching is one of the key methods to improve data access efficiency. MyBatis provides two caching mechanisms: first-level cache and second-level cache. The first-level cache is enabled by default. This article will introduce the mechanism of MyBatis first-level cache in detail and provide specific code examples to help readers better understand

In-depth understanding of the batch Insert implementation principle in MyBatis In-depth understanding of the batch Insert implementation principle in MyBatis Feb 21, 2024 pm 04:42 PM

MyBatis is a popular Java persistence layer framework that is widely used in various Java projects. Among them, batch insertion is a common operation that can effectively improve the performance of database operations. This article will deeply explore the implementation principle of batch Insert in MyBatis, and analyze it in detail with specific code examples. Batch Insert in MyBatis In MyBatis, batch Insert operations are usually implemented using dynamic SQL. By constructing a line S containing multiple inserted values

MyBatis Generator configuration parameter interpretation and best practices MyBatis Generator configuration parameter interpretation and best practices Feb 23, 2024 am 09:51 AM

MyBatisGenerator is a code generation tool officially provided by MyBatis, which can help developers quickly generate JavaBeans, Mapper interfaces and XML mapping files that conform to the database table structure. In the process of using MyBatisGenerator for code generation, the setting of configuration parameters is crucial. This article will start from the perspective of configuration parameters and deeply explore the functions of MyBatisGenerator.

Analyze the caching mechanism of MyBatis: compare the characteristics and usage of first-level cache and second-level cache Analyze the caching mechanism of MyBatis: compare the characteristics and usage of first-level cache and second-level cache Feb 25, 2024 pm 12:30 PM

Analysis of MyBatis' caching mechanism: The difference and application of first-level cache and second-level cache In the MyBatis framework, caching is a very important feature that can effectively improve the performance of database operations. Among them, first-level cache and second-level cache are two commonly used caching mechanisms in MyBatis. This article will analyze the differences and applications of first-level cache and second-level cache in detail, and provide specific code examples to illustrate. 1. Level 1 Cache Level 1 cache is also called local cache. It is enabled by default and cannot be turned off. The first level cache is SqlSes

Security First: Best Practices to Prevent SQL Injection in MyBatis Security First: Best Practices to Prevent SQL Injection in MyBatis Feb 22, 2024 pm 12:51 PM

As network technology continues to develop, database attacks are becoming more and more common. SQL injection is one of the common attack methods. Attackers enter malicious SQL statements into the input box to perform illegal operations, causing data leakage, tampering or even deletion. In order to prevent SQL injection attacks, developers must pay special attention when writing code, and when using an ORM framework such as MyBatis, they need to follow some best practices to ensure the security of the system. 1. Parameterized query Parameterized query is the anti-

Sharing optimization tips for batch Insert statements in MyBatis Sharing optimization tips for batch Insert statements in MyBatis Feb 22, 2024 pm 04:51 PM

MyBatis is a popular Java persistence layer framework that implements the mapping of SQL and Java methods through XML or annotations, and provides many convenient functions for operating databases. In actual development, sometimes a large amount of data needs to be inserted into the database in batches. Therefore, how to optimize batch Insert statements in MyBatis has become an important issue. This article will share some optimization tips and provide specific code examples. 1.Use BatchExecu

See all articles