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

Detailed explanation of mybatis paging plug-in pageHelper instance

小云云
Release: 2018-01-27 14:54:49
Original
3239 people have browsed it

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!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template