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:
|
<<: Vue imitates ElementUI's form example code
>>: Detailed steps to install Docker 1.8 on CentOS 7
Preface All hardware devices in the Linux system ...
It is mainly the configuration jump of the if jud...
Table of contents Preface Child components pass d...
When encapsulating Vue components, I will still u...
This article shares a native JS implementation of...
First, understand the updatexml() function UPDATE...
It was found in the test that when the page defini...
Install Apache from source 1. Upload the Apache s...
1. Install the built-in Linux subsystem of win10 ...
This article shares the specific code of jQuery t...
This article uses a jQuery plug-in to create an a...
<br />This tag can display a horizontal line...
ElementUI implements the table list paging effect...
1. Background The company's projects have alw...
This article shares with you how to use thinkphp5...