Recently, the company needs to build an alarm page function, which requires paging. After checking a lot of information, I found that PageHelper is more suitable.
So I wrote a tutorial on using PageHelper from scratch, and also recorded what I did during the busy day.
1. First, you need to add the dependency of PageHelper to the project. Here I use Maven to add
<dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>4.1.6</version> </dependency>
2. Add the configuration of pagehelper in the mybatis configuration file
<configuration> <plugins> <!-- com.github.pagehelper为PageHelper类所在包名 --> <plugin interceptor="com.github.pagehelper.PageHelper"> <!-- 4.0.0以后版本可以不设置该参数 --> <property name="dialect" value="mysql"/> <!-- 该参数默认为false --> <!-- 设置为true时,会将RowBounds第一个参数offset当成pageNum页码使用 --> <!-- 和startPage中的pageNum效果一样--> <property name="offsetAsPageNum" value="true"/> <!-- 该参数默认为false --> <!-- 设置为true时,使用RowBounds分页会进行count查询 --> <property name="rowBoundsWithCount" value="true"/> <!-- 设置为true时,如果pageSize=0或者RowBounds.limit = 0就会查询出全部的结果 --> <!-- (相当于没有执行分页查询,但是返回结果仍然是Page类型)--> <property name="pageSizeZero" value="true"/> <!-- 3.3.0版本可用 - 分页参数合理化,默认false禁用 --> <!-- 启用合理化时,如果pageNum<1会查询第一页,如果pageNum>pages会查询最后一页 --> <!-- 禁用合理化时,如果pageNum<1或pageNum>pages会返回空数据 --> <property name="reasonable" value="true"/> <!-- 3.5.0版本可用 - 为了支持startPage(Object params)方法 --> <!-- 增加了一个`params`参数来配置参数映射,用于从Map或ServletRequest中取值 --> <!-- 可以配置pageNum,pageSize,count,pageSizeZero,reasonable,orderBy,不配置映射的用默认值 --> <!-- 不理解该含义的前提下,不要随便复制该配置 --> <!-- <property name="params" value="pageNum=start;pageSize=limit;"/> --> <!-- 支持通过Mapper接口参数来传递分页参数 --> <property name="supportMethodsArguments" value="true"/> <!-- always总是返回PageInfo类型,check检查返回类型是否为PageInfo,none返回Page --> <property name="returnPageInfo" value="check"/> </plugin> </plugins> </configuration>
3. Add a PageBean class to store paging information
public class PageBean<T> implements Serializable { private static final long serialVersionUID = 1L; private long total; //总记录数 private List<T> list; //结果集 private int pageNum; //第几页 private int pageSize; //每页记录数 private int pages; // 总页数 private int size; //当前页的数量<=pageSize public PageBean(List<T> list){ if (list instanceof Page){ 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(); } } public long getTotal() { return total; } public void setTotal(long total) { this.total = total; } public List<T> getList() { return list; } public void setList(List<T> list) { this.list = list; } public int getSize() { return size; } public void setSize(int size) { this.size = size; } public int getPageNum() { return pageNum; } public void setPageNum(int pageNum) { this.pageNum = pageNum; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public int getPages() { return pages; } public void setPages(int pages) { this.pages = pages; } }
The following is the business logic code
4. First start from the mapper.xml file, operate the database sql, check Output the data we need
<select id="selectallList" parameterType="com.alarm.bean.AlarmParamModel" resultMap="AlarmMap"> select message_id, seqnum, message_type, process_status, distribute_status, processor, occur_time, close_time, system_id, group_id, warn_level, message_content from td_alarm_info </select>
5. Mapper’s interface method
public List<AlarmParamModel> selectallList(AlarmParamModel model);
##6.service’s interface method
Datagrid selectallList(AlarmParamModel model,int pageNum, int pageSize);
public Datagrid selectallList(AlarmParamModel model,int pageNum, int pageSize){ PageHelper.startPage(pageNum, pageSize); PageHelper.orderBy("occur_time desc"); List<AlarmParamModel> list = this.alarmMgrMapper.selectallList(model); PageInfo<AlarmParamModel> pageInfo = new PageInfo<AlarmParamModel>(list); Datagrid datagrid = new Datagrid(pageInfo.getTotal(),pageInfo.getList()); return datagrid; }
public class Datagrid { private long total; private List rows = new ArrayList<>(); public Datagrid() { super(); } public Datagrid(long total, List rows) { super(); this.total = total; this.rows = 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; } }
@RequestMapping(value = "/AlarmInfo/list", method = {RequestMethod.GET,RequestMethod.POST}) @ResponseBody public Datagrid alarmInfo(AlarmParamModel model,@RequestParam(value="offset",defaultValue="0",required=false)Integer pageNum, @RequestParam(value="limit",defaultValue="10",required=false)Integer pageSize) { Datagrid datagrid = this.alarmMgrService.selectallList(model,pageNum, pageSize); return datagrid; }
$('#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",