您好,登錄后才能下訂單哦!
這篇文章主要講解了怎么配置和使用Mybatis分頁插件PageHelper,內(nèi)容清晰明了,對此有興趣的小伙伴可以學(xué)習(xí)一下,相信大家閱讀完之后會有幫助。
環(huán)境
框架: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:分頁合理化參數(shù),默認(rèn)值為false,直接根據(jù)參數(shù)進(jìn)行查詢。 當(dāng)該參數(shù)設(shè)置為 true 時,pageNum<=0 時會查詢第一頁, pageNum>pages(超過總數(shù)時),會查詢最后一頁。--> <!--<property name="reasonable" value="true"/>--> </plugin> </plugins> </configuration>
使用
例如:實(shí)現(xiàn)對用戶的多條件查詢
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分頁查詢 注意:這個一定要放查詢語句的前一行,否則無法進(jìn)行分頁,因?yàn)樗鼘o隨其后第一個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自帶的分頁對象類,詳情如下:
當(dāng)前頁
private int pageNum;
每頁的數(shù)量
private int pageSize;
當(dāng)前頁的數(shù)量
private int size;
//由于startRow和endRow不常用,這里說個具體的用法
//可以在頁面中"顯示startRow到endRow 共size條數(shù)據(jù)"當(dāng)前頁面第一個元素在數(shù)據(jù)庫中的行號
private int startRow;
當(dāng)前頁面最后一個元素在數(shù)據(jù)庫中的行號
private int endRow;
總記錄數(shù)
private long total;
總頁數(shù)
private int pages;
結(jié)果集
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;
導(dǎo)航頁碼數(shù)
private int navigatePages;
所有導(dǎo)航頁號
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
看完上述內(nèi)容,是不是對怎么配置和使用Mybatis分頁插件PageHelper有進(jìn)一步的了解,如果還想學(xué)習(xí)更多內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。