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
Table of contents tool Install the plugin Add a ....
<br />When inserting music into a web page, ...
This article shares the tutorial of MySql install...
Array Methods JavaScript has provided many array ...
Table of contents 1. Introduction 2. Understand t...
If you are a developer looking to get into the wo...
Table of contents Algorithmic Strategy Single-nod...
Introduction Memcached is a distributed caching s...
1. First, create the corresponding folder accordi...
The css technique for changing the color of an im...
I recently reviewed some CSS-related knowledge po...
1. Installation environment 1. HUAWEI mate x cpu ...
Table of contents Scenario Analysis Development S...
In a front-end technology group before, a group m...
This article introduces the installation of Windo...