Mybatis分页插件PageHelper配置及使用方法详解

 更新时间:2020年08月04日 11:04:52   作者:陈彦斌  
这篇文章主要介绍了Mybatis分页插件PageHelper配置及使用方法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

环境

框架:spring+springmvc+mybatis

pom.xml

<!-- 引入mybatis的 pagehelper 分页插件 -->
    <dependency>
      <groupId>com.github.pagehelper</groupId>
      <artifactId>pagehelper</artifactId>
      <version>5.1.2</version>
    </dependency>

配置全局配置文件

在mybatis的全局配置文件中配置PageHelper分页插件

<?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>
  <!-- 引入 pageHelper插件 -->
  <!--注意这里要写成PageInterceptor, 5.0之前的版本都是写PageHelper, 5.0之后要换成PageInterceptor-->
  <plugins>
    <plugin interceptor="com.github.pagehelper.PageInterceptor">
      <!--reasonable:分页合理化参数,默认值为false,直接根据参数进行查询。
       当该参数设置为 true 时,pageNum<=0 时会查询第一页, pageNum>pages(超过总数时),会查询最后一页。-->
      <!--<property name="reasonable" value="true"/>-->
    </plugin>
  </plugins>
</configuration>

使用

例如:实现对用户的多条件查询

package com.szfore.model;
import java.util.Date;
import java.util.List;
public class User {
	private Integer id;
	private String uname;
	private String pwd;
	private String name;
	private Integer sex;
	private String phone;
	private String company;
	private String jobtitle;
	private String birth;
	private Date createdate;
	private Date lastlogintime;
	private List<Role> roleList;
	public List<Role> getRoleList() {
		return roleList;
	}
	public void setRoleList(List<Role> roleList) {
		this.roleList = roleList;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getUname() {
		return uname;
	}
	public void setUname(String uname) {
		this.uname = uname == null ? null : uname.trim();
	}
	public String getPwd() {
		return pwd;
	}
	public void setPwd(String pwd) {
		this.pwd = pwd == null ? null : pwd.trim();
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name == null ? null : name.trim();
	}
	public Integer getSex() {
		return sex;
	}
	public void setSex(Integer sex) {
		this.sex = sex;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone == null ? null : phone.trim();
	}
	public String getCompany() {
		return company;
	}
	public void setCompany(String company) {
		this.company = company == null ? null : company.trim();
	}
	public String getJobtitle() {
		return jobtitle;
	}
	public void setJobtitle(String jobtitle) {
		this.jobtitle = jobtitle == null ? null : jobtitle.trim();
	}
	public String getBirth() {
		return birth;
	}
	public void setBirth(String birth) {
		this.birth = birth == null ? null : birth.trim();
	}
	public Date getCreatedate() {
		return createdate;
	}
	public void setCreatedate(Date createdate) {
		this.createdate = createdate;
	}
	public Date getLastlogintime() {
		return lastlogintime;
	}
	public void setLastlogintime(Date lastlogintime) {
		this.lastlogintime = lastlogintime;
	}
}

UserMapper

注意:mapper中就按不分页的那种写法就好

package com.szfore.dao;

import com.szfore.model.User;
import com.szfore.model.UserExample;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

@Repository
public interface UserMapper {
  /**
   * 多条件分页查询
   * @param userParam
   * @return
   */
  public List<User> queryByPage(User userParam);
}

UserMapper.xml

注意:sql中就不要写limit了,pageHelp会自己处理,sql就按不分页的那种写法就好

<!--多条件分页查询用户-->
 <select id="queryByPage" resultType="com.szfore.model.User">
  SELECT
     *
  FROM
    `user` 
  <WHERE>
     <if test="id != null and id != ''">
     AND id = #{id}
    </if>
    <if test="uname != null and uname != ''">
     AND uname = #{uname}
    </if>
    <if test="name != null and name != ''">
     AND name like '%${name}%'
    </if>
    <if test="phone != null and phone != ''">
     AND phone like '%${phone}%'
    </if>
    <if test="company != null and company != ''">
     AND company like '%${company}%'
    </if>
    <if test="jobtitle != null and jobtitle != ''">
     AND jobTitle like '%${jobtitle}%'
    </if>
    <if test="birth != null and birth != ''">
     AND birth like '%${birth}%'
    </if>  </WHERE>
 </select>

UserServiceImpl

package com.szfore.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.szfore.dao.MenuMapper;
import com.szfore.dao.UserMapper;
import com.szfore.dao.UserRoleMapper;
import com.szfore.model.*;
import com.szfore.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements IUserService{

  @Autowired
  private UserMapper userMapper;
  @Autowired
  private MenuMapper menuMapper;
  @Autowired
  private UserRoleMapper userRoleMapper;
 
  /**
   * 多条件分页查询用户
   * @param userParam
   * @param pageNum
   * @param pageSize
   * @return
   */
  public Json queryByPage(User userParam,Integer pageNum,Integer pageSize) {
    //利用PageHelper分页查询 注意:这个一定要放查询语句的前一行,否则无法进行分页,因为它对紧随其后第一个sql语句有效
    PageHelper.startPage(pageNum, pageSize);
    List<User> userList = userMapper.queryByPage(userParam);
    PageInfo<User> pageInfo = new PageInfo<User>(userList);
    Json json = new Json();
    json.setMsg("成功!");
    json.setObj(pageInfo);
    json.setSuccess(true);
    return json;
  }
}

说明:PageInfo是PageHelper自带的分页对象类,详情如下:

当前页
private int pageNum;
每页的数量
private int pageSize;
当前页的数量
private int size;
//由于startRow和endRow不常用,这里说个具体的用法
//可以在页面中"显示startRow到endRow 共size条数据"

当前页面第一个元素在数据库中的行号
private int startRow;
当前页面最后一个元素在数据库中的行号
private int endRow;
总记录数
private long total;
总页数
private int pages;
结果集
private List<T> list;

第一页
private int firstPage;
前一页
private int prePage;

是否为第一页
private boolean isFirstPage = false;
是否为最后一页
private boolean isLastPage = false;
是否有前一页
private boolean hasPreviousPage = false;
是否有下一页
private boolean hasNextPage = false;
导航页码数
private int navigatePages;
所有导航页号
private int[] navigatepageNums;

通过PageInfo获取其他信息

PageHelper.startPage(req.getCurrentPage(), req.getPageSize(), true);
List<SecurityRiskLibary> list=securityRiskLibaryDAO.queryList(srl);
PageInfo page=new PageInfo(list);
page.getTotal();
page.xxxx

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • spring boot实战教程之shiro session过期时间详解

    spring boot实战教程之shiro session过期时间详解

    这篇文章主要给大家介绍了关于spring boot实战教程之shiro session过期时间的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起看看吧。
    2017-10-10
  • idea手动刷新git分支的详细教程

    idea手动刷新git分支的详细教程

    这篇文章主要介绍了idea手动刷新git分支,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-08-08
  • Docker使用 Maven 插件构建镜像的方法

    Docker使用 Maven 插件构建镜像的方法

    本篇文章主要介绍了Docker使用 Maven 插件构建镜像的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-02-02
  • SpringBoot接值实现方法详解

    SpringBoot接值实现方法详解

    这篇文章主要介绍了SpringBoot接值实现方法,SpringBoot接值是指在SpringBoot应用程序中接收请求参数,从HTTP请求中获取参数,并将其绑定到Java对象中进行处理的过程,感兴趣想要详细了解可以参考下文
    2023-05-05
  • JAVAEE中用Session简单实现购物车功能示例代码

    JAVAEE中用Session简单实现购物车功能示例代码

    本篇文章主要介绍了JAVAEE中用Session简单实现购物车功能示例代码,非常具有实用价值,需要的朋友可以参考下。
    2017-03-03
  • java实现文件上传的详细步骤

    java实现文件上传的详细步骤

    文件上传是用户将本地文件通过Web页面提交到服务器的过程,涉及客户端、服务器端、上传表单等组件,在SpringBoot中,通过MultipartFile接口处理上传文件,并将其保存在服务器,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2024-10-10
  • 微服务框架FEIGN使用常见问题分析

    微服务框架FEIGN使用常见问题分析

    这篇文章主要为大家介绍了微服务框架FEIGN常见问题分析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-08-08
  • Java使用Jasypt进行加密和解密的技术指南

    Java使用Jasypt进行加密和解密的技术指南

    Jasypt (Java Simplified Encryption) 是一个简化 Java 应用中加密工作的库,它支持加密和解密操作,易于与 Spring Boot 集成,通过 Jasypt,可以安全地管理敏感信息,比如数据库密码、API 密钥等,本文介绍了Java使用Jasypt进行加密和解密的技术指南,需要的朋友可以参考下
    2025-03-03
  • Java中properties文件中的中文乱码问题

    Java中properties文件中的中文乱码问题

    Properties为了方便用户的配置,用于读取Java的配置文件,不同的编程语言有自己所支持的配置文件,能让用户够脱离程序本身去修改相关的变量设置,这篇文章主要介绍了Java中properties文件中的中文乱码问题,需要的朋友可以参考下
    2023-08-08
  • Spring Boot 集成 Quartz并使用Cron 表达式实现定时任务

    Spring Boot 集成 Quartz并使用Cron 表达式实现定时任务

    本篇文章介绍了如何在 Spring Boot 中集成 Quartz 进行定时任务调度,并通过 Cron 表达式 控制任务执行时间,Quartz 提供了更强大的任务调度能力,比 @Scheduled 注解更灵活,适用于复杂的定时任务需求
    2025-04-04

最新评论