Home > Java > javaTutorial > SpringMvc+Mybatis+Pagehelper paging detailed explanation

SpringMvc+Mybatis+Pagehelper paging detailed explanation

高洛峰
Release: 2017-01-07 10:27:24
Original
2195 people have browsed it

Recently, the company needs to build an alarm page function, which requires paging. After checking a lot of information, I found that PageHelper is more suitable.

So I wrote a tutorial on using PageHelper from scratch, and also recorded what I did during the busy day.

1. First, you need to add the dependency of PageHelper to the project. Here I use Maven to add

<dependency> 
 <groupId>com.github.pagehelper</groupId> 
 <artifactId>pagehelper</artifactId> 
 <version>4.1.6</version> 
</dependency>
Copy after login

2. Add the configuration of pagehelper in the mybatis configuration file

<configuration>   
  <plugins>  
    <!-- com.github.pagehelper为PageHelper类所在包名 -->  
    <plugin interceptor="com.github.pagehelper.PageHelper">  
      <!-- 4.0.0以后版本可以不设置该参数 -->  
      <property name="dialect" value="mysql"/>  
      <!-- 该参数默认为false -->  
      <!-- 设置为true时,会将RowBounds第一个参数offset当成pageNum页码使用 -->  
      <!-- 和startPage中的pageNum效果一样-->  
      <property name="offsetAsPageNum" value="true"/>  
      <!-- 该参数默认为false -->  
      <!-- 设置为true时,使用RowBounds分页会进行count查询 -->  
      <property name="rowBoundsWithCount" value="true"/>  
      <!-- 设置为true时,如果pageSize=0或者RowBounds.limit = 0就会查询出全部的结果 -->  
      <!-- (相当于没有执行分页查询,但是返回结果仍然是Page类型)-->  
      <property name="pageSizeZero" value="true"/>  
      <!-- 3.3.0版本可用 - 分页参数合理化,默认false禁用 -->  
      <!-- 启用合理化时,如果pageNum<1会查询第一页,如果pageNum>pages会查询最后一页 -->  
      <!-- 禁用合理化时,如果pageNum<1或pageNum>pages会返回空数据 -->  
      <property name="reasonable" value="true"/>  
      <!-- 3.5.0版本可用 - 为了支持startPage(Object params)方法 -->  
      <!-- 增加了一个`params`参数来配置参数映射,用于从Map或ServletRequest中取值 -->  
      <!-- 可以配置pageNum,pageSize,count,pageSizeZero,reasonable,orderBy,不配置映射的用默认值 -->  
      <!-- 不理解该含义的前提下,不要随便复制该配置 -->  
      <!-- <property name="params" value="pageNum=start;pageSize=limit;"/> -->  
      <!-- 支持通过Mapper接口参数来传递分页参数 -->  
      <property name="supportMethodsArguments" value="true"/>  
      <!-- always总是返回PageInfo类型,check检查返回类型是否为PageInfo,none返回Page -->  
      <property name="returnPageInfo" value="check"/>  
    </plugin>  
  </plugins>  
</configuration>
Copy after login

3. Add a PageBean class to store paging information

public class PageBean<T> implements Serializable { 
   private static final long serialVersionUID = 1L; 
    private long total; //总记录数 
    private List<T> list; //结果集 
    private int pageNum; //第几页 
    private int pageSize; //每页记录数 
    private int pages; // 总页数 
    private int size; //当前页的数量<=pageSize 
  
    public PageBean(List<T> list){ 
      if (list instanceof Page){ 
        Page<T> page = (Page<T>) list; 
        this.pageNum = page.getPageNum(); 
        this.pageSize = page.getPageSize(); 
        this.total = page.getTotal(); 
        this.pages = page.getPages(); 
        this.list = page; 
        this.size = page.size(); 
      } 
    } 
    public long getTotal() { 
      return total; 
    } 
    public void setTotal(long total) { 
      this.total = total; 
    } 
    public List<T> getList() { 
      return list; 
    } 
    public void setList(List<T> list) { 
      this.list = list; 
    } 
    public int getSize() { 
      return size; 
    } 
    public void setSize(int size) { 
      this.size = size; 
    } 
    public int getPageNum() { 
      return pageNum; 
    } 
    public void setPageNum(int pageNum) { 
      this.pageNum = pageNum; 
    } 
    public int getPageSize() { 
      return pageSize; 
    } 
    public void setPageSize(int pageSize) { 
      this.pageSize = pageSize; 
    } 
    public int getPages() { 
      return pages; 
    } 
    public void setPages(int pages) { 
      this.pages = pages; 
    } 
  }
Copy after login

The following is the business logic code

4. First start from the mapper.xml file, operate the database sql, check Output the data we need

<select id="selectallList" parameterType="com.alarm.bean.AlarmParamModel"
  resultMap="AlarmMap"> 
  select message_id, seqnum, message_type, process_status, 
  distribute_status, processor, occur_time, close_time,  
  system_id, group_id, warn_level, message_content 
  from td_alarm_info 
</select>
Copy after login

5. Mapper’s interface method

public List<AlarmParamModel> selectallList(AlarmParamModel model);
Copy after login

##6.service’s interface method

Datagrid selectallList(AlarmParamModel model,int pageNum, int pageSize);
Copy after login

7.service implementation class

What needs to be noted here is the main logic of paging. pageNum represents the page number, pageSize represents the number displayed on each page, the startPag method is the initial page, the orderBy method is to sort the data by a certain field, here I use the descending order (desc) of occr_time

public Datagrid selectallList(AlarmParamModel model,int pageNum, int pageSize){
    PageHelper.startPage(pageNum, pageSize);
    PageHelper.orderBy("occur_time desc");
     List<AlarmParamModel> list = this.alarmMgrMapper.selectallList(model);
     PageInfo<AlarmParamModel> pageInfo = new PageInfo<AlarmParamModel>(list);
    Datagrid datagrid = new Datagrid(pageInfo.getTotal(),pageInfo.getList());
     return datagrid;
  }
Copy after login

8. Notice that I use a Datagrid class here, which is used to transmit data to the front desk, including total (total number) and rows (data)

public class Datagrid {
  private long total;
  private List rows = new ArrayList<>();
  public Datagrid() {
    super();
  }
  public Datagrid(long total, List rows) {
    super();
    this.total = total;
    this.rows = rows;
  }
  public long getTotal() {
    return total;
  }
  public void setTotal(long total) {
    this.total = total;
  }
  public List getRows() {
    return rows;
  }
  public void setRows(List rows) {
    this.rows = rows;
  }
}
Copy after login

9. Start writing the controller layer and call the method written before

It should be noted here that offset and limit are the page number and each page passed from the front desk The displayed quantity is different from the offset and limit of bootstraptable. The offset represents the offset, that is, if 10 pieces of data are displayed on each page, then the offset represented by the second page in bootstrap is 10, the first page and the third page. They are 0 and 20 respectively. And the offset here refers to the pageNum.

@RequestMapping(value = "/AlarmInfo/list", method = {RequestMethod.GET,RequestMethod.POST})
  @ResponseBody
  public Datagrid alarmInfo(AlarmParamModel model,@RequestParam(value="offset",defaultValue="0",required=false)Integer pageNum, 
      @RequestParam(value="limit",defaultValue="10",required=false)Integer pageSize) 
  {
    Datagrid datagrid = this.alarmMgrService.selectallList(model,pageNum, pageSize);
    return datagrid;
  }
Copy after login

10. Now the front-end request can obtain the background data and paginate it. I will post the configuration of my front-end bootstrap table

$(&#39;#tb_departments&#39;).bootstrapTable({
      url: &#39;http://10.1.234.134:8088/api/AlarmInfo/list&#39;,     //请求后台的URL(*)
      method: &#39;get&#39;,           //请求方式(*)
      striped: false,           //是否显示行间隔色
      cache: false,            //是否使用缓存,默认为true,所以一般情况下需要设置一下这个属性(*)
      pagination: true,          //是否显示分页(*)
      onlyInfoPagination:true,     //设置为 true 只显示总数据数,而不显示分页按钮。需要 pagination=&#39;True&#39;
      sortable: true,           //是否启用排序
      sortOrder: "asc",          //排序方式
      queryParams: oTableInit.queryParams,//传递参数(*)
      sidePagination: "server",      //分页方式:client客户端分页,server服务端分页(*)
      pageNumber:1,            //初始化加载第一页,默认第一页
      pageSize: 10,            //每页的记录行数(*)
      pageList: [10, 25, 50, 100],    //可供选择的每页的行数(*)
      search: false,            //是否显示表格搜索,此搜索是客户端搜索,不会进服务端,所以,个人感觉意义不大
      strictSearch: true,
      showColumns: false,         //是否显示所有的列
      showRefresh: false,         //是否显示刷新按钮
      minimumCountColumns: 2,       //最少允许的列数
      clickToSelect: true,        //是否启用点击选中行
      checkboxHeader:true,       //add
      height: 500,            //行高,如果没有设置height属性,表格自动根据记录条数觉得表格高度
      uniqueId: "id",           //每一行的唯一标识,一般为主键列
      showToggle:false,          //是否显示详细视图和列表视图的切换按钮
      cardView: false,          //是否显示详细视图
      detailView: true,
      detailFormatter:detailFormatter ,
      paginationHAlign:"left",
      paginationDetailHAlign:"right",
Copy after login

Here I did not use the paging buttons that come with bootstrap. I wrote the button group myself using jq. I will post the button code in the next article. This way the level of customization will be higher~ You can also directly use the paging button of the bootstraptable subband to change the configuration.


The above is the detailed explanation of SpringMvc+Mybatis+Pagehelper pagination introduced by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. of. I would also like to thank you all for your support of the PHP Chinese website!

For more SpringMvc+Mybatis+Pagehelper paging related articles, please pay attention to 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