Mybatis分頁(yè)插件PageHelper配置及使用方法詳解
環(huán)境
框架:spring+springmvc+mybatis
pom.xml
<!-- 引入mybatis的 pagehelper 分頁(yè)插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.2</version>
</dependency>
配置全局配置文件
在mybatis的全局配置文件中配置PageHelper分頁(yè)插件
<?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插件 -->
<!--注意這里要寫(xiě)成PageInterceptor, 5.0之前的版本都是寫(xiě)PageHelper, 5.0之后要換成PageInterceptor-->
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<!--reasonable:分頁(yè)合理化參數(shù),默認(rèn)值為false,直接根據(jù)參數(shù)進(jìn)行查詢。
當(dāng)該參數(shù)設(shè)置為 true 時(shí),pageNum<=0 時(shí)會(huì)查詢第一頁(yè), pageNum>pages(超過(guò)總數(shù)時(shí)),會(huì)查詢最后一頁(yè)。-->
<!--<property name="reasonable" value="true"/>-->
</plugin>
</plugins>
</configuration>
使用
例如:實(shí)現(xiàn)對(duì)用戶的多條件查詢
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中就按不分頁(yè)的那種寫(xiě)法就好
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 {
/**
* 多條件分頁(yè)查詢
* @param userParam
* @return
*/
public List<User> queryByPage(User userParam);
}
UserMapper.xml
注意:sql中就不要寫(xiě)limit了,pageHelp會(huì)自己處理,sql就按不分頁(yè)的那種寫(xiě)法就好
<!--多條件分頁(yè)查詢用戶-->
<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;
/**
* 多條件分頁(yè)查詢用戶
* @param userParam
* @param pageNum
* @param pageSize
* @return
*/
public Json queryByPage(User userParam,Integer pageNum,Integer pageSize) {
//利用PageHelper分頁(yè)查詢 注意:這個(gè)一定要放查詢語(yǔ)句的前一行,否則無(wú)法進(jìn)行分頁(yè),因?yàn)樗鼘?duì)緊隨其后第一個(gè)sql語(yǔ)句有效
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;
}
}
說(shuō)明:PageInfo是PageHelper自帶的分頁(yè)對(duì)象類,詳情如下:
當(dāng)前頁(yè)
private int pageNum;
每頁(yè)的數(shù)量
private int pageSize;
當(dāng)前頁(yè)的數(shù)量
private int size;
//由于startRow和endRow不常用,這里說(shuō)個(gè)具體的用法
//可以在頁(yè)面中"顯示startRow到endRow 共size條數(shù)據(jù)"當(dāng)前頁(yè)面第一個(gè)元素在數(shù)據(jù)庫(kù)中的行號(hào)
private int startRow;
當(dāng)前頁(yè)面最后一個(gè)元素在數(shù)據(jù)庫(kù)中的行號(hào)
private int endRow;
總記錄數(shù)
private long total;
總頁(yè)數(shù)
private int pages;
結(jié)果集
private List<T> list;第一頁(yè)
private int firstPage;
前一頁(yè)
private int prePage;是否為第一頁(yè)
private boolean isFirstPage = false;
是否為最后一頁(yè)
private boolean isLastPage = false;
是否有前一頁(yè)
private boolean hasPreviousPage = false;
是否有下一頁(yè)
private boolean hasNextPage = false;
導(dǎo)航頁(yè)碼數(shù)
private int navigatePages;
所有導(dǎo)航頁(yè)號(hào)
private int[] navigatepageNums;
通過(guò)PageInfo獲取其他信息
PageHelper.startPage(req.getCurrentPage(), req.getPageSize(), true);
List<SecurityRiskLibary> list=securityRiskLibaryDAO.queryList(srl);
PageInfo page=new PageInfo(list);
page.getTotal();
page.xxxx
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Mybatis分頁(yè)插件PageHelper手寫(xiě)實(shí)現(xiàn)示例
- Mybatis第三方PageHelper分頁(yè)插件的使用與原理
- SpringBoot+Mybatis分頁(yè)插件PageHelper實(shí)現(xiàn)分頁(yè)效果
- Mybatis利用分頁(yè)插件PageHelper快速實(shí)現(xiàn)分頁(yè)查詢
- Mybatis pagehelper分頁(yè)插件使用過(guò)程解析
- MyBatis分頁(yè)插件PageHelper的具體使用
- Mybatis分頁(yè)插件PageHelper的配置和簡(jiǎn)單使用方法(推薦)
- Mybatis分頁(yè)插件PageHelper的使用詳解
- MyBatis分頁(yè)插件PageHelper的使用與原理
相關(guān)文章
idea創(chuàng)建的idea項(xiàng)目時(shí)springframework出現(xiàn)紅色的原因和解決方法
當(dāng)使用 IntelliJ IDEA 創(chuàng)建 Spring Framework 項(xiàng)目時(shí),springframework 出現(xiàn)紅色可能是因?yàn)橄嚓P(guān)的 Spring Framework 依賴沒(méi)有正確加載或項(xiàng)目的配置有問(wèn)題,本文給大家介紹了一些常見(jiàn)的原因和解決方法,需要的朋友可以參考下2023-09-09
Springboot單體架構(gòu)http請(qǐng)求轉(zhuǎn)換https請(qǐng)求來(lái)支持微信小程序調(diào)用接口
這篇文章主要介紹了Springboot單體架構(gòu)http請(qǐng)求轉(zhuǎn)換https請(qǐng)求來(lái)支持微信小程序調(diào)用接口,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
Springboot詳解實(shí)現(xiàn)食品倉(cāng)庫(kù)管理系統(tǒng)流程
這是一個(gè)使用Springboot開(kāi)發(fā)的食品倉(cāng)庫(kù)管理系統(tǒng),是為商家提供商品貨物進(jìn)銷存的信息化管理系統(tǒng),具有一個(gè)倉(cāng)庫(kù)管理系統(tǒng)該有的所有功能,感興趣的朋友快來(lái)看看吧2022-06-06
springmvc實(shí)現(xiàn)自定義類型轉(zhuǎn)換器示例
本篇文章主要介紹了springmvc實(shí)現(xiàn)自定義類型轉(zhuǎn)換器示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02
使用Spring Data JDBC實(shí)現(xiàn)DDD聚合的示例代碼
這篇文章主要介紹了使用Spring Data JDBC實(shí)現(xiàn)DDD聚合的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-09-09

