前些天按照视频里讲的做一个分页功能,可是不知道什么原因在页面就是不显示数据。昨天碰巧发现了一个分页插件,只需一些设置就可以完成分页,非常方便。不过由于是新手,其中遇到了很多很多麻烦,不过幸好得到大神的帮助,最终完成了功能,非常感谢他十分耐心地帮我,给你一个么么哒(づ ̄ 3 ̄)づ
没想到做一个小小的分页功能也有这么多的不懂的地方,感觉要学的东西太多太多,加油努力学习吧~

首先给出项目Github地址:Mybatis通用分页插件
然后按步骤给出各部分代码(其他无关代码和配置文件省略)

一、SqlMapConfig.xml

此处要导入两个jar包:pagehelper-5.0.0.jarjsqlparser-0.9.5.jar

<!-- 配置分页插件 -->
<plugins><plugin interceptor="com.github.pagehelper.PageHelper"><!-- 指定数据库方言 --><property name="dialect" value="mysql"/></plugin>
</plugins>
复制代码

二、po

Country.java

public class Country {private Integer id;private String countryname;private String countrycode;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getCountryname() {return countryname;}public void setCountryname(String countryname) {this.countryname = countryname == null ? null : countryname.trim();}public String getCountrycode() {return countrycode;}public void setCountrycode(String countrycode) {this.countrycode = countrycode == null ? null : countrycode.trim();}@Overridepublic String toString() {// return (this.getId()+" "+this.getCountryname()+" "+this.getCountrycode());return "Country [id=" + id +", countryname=" + countryname + ", countrycode=" + countrycode + "]";}
}
复制代码

EasyUIDataGridResult.java

public class EasyUIDataGridResult {// easyUI dataGrid 返回结果封装Long total;    // 总的记录数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;}
}
复制代码

三、service

CountryService.java

public interface CountryService {public EasyUIDataGridResult getCountryList(int page, int rows);
}
复制代码

CountryServiceImpl.java

@Service
public class CountryServiceImpl implements CountryService{@Autowiredprivate CountryMapper countryMapper;public EasyUIDataGridResult getCountryList(int page, int rows) {//分页处理PageHelper.startPage(page, rows);//查询结果CountryExample example = new CountryExample();List<Country> list = countryMapper.selectByExample(example);//获取分页信息PageInfo<Country> info = new PageInfo<>(list);EasyUIDataGridResult result = new EasyUIDataGridResult();long total = info.getTotal();//封装分页信息List<Country> row = info.getList();result.setRows(row);result.setTotal(total);return result;}
}
复制代码

四、controller

CountryController.java

@Controller
public class CountryController {@Autowiredprivate CountryService countryService;@RequestMapping("/country/list")@ResponseBodyprivate EasyUIDataGridResult getItemList(Integer page, Integer rows){EasyUIDataGridResult countrylists = countryService.getCountryList(page,rows);return countrylists;}
}
复制代码

五、springmvc.xml

在原来的基础上增加json convertors,以便将List集合转换成json对象,可以使用fastjson或者jackson库,二者择其一就好 注意Jar的版本,我选择的是jackson-core-2.8.7.jar jackson-databind-2.8.7.jar jackson-annotations-2.8.7.jar,三者缺一不可,而且不同的版本可能会导致一些异常

<mvc:annotation-driven><mvc:message-converters><bean class="org.springframework.http.converter.StringHttpMessageConverter"/><bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/></mvc:message-converters>
</mvc:annotation-driven>
复制代码

六、jsp

countryList.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<html><head><title>列表分页展示</title><!--必须引入jQuery和easyUI库--><link rel="stylesheet" type="text/css" href="../js/jquery-easyui-1.4.1/themes/default/easyui.css" /><link rel="stylesheet" type="text/css" href="../js/jquery-easyui-1.4.1/themes/icon.css" /><link rel="stylesheet" type="text/css" href="../css/taotao.css" /><script type="text/javascript" src="../js/jquery-easyui-1.4.1/jquery.min.js"></script><script type="text/javascript" src="../js/jquery-easyui-1.4.1/jquery.easyui.min.js"></script><script type="text/javascript" src="../js/jquery-easyui-1.4.1/locale/easyui-lang-zh_CN.js"></script><script type="text/javascript" src="../js/common.js"></script></head><body><table id="dg"></table></body><script>$(document).ready(function () {$('#dg').datagrid({url:'/country/list',pagination: true,columns:[[{field:'id',title:'id',width:100},{field:'countryname',title:'国家名称',width:100,align:'center'},{field:'countrycode',title:'国家代码',width:100,align:'center'}]]});});</script>
</html>
复制代码

最后成果如下所示~

转载于:https://juejin.im/post/5a3207785188257dd239a9dd

(血和泪的成果)使用PageHelper分页插件进行后台分页相关推荐

  1. spring boot整合mybatis+通用mapper+pagehelper分页插件

    spring boot整合mybatis+通用mapper+pagehelper分页插件 pom依赖 <?xml version="1.0" encoding="U ...

  2. SpringBoot入门篇--整合mybatis+generator自动生成代码+druid连接池+PageHelper分页插件

    我们这一一篇博客讲的是如何整合Springboot和Mybatis框架,然后使用generator自动生成mapper,pojo等文件.然后再使用阿里巴巴提供的开源连接池druid,这个连接池的好处我 ...

  3. SpringBoot中使用Mybatis-plus整合PageHelper分页插件踩坑

    今天使用SpringBoot中使用Mybatis-plus整PageHelper分页插件把我给坑惨了,我报错的pom.xml配置如下,其它的依赖就忽略掉了 <!--pagehelper --&g ...

  4. PageHelper 分页插件只用Page、不用PageInfo 的写法

    PageHelper 分页插件,学到的写法是得到Page 对象后再转成PageInfo 对象,但Page 是ArrayList 的子类,里面包含了数据列表和分页信息,为什么不能直接用Page 对象做分 ...

  5. springboot2.0.5集成mybatis(PageHelper分页插件、generator插件使用)

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/zab635590867/article ...

  6. Springboot 系列(十二)使用 Mybatis 集成 pagehelper 分页插件和 mapper 插件

    前言 在 Springboot 系列文章第十一篇里(使用 Mybatis(自动生成插件) 访问数据库),实验了 Springboot 结合 Mybatis 以及 Mybatis-generator 生 ...

  7. PageHelper 分页插件使用总结

    一个简洁的博客网站:http://lss-coding.top,欢迎大家来访 学习娱乐导航页:http://miss123.top/ 一.认识 PageHelper **官网:**https://pa ...

  8. 使用PageHelper分页插件手动分页,其他查询数据出现串连问题

    在一个需求中,需要在代码中进行分页,于是就选用了PageHelper分页插件进行分页,刚开始的时候忘记了PageHelper分页插件的用法于是我就用: // 设置分页查询条件 PageHelper.s ...

  9. 附加:PageHelper分页插件的:Page和PageInfo的区别;

    说明: (1)为什么写本篇博客?: ● 在[Spring Boot电商项目29:商品分类模块八:后台的[分类列表(平铺)]接口:]中,实现分页功能时,使用到了PageHelper分页插件: ● 但是, ...

最新文章

  1. nethogs监控linux流量
  2. python切片语法-Python切片符号(:)用法及示例
  3. java导出excel数据量大_解决大批量Excel导出OOM问题
  4. 20211018 一些特殊矩阵
  5. 如何处理alert、confirm、prompt对话框
  6. ASP调用.Net dll
  7. 【2016年第1期】专题导读:农业大数据
  8. java邮箱代码_java邮箱开发代码——发邮件
  9. h5 语音识别_h5 在线语音识别接口_html5 语音识别 - 云+社区 - 腾讯云
  10. mac java jni_Mac OS上编译JNI的动态库
  11. [译] 为什么需要在 React 类组件中为事件处理程序绑定 this
  12. mac 环境变量 配置
  13. 社交网络时代下的网络营销
  14. 上海计算机一级考试理论,上海市计算机一级考试理论部分(上).doc
  15. 微信订阅号“头条化”,内容创业要重新洗牌?
  16. 线性代数: 什么是矩阵,以及矩阵的线性代数意义
  17. mysql replication 监控_MySQL之-Replication监控及自动故障切换的详细分析
  18. python摄像头动作捕捉_OpenMMD:没有专业摄像设备也能动作捕捉!K帧动作设计苦手的福音~...
  19. 3255 Roadblocks
  20. java web网络硬盘设计_基于JavaEE网络硬盘的设计与实现

热门文章

  1. android顶部横线动态导航
  2. 【网络基础】为什么要对url进行encode呢?
  3. uniapp中搜索输入与频繁点击(防抖节流)
  4. .net core webapi 部署windows server 2008 r2 笔记
  5. python2.6更改为Python2.7
  6. PHP 数组变量之写时复制的要点 只有数组才有的概念。
  7. [Week17] 个人阅读作业
  8. 调整linux的时钟
  9. C#引用C++ Dll 所有類型轉換的方式(转)
  10. oracle datetime