Mybatis学习(4):Mybatis及PageHelper插件和easyUI实现分页
来源:互联网 发布:mac收藏网页 编辑:程序博客网 时间:2024/06/06 09:10
前言
很多项目中需要将数据库中的所有数据分页展示出来,这样的技术有很多。今天我们来介绍如何使用Mybatis及其PageHelper插件和前端easyUI实现数据库信息分页展示。
正文
一,easyUI
其实我对前端js一点都不了解,但是为了做项目硬着头皮也要上,下面简单介绍一下easyUI的数据表格(datagrid)。
<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',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'">状态</th> <th data-options="field:'created'">创建日期</th> <th data-options="field:'updated'">更新日期</th> </tr> </thead></table>
这就是easyUI修饰的table标签,我们来看几个常用的属性(data-options包裹的属性):
1,singleSelect :如果为true,则只允许选择一行。
2,pagination :如果为true,则在DataGrid控件底部显示分页工具栏。
3,url :一个URL从远程站点请求数据。
4,method :该方法类型请求远程数据。
5,pageSize : 在设置分页属性的时候初始化页面大小。
我们来看一下表格长什么样:
这样,从页面点击“查询商品”,即访问表格的时候的时候,就会产生一个URL:
http://localhost:8080/item/list?page=1&rows=30
而且我们点下一页的按钮,同样会产生一个URL
http://localhost:8080/item/list?page=2&rows=30http://localhost:8080/item/list?page=3&rows=30http://localhost:8080/item/list?page=4&rows=30
我们可以使用Controller来捕获这个url并进行处理,后面会讲到。
而easyUI的DataGrid(数据表格)只接受JSON格式的返回值,并且要求的json格式为:
{total:"100",rows:[{"id":"1" , "name":"aaa"},{"id":"2" , "name":"bbb"}]}
第一个元素是数据库中的总条目数,第2个原始是个列表,列表中是每一条具体的记录,内容是数据库的列名和值。
二,Mybatis
上一篇我们使用了Mybatis逆向生成代码,其中生成的Mapper接口中就有根据条件查询数据库记录的方法,不指定条件就是查询所有。本文就使用这个方法。
三,PageHelper插件
该插件可实现对Mybatis封装的sql语句进行拦截,从而实现分页查询的功能。
实现原理:
使用步骤:
1,引入PageHelper的jar包。
2,需要在SqlMapConfig.xml中配置插件。
<plugins> <!-- com.github.pagehelper为PageHelper类所在包名 --> <plugin interceptor="com.github.pagehelper.PageHelper"> <!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库--> <property name="dialect" value="mysql"/> </plugin></plugins>
3,在查询sql语句执行前,添加一行代码:
PageHelper.startPage(page, rows) //page:要显示第几页,rows:每页显示的记录数
这样,查询结果就能按指定的参数,将表分页显示出来。
4,获取查询结果的总量
第3,4步的结果封装成对象,再返回给前端以json格式。
四,完整的流程。
1,dao层
该层就用Mapper接口中的查询方法
public interface TbItemMapper { List<TbItem> selectByExample(TbItemExample example);}
2,json对应的pojo类
service层将查询到的总记录数和每页的记录封装进该对象,并返回给controller。
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; } }
3,service层
public class ItemServiceImpl implements ItemService { @Autowired private TbItemMapper itemMapper; @Override public EUDataGridResult getItemList(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,web层
url中的page和rows属性会自动封装进控制器方法的入参中,返回值类型就是上面的pojo类型,但是@ResponseBody注解将其转化为对应的json格式,前端收到json就能自动解析进表格了。
@Controllerpublic class ItemController { @Autowired private ItemService itemService; @RequestMapping("/item/list") @ResponseBody public EUDataGridResult getItemList(Integer page, Integer rows) { EUDataGridResult result = itemService.getItemList(page, rows); return result; }}
总结
分页查询是很常见的,应掌握。
- Mybatis学习(4):Mybatis及PageHelper插件和easyUI实现分页
- mybatis实现分页插件PageHelper
- easyui 分页 + mybatis 分页插件 PageHelper 完成分页显示功能(SpringMVC + mybatis 框架)
- Mybatis分页插件PageHelper实现自动分页
- MyBatis分页插件PageHelper
- mybatis分页插件pagehelper
- Mybatis分页插件-pagehelper
- Mybatis分页插件 - PageHelper
- Mybatis分页插件PageHelper
- Mybatis分页插件PageHelper
- MyBatis分页插件PageHelper
- mybatis插件分页PageHelper
- Mybatis分页插件PageHelper
- Mybatis分页插件PageHelper
- mybatis分页插件 PageHelper
- 【mybatis】分页插件PageHelper
- Mybatis分页插件PageHelper
- Mybatis分页插件PageHelper
- C语言的单字符IO之getchar()和putchar()
- 高考机器人首秀未答满分 人工智能未知大于已知
- 字符串匹配小结
- 线程的控制
- 打造Android万能上拉下拉刷新框架--XRefreshView(三)
- Mybatis学习(4):Mybatis及PageHelper插件和easyUI实现分页
- Vim插件之vimwiki
- 人工智能实现简单的五子棋程序
- 决策树实现
- Reverse Singly LinkedList的方法探讨
- 在ssh中用struts2标签读取list数组集合
- C++之MYSQL的简介
- Angular2的一些快捷查看
- DeepCoder初步了解