SpringMvc+Mybatis+Pagehelper分页详解
最近公司需要做一个告警页面的功能,需要分页,查了很多资料发现PageHelper比较合适
故写一篇从零开始的PageHelper使用的教程,也记录下忙活一天的东西
1.首先需要在项目中添加PageHelper的依赖,这里我用的Maven添加
<dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>4.1.6</version> </dependency>
2.在mybatis的配置文件中添加对pagehelper的配置
<configuration> <plugins> <!--com.github.pagehelper为PageHelper类所在包名--> <plugininterceptor="com.github.pagehelper.PageHelper"> <!--4.0.0以后版本可以不设置该参数--> <propertyname="dialect"value="mysql"/> <!--该参数默认为false--> <!--设置为true时,会将RowBounds第一个参数offset当成pageNum页码使用--> <!--和startPage中的pageNum效果一样--> <propertyname="offsetAsPageNum"value="true"/> <!--该参数默认为false--> <!--设置为true时,使用RowBounds分页会进行count查询--> <propertyname="rowBoundsWithCount"value="true"/> <!--设置为true时,如果pageSize=0或者RowBounds.limit=0就会查询出全部的结果--> <!--(相当于没有执行分页查询,但是返回结果仍然是Page类型)--> <propertyname="pageSizeZero"value="true"/> <!--3.3.0版本可用-分页参数合理化,默认false禁用--> <!--启用合理化时,如果pageNum<1会查询第一页,如果pageNum>pages会查询最后一页--> <!--禁用合理化时,如果pageNum<1或pageNum>pages会返回空数据--> <propertyname="reasonable"value="true"/> <!--3.5.0版本可用-为了支持startPage(Objectparams)方法--> <!--增加了一个`params`参数来配置参数映射,用于从Map或ServletRequest中取值--> <!--可以配置pageNum,pageSize,count,pageSizeZero,reasonable,orderBy,不配置映射的用默认值--> <!--不理解该含义的前提下,不要随便复制该配置--> <!--<propertyname="params"value="pageNum=start;pageSize=limit;"/>--> <!--支持通过Mapper接口参数来传递分页参数--> <propertyname="supportMethodsArguments"value="true"/> <!--always总是返回PageInfo类型,check检查返回类型是否为PageInfo,none返回Page--> <propertyname="returnPageInfo"value="check"/> </plugin> </plugins> </configuration>
3.添加一个PageBean的类来储存分页的信息
publicclassPageBean<T>implementsSerializable{
privatestaticfinallongserialVersionUID=1L;
privatelongtotal;//总记录数
privateList<T>list;//结果集
privateintpageNum;//第几页
privateintpageSize;//每页记录数
privateintpages;//总页数
privateintsize;//当前页的数量<=pageSize
publicPageBean(List<T>list){
if(listinstanceofPage){
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();
}
}
publiclonggetTotal(){
returntotal;
}
publicvoidsetTotal(longtotal){
this.total=total;
}
publicList<T>getList(){
returnlist;
}
publicvoidsetList(List<T>list){
this.list=list;
}
publicintgetSize(){
returnsize;
}
publicvoidsetSize(intsize){
this.size=size;
}
publicintgetPageNum(){
returnpageNum;
}
publicvoidsetPageNum(intpageNum){
this.pageNum=pageNum;
}
publicintgetPageSize(){
returnpageSize;
}
publicvoidsetPageSize(intpageSize){
this.pageSize=pageSize;
}
publicintgetPages(){
returnpages;
}
publicvoidsetPages(intpages){
this.pages=pages;
}
}
下面就是业务逻辑的代码了
4.首先从mapper.xml文件写起,操作数据库的sql,查出我们所需要的数据
<selectid="selectallList"parameterType="com.alarm.bean.AlarmParamModel" resultMap="AlarmMap"> selectmessage_id,seqnum,message_type,process_status, distribute_status,processor,occur_time,close_time, system_id,group_id,warn_level,message_content fromtd_alarm_info </select>
5.mapper的接口方法
publicList<AlarmParamModel>selectallList(AlarmParamModelmodel);
6.service的接口方法
DatagridselectallList(AlarmParamModelmodel,intpageNum,intpageSize);
7.service的实现类
这里需要注意下,是分页的主要逻辑。pageNum表示页码,pageSize表示每页显示的数目,startPag方法是初始的页面,orderBy方法是将数据按某个字段进行排序,这里我用的是occr_time的降序(desc)
publicDatagridselectallList(AlarmParamModelmodel,intpageNum,intpageSize){
PageHelper.startPage(pageNum,pageSize);
PageHelper.orderBy("occur_timedesc");
List<AlarmParamModel>list=this.alarmMgrMapper.selectallList(model);
PageInfo<AlarmParamModel>pageInfo=newPageInfo<AlarmParamModel>(list);
Datagriddatagrid=newDatagrid(pageInfo.getTotal(),pageInfo.getList());
returndatagrid;
}
8.注意到我这边用了一个Datagrid类,是用于向前台传数据用的类,包括total(总数)和rows(数据)
publicclassDatagrid{
privatelongtotal;
privateListrows=newArrayList<>();
publicDatagrid(){
super();
}
publicDatagrid(longtotal,Listrows){
super();
this.total=total;
this.rows=rows;
}
publiclonggetTotal(){
returntotal;
}
publicvoidsetTotal(longtotal){
this.total=total;
}
publicListgetRows(){
returnrows;
}
publicvoidsetRows(Listrows){
this.rows=rows;
}
}
9.开始写controller层,调用之前写的方法
这里需要注意的是,offset和limit是前台传来的页码和每页显示的数量,区别于bootstraptable的offset和limit,那个offset表示的是偏移量,即如果每页显示10条数据,那么bootstrap中的第二页表示的offset就是10,第一页和第三页分别是0和20。而我这里的offset就是指代的pageNum。
@RequestMapping(value="/AlarmInfo/list",method={RequestMethod.GET,RequestMethod.POST})
@ResponseBody
publicDatagridalarmInfo(AlarmParamModelmodel,@RequestParam(value="offset",defaultValue="0",required=false)IntegerpageNum,
@RequestParam(value="limit",defaultValue="10",required=false)IntegerpageSize)
{
Datagriddatagrid=this.alarmMgrService.selectallList(model,pageNum,pageSize);
returndatagrid;
}
10.到现在前台的请求已经可以获取后台的数据并且分页了,我再将我的前台bootstraptable的配置贴一下
$('#tb_departments').bootstrapTable({
url:'http://10.1.234.134:8088/api/AlarmInfo/list',//请求后台的URL(*)
method:'get',//请求方式(*)
striped:false,//是否显示行间隔色
cache:false,//是否使用缓存,默认为true,所以一般情况下需要设置一下这个属性(*)
pagination:true,//是否显示分页(*)
onlyInfoPagination:true,//设置为true只显示总数据数,而不显示分页按钮。需要pagination='True'
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",
这里我没有用bootstrap自带的分页按钮,我是自己用jq写的按钮组,在下一篇文章我会把按钮代码贴出来,这样可自定义的程度会高一些~ 你也可以直接用bootstraptable子带的分页按钮,把配置改下就好。
以上所述是小编给大家介绍的SpringMvc+Mybatis+Pagehelper分页详解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!
