Detailed description and simple example of mybatis pageHelper, mybatispagehelper
Detailed description and simple example of mybatis pageHelper
Working framework spring springmvc mybatis3
Before using the paging plug-in, you must first introduce maven dependencies and add the following to pom. xml:
<! -- Paging assistant --> <dependency> <groupId> com. github. pagehelper </groupId> <artifactId> pagehelper </artifactId> <version> 3.7.5 </version> </dependency>
You need to add configurations to the configuration file in either of the following ways:
1, new mybatis-config.xml content is as follows
<? Xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE configuration PUBLIC "-// mybatis.org//DTD Config 3.0 //" http://mybatis.org/dtd/mybatis-3-config.dtd "> <configuration> <! -- Paging assistant --> <plugins> <! -- Com. github. pagehelper indicates the package name of the PageHelper class --> <plugin interceptor = "com. github. pagehelper. PageHelper"> <! -- Database dialect --> <property name = "dialect" value = "MySQL"/> <! -- When set to true, when RowBounds is used for paging, count queries are performed to query the total number --> <property name = "rowBoundsWithCount" value = "true"/> </plugin> </plugins> </configuration>
Add a bean property in the 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 scan to find all mapper. xml ing files.
<property name="mapperLocations" value="classpath:com/lyitong/mapping/*.xml"></property>
Note: If the following alias configuration is enabled in your mybatis-config.xml configuration file:
<TypeAliases> <! -- The lower-case non-qualified class name of a javabean is used as its alias (in fact, the alias is not case-sensitive ). You can also add the annotation @ Alias to the javabean to customize the Alias, for example, @ Alias (student) --> <package name = "com. lyt. usermanage. mapper"/> </typeAliases>
Therefore, you must add the corresponding attributes to the spring and mybatis integration files. Otherwise, an exception will be reported if the mybatis configuration file fails to be loaded, as shown below:
<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 scan to find all mapper. xml ing 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>
During configuration, note that the attributes of the mybatis configuration file and the spring-mybatis integration file must be consistent.
2. The configuration above is complete. The second method is as follows:
Configure the following properties directly in the 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 plug-in --> <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, you can use it directly. The specific 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 at the front end are the current page and page display quantity. Of course, the page display quantity 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
This is the default page and number of lines displayed when the receiving parameter is a null string.
The preceding is a simple application of pageHelper.
Thank you for reading this article. I hope it will help you. Thank you for your support for this site!