design项目:2、mybatis + easyui datagrid 分页查询功能介绍
2017-07-04 06:31
435 查看
当前博客介绍的所有代码,均为design项目原代码。
design项目地址:https://github.com/wenhaoran/design
design 项目,用mybatis 来操作数据库 。页面展示 用 easyui 的 datagrid 来生成 table。
先说 mybatis 的分页查询。
下面是mybatis 分页的原理。
分页拦截器,用于拦截需要进行分页查询的操作,然后对其进行分页处理。
利用拦截器实现Mybatis分页的原理:
要利用JDBC对数据库进行操作就必须要有一个对应的Statement对象,Mybatis在执行Sql语句前就会产生一个包含Sql语句的Statement对象,而且对应的Sql语句
是在Statement之前产生的,所以我们就可以在它生成Statement之前对用来生成Statement的Sql语句下手。在Mybatis中Statement语句是通过RoutingStatementHandler对象的
prepare方法生成的。所以利用拦截器实现Mybatis分页的一个思路就是拦截StatementHandler接口的prepare方法,然后在拦截器方法中把Sql语句改成对应的分页查询Sql语句,之后再调用
StatementHandler对象的prepare方法,即调用invocation.proceed()。
对于分页而言,在拦截器里面我们还需要做的一个操作就是统计满足当前条件的记录一共有多少,这是通过获取到了原始的Sql语句后,把它改为对应的统计语句再利用Mybatis封装好的参数和设
置参数的功能把Sql语句中的参数进行替换,之后再执行查询记录数的Sql语句进行总记录数的统计。
上图是,mybatis 分页插件配置。
PageInterceptor 就是,分页插件实现的具体util类。
PageInfo 类,是 分页查询,需要的参数类,其中包含 分页查询等等所必须的信息。
下图是mapper.xml 中,分页查询的方法代码:
上面提到 PageInterceptor 类,会拦截所有的查询方法。在 intercept 方法,第 61到 65行代码可以看出,拦截所有查询方法之后,会判断查询的 当前查询绑定的参数对象,是否是 pageInfo 。如果是,则会根据pageInfo 的 分页信息 拼接sql ,查询需要的结果。
***********************后台分页查询数据介绍end*****************************
下面说一下,easyui 前端页面分页。
其实前端easyui 的datagrid 分页就很简单了,datagrid自己就支持分页功能。
代码如下:
$(function() {
dataGrid = $('#dataGrid').datagrid({
url : '${path}/pages/dbconn/dataGrid',
striped : true,
rownumbers : true,
pagination : true,
singleSelect : true,
idField : 'id',
sortName : 'createDate',//id
sortOrder : 'desc',
fit : true,
fitColumns : true,
fix : false,
autoRowHeight : false,
//onClickCell:function(){}
pageSize : 20,
pageList : [ 10, 20, 30, 40, 50, 100, 200, 300, 400, 500 ],
columns: [[
{
field : 'ck', checkbox : true
},
{
width : '10%',
title : '主键',
field : 'id',
sortable : true,
align : 'center',
hidden : true
},
{
width : '10%',
title : '数据库名称',
fiel
112b5
d : 'dbname',
sortable : false,
align : 'center',
hidden : false,
} ,
{
field : 'action',
title : '操作',
width : 200,
align : 'center',
formatter : function(value, row, index) {
var str = '';
str += $.formatString('<a href="javascript:void(0)" data-row="edit_{0}" class="dbconn-easyui-linkbutton-edit" data-options="plain:true,iconCls:\'icon-dc-del\'" onclick="editFun(\'{1}\',\'{2}\');" >编辑</a>', index, row.id, index);
str += ' | ';
str += $.formatString('<a href="javascript:void(0)" data-row="del_{0}" class="dbconn-easyui-linkbutton-del" data-options="plain:true,iconCls:\'icon-dc-del\'" onclick="deleteFun(\'{1}\',\'{2}\');" >删除</a>', index, row.id, index);
str += ' | ';
str += $.formatString('<a href="javascript:void(0)" data-row="conn_{0}" class="dbconn-easyui-linkbutton-conn" data-options="plain:true,iconCls:\'icon-dc-del\'" onclick="connect(\'{1}\');" >测试连接</a>', index, row.id);
return str;
}
}
]],
onLoadSuccess:function(data){
$('.dbconn-easyui-linkbutton-edit').linkbutton({text:'更改',plain:true,iconCls:'icon-dc-edit'});
$('.dbconn-easyui-linkbutton-del').linkbutton({text:'删除',plain:true,iconCls:'icon-dc-del'});
$('.dbconn-easyui-linkbutton-conn').linkbutton({text:'测试连接',plain:true,iconCls:'icon-dc-home'});
},
toolbar : '#toolbar'
});
});
上面代码所在文件夹,如图下。
上面代码中,url : '${path}/pages/dbconn/dataGrid', 就是说,当前 table 的数据,是从 /pages/dbconn/dataGrid 这个方法中获取的。这个方法的代码,如图下:
查询需要的四个参数,page ,rows ,sort ,order 。均在 .datagrid 封装方法中 。
页面展示如图下:
更改每页显示数量为300 后,在controller 中断点查看数据如图下:
that all
可能某些地方介绍的优点模糊,不算很明确。个人水平有限,请见谅,如果有啥疑问,可以 到 github 下载代码运行之后,照着 项目看博客文档。如果还有疑问,欢迎联系本人
QQ:1286238812
design github :https://github.com/wenhaoran/design
design项目地址:https://github.com/wenhaoran/design
design 项目,用mybatis 来操作数据库 。页面展示 用 easyui 的 datagrid 来生成 table。
先说 mybatis 的分页查询。
下面是mybatis 分页的原理。
分页拦截器,用于拦截需要进行分页查询的操作,然后对其进行分页处理。
利用拦截器实现Mybatis分页的原理:
要利用JDBC对数据库进行操作就必须要有一个对应的Statement对象,Mybatis在执行Sql语句前就会产生一个包含Sql语句的Statement对象,而且对应的Sql语句
是在Statement之前产生的,所以我们就可以在它生成Statement之前对用来生成Statement的Sql语句下手。在Mybatis中Statement语句是通过RoutingStatementHandler对象的
prepare方法生成的。所以利用拦截器实现Mybatis分页的一个思路就是拦截StatementHandler接口的prepare方法,然后在拦截器方法中把Sql语句改成对应的分页查询Sql语句,之后再调用
StatementHandler对象的prepare方法,即调用invocation.proceed()。
对于分页而言,在拦截器里面我们还需要做的一个操作就是统计满足当前条件的记录一共有多少,这是通过获取到了原始的Sql语句后,把它改为对应的统计语句再利用Mybatis封装好的参数和设
置参数的功能把Sql语句中的参数进行替换,之后再执行查询记录数的Sql语句进行总记录数的统计。
上图是,mybatis 分页插件配置。
PageInterceptor 就是,分页插件实现的具体util类。
PageInfo 类,是 分页查询,需要的参数类,其中包含 分页查询等等所必须的信息。
下图是mapper.xml 中,分页查询的方法代码:
上面提到 PageInterceptor 类,会拦截所有的查询方法。在 intercept 方法,第 61到 65行代码可以看出,拦截所有查询方法之后,会判断查询的 当前查询绑定的参数对象,是否是 pageInfo 。如果是,则会根据pageInfo 的 分页信息 拼接sql ,查询需要的结果。
***********************后台分页查询数据介绍end*****************************
下面说一下,easyui 前端页面分页。
其实前端easyui 的datagrid 分页就很简单了,datagrid自己就支持分页功能。
代码如下:
$(function() {
dataGrid = $('#dataGrid').datagrid({
url : '${path}/pages/dbconn/dataGrid',
striped : true,
rownumbers : true,
pagination : true,
singleSelect : true,
idField : 'id',
sortName : 'createDate',//id
sortOrder : 'desc',
fit : true,
fitColumns : true,
fix : false,
autoRowHeight : false,
//onClickCell:function(){}
pageSize : 20,
pageList : [ 10, 20, 30, 40, 50, 100, 200, 300, 400, 500 ],
columns: [[
{
field : 'ck', checkbox : true
},
{
width : '10%',
title : '主键',
field : 'id',
sortable : true,
align : 'center',
hidden : true
},
{
width : '10%',
title : '数据库名称',
fiel
112b5
d : 'dbname',
sortable : false,
align : 'center',
hidden : false,
} ,
{
field : 'action',
title : '操作',
width : 200,
align : 'center',
formatter : function(value, row, index) {
var str = '';
str += $.formatString('<a href="javascript:void(0)" data-row="edit_{0}" class="dbconn-easyui-linkbutton-edit" data-options="plain:true,iconCls:\'icon-dc-del\'" onclick="editFun(\'{1}\',\'{2}\');" >编辑</a>', index, row.id, index);
str += ' | ';
str += $.formatString('<a href="javascript:void(0)" data-row="del_{0}" class="dbconn-easyui-linkbutton-del" data-options="plain:true,iconCls:\'icon-dc-del\'" onclick="deleteFun(\'{1}\',\'{2}\');" >删除</a>', index, row.id, index);
str += ' | ';
str += $.formatString('<a href="javascript:void(0)" data-row="conn_{0}" class="dbconn-easyui-linkbutton-conn" data-options="plain:true,iconCls:\'icon-dc-del\'" onclick="connect(\'{1}\');" >测试连接</a>', index, row.id);
return str;
}
}
]],
onLoadSuccess:function(data){
$('.dbconn-easyui-linkbutton-edit').linkbutton({text:'更改',plain:true,iconCls:'icon-dc-edit'});
$('.dbconn-easyui-linkbutton-del').linkbutton({text:'删除',plain:true,iconCls:'icon-dc-del'});
$('.dbconn-easyui-linkbutton-conn').linkbutton({text:'测试连接',plain:true,iconCls:'icon-dc-home'});
},
toolbar : '#toolbar'
});
});
上面代码所在文件夹,如图下。
上面代码中,url : '${path}/pages/dbconn/dataGrid', 就是说,当前 table 的数据,是从 /pages/dbconn/dataGrid 这个方法中获取的。这个方法的代码,如图下:
查询需要的四个参数,page ,rows ,sort ,order 。均在 .datagrid 封装方法中 。
页面展示如图下:
更改每页显示数量为300 后,在controller 中断点查看数据如图下:
that all
可能某些地方介绍的优点模糊,不算很明确。个人水平有限,请见谅,如果有啥疑问,可以 到 github 下载代码运行之后,照着 项目看博客文档。如果还有疑问,欢迎联系本人
QQ:1286238812
design github :https://github.com/wenhaoran/design
相关文章推荐
- EasyUI使用心得——datagrid的查询功能和SubGrid冲突导致分页无效问题
- JAVAEE——BOS物流项目04:学习计划、datagrid、分页查询、批量删除、修改功能
- EasyUI datagrid+SpringMVC+MyBatis实现分页查询
- easyui 通用的datagrid中如何带有查询条件分页
- EasyUI 之 DataGrid利用用拉姆达表达式实现分页查询
- springmvc+easyUI的DataGrid分页功能
- Spring+SpringMVC+MyBatis+easyUI整合基础篇(一)项目简述及技术选型介绍
- 家庭财务管理系统实战4- mybatis分页查询功能改进实现
- easyui datagrid 查询 分页
- SpringMVC与Mybatis集合实现分页功能(实际项目)
- 框架 day49 BOS项目练习3(修复window控件BUG,添加/修改/作废取派员,datagrid使用,分页查询(DetachedCriteria离线),formatter函数)
- maven项目 spring-boot 整合 mybatis 实现查询功能demo
- EasyUI-datagrid分页查询前端显示
- Asp.Net MVC EasyUI DataGrid查询分页
- 使用easyUI添加查询功能在datagrid
- (easyui datagrid+mvc+json)之asp.net分页查询
- SpringMVC+MyBatis+EasyUI 实现分页查询
- ASP.NET easyUI--datagrid 通过ajax请求ASP.NET后台数据的分页查询
- Spring+SpringMVC+MyBatis+easyUI整合基础篇(一)项目简述及技术选型介绍
- J2EE项目系列(二)--博客管理系统(Maven+SpringMVC+Hibernate以及附加分页和一对多查询功能)