一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Mybatis分頁插件PageHelper配置及使用方法詳解

Mybatis分頁插件PageHelper配置及使用方法詳解

2020-08-04 12:56陳彥斌 Java教程

這篇文章主要介紹了Mybatis分頁插件PageHelper配置及使用方法詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

環境

框架:spring+springmvc+mybatis

pom.xml

?
1
2
3
4
5
6
<!-- 引入mybatis的 pagehelper 分頁插件 -->
    <dependency>
      <groupId>com.github.pagehelper</groupId>
      <artifactId>pagehelper</artifactId>
      <version>5.1.2</version>
    </dependency>

配置全局配置文件

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?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>

使用

例如:實現對用戶的多條件查詢

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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中就按不分頁的那種寫法就好

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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就按不分頁的那種寫法就好

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<!--多條件分頁查詢用戶-->
 <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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:https://www.cnblogs.com/chenyanbin/p/13416870.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧洲肥女大肥臀tv | 高清在线观看免费 | 亚洲黄色三级视频 | h版欧美大片免费观看 | 国产区综合另类亚洲欧美 | 欧美日韩国产成人综合在线影院 | 欧美精品一国产成人性影视 | 国内精品国语自产拍在线观看55 | 日本肉体xxxx69xxxx | 亚洲国产精品久久久久久网站 | 男人边吃奶边做好爽视频免费 | 猫咪免费人成网站在线观看入口 | 国产成人永久免费视 | 9191视频 | aaaa黄色片 | 啊哈用力cao我 | 欧美成人在线影院 | 欧美精品一区二区三区久久 | 和肥岳在厨房激情 | 免费看男女污污完整版 | 国产精品免费视频一区一 | 亚洲高清在线天堂精品 | 九九热精品免费观看 | 亚洲国产精品自在现线让你爽 | 久久高清一级毛片 | 日本xxx在线观看免费播放 | 亚洲视频免费在线观看 | 毛片免 | 久久精品男人影院 | 四虎最新网址在线观看 | 亚洲精品一区制服丝袜 | 欧美特级午夜一区二区三区 | 欧美理论片手机在线观看片免费 | 美女被爆操 | 亚洲国产三级在线观看 | 538精品视频在线观看 | 丰腴尤物贵妇浪荡小说 | 亚洲国产欧美另类va在线观看 | 国产美女做爰免费视频网址 | 欧美在线视频一区在线观看 | 被老外玩爽的中国美女视频 |