服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - mybatis插件pageHelper实现分页效果

mybatis插件pageHelper实现分页效果

2021-06-26 13:31肖红 Java教程

这篇文章主要为大家详细介绍了mybatis插件pageHelper实现分页效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

最近做的一个项目在持久层我们采用的是mybatis今天完成了商品列表的分页查询的功能,这篇博客我分享一下如何采用pagehelper的插件实现分页。mybatis的应用,最大的好处就在于我们可以更加方便灵活的编写我们的sql语句,实现对单表或者多表的增删改查,在这基础上我们使用pagehelper插件实现分页更加方便了我们对项目的开发,提高了开发效率,我们以实现商品列表的查询为背景,详细介绍一下如何应用这个插件简单的实现分页功能。

1、jar包引入

我们项目中在依赖管理方面采用的是maven,所以想要引入分页的jar包,我们需要配置三坐标:

?
1
2
3
4
5
<dependency>
 <groupid>com.github.pagehelper</groupid>
 <artifactid>pagehelper</artifactid>
 <version>${pagehelper.version}</version>
</dependency>

2、配置mybatis的拦截器:

?
1
2
3
4
5
6
7
8
9
<configuration>
  <!-- 配置分页插件 -->
 <plugins>
 <plugin interceptor="com.github.pagehelper.pagehelper">
  <!-- 设置数据库类型 -->
  <property name="dialect" value="mysql"/>
 </plugin>
 </plugins>
</configuration>

3、编写service层

页面采用的是easyui的框架,页面接收数据采用的是json格式,所以在数据传输过程中,我们把最终的结果封装在一个实体里面,就需要在增加一个分页实体类:eudatagridresult

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.taotao.common.pojo;
 
import java.util.list;
 
public class eudatagridresult {
   //结果总数
 private long total;
   //结果行数
 private list<?> 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;
 }
 
}

编写业务层代码,增加分页处理,设置返回对象:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/**
 * 分页查询商品列表信息
 */
 @override
 public eudatagridresult getitembylist(int page, int rows) {
 //查询商品列表
 tbitemexample example=new tbitemexample();
 //分页处理
 pagehelper.startpage(page, rows);
 list<tbitem> list=itemmapper.selectbyexample(example);
 //创建一个返回值对象
 eudatagridresult result=new eudatagridresult();
 //设置返回结果
 result.setrows(list);
 //设置返回的总记录数
 pageinfo<tbitem> pageinfo=new pageinfo<>(list);
 result.settotal(pageinfo.gettotal());
 return result;
 }

4、编写前端控制层controller代码:

controller中主要功能是接收页面传过来的参数,并且返回json类型的数据结果:

?
1
2
3
4
5
6
7
8
9
10
11
12
/**
 * 分页查询商品信息列表
 * @param page
 * @param rows
 * @return
 */
 @requestmapping("/item/list")
 @responsebody
 public eudatagridresult getitemlist(integer page,integer rows){
 eudatagridresult result=itemservice.getitembylist(page, rows);
 return result;
 }

5、jsp的页面:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<%@ page language="java" contenttype="text/html; charset=utf-8" pageencoding="utf-8"%>
<table class="easyui-datagrid" id="itemlist" title="商品列表"
    data-options="singleselect:false,collapsible:true,pagination:true,url:'/item/list',method:'get',pagesize:30,toolbar:toolbar">
  <thead>
    <tr>
     <th data-options="field:'ck',checkbox:true"></th>
     <th data-options="field:'id',width:60">商品id</th>
      <th data-options="field:'title',width:200">商品标题</th>
      <th data-options="field:'cid',width:100">叶子类目</th>
      <th data-options="field:'sellpoint',width:100">卖点</th>
      <th data-options="field:'price',width:70,align:'right',formatter:taotao.formatprice">价格</th>
      <th data-options="field:'num',width:70,align:'right'">库存数量</th>
      <th data-options="field:'barcode',width:100">条形码</th>
      <th data-options="field:'status',width:60,align:'center',formatter:taotao.formatitemstatus">状态</th>
      <th data-options="field:'created',width:130,align:'center',formatter:taotao.formatdatetime">创建日期</th>
      <th data-options="field:'updated',width:130,align:'center',formatter:taotao.formatdatetime">更新日期</th>
    </tr>
  </thead>
</table>

6、最后的实现结果

mybatis插件pageHelper实现分页效果

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

原文链接:https://blog.csdn.net/xh921/article/details/51548578

延伸 · 阅读

精彩推荐