Recently, the company needs to do an alarm page function, which requires pagination. After checking a lot of information, it found that PageHelper is more suitable.
Therefore, I wrote a tutorial on using PageHelper starting from scratch, and also recorded what I had been busy with for a day.
1. First, you need to add the dependency of PageHelper in the project. Here I added it with Maven.
<dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>4.1.6</version> </dependency>
2. Add the configuration of pagehelper in mybatis configuration file
<configuration> <plugins> <!-- com.github.pagehelper is the package name where the PageHelper class is located --> <plugin interceptor="com.github.pagehelper.PageHelper"> <!-- This parameter can be set without setting in versions 4.0.0---- This parameter is false by default --> <!-- When set to true, the first parameter offset of RowBounds will be used as the pageNum page number --> <!-- The effect is the same as the pageNum in startPage--> <property name="offsetAsPageNum" value="true"/> <!-- This parameter defaults to false --> <!-- When set to true, count query will be performed using RowBounds pagination --> <property name="rowBoundsWithCount" value="true"/> <!-- When set to true, if pageSize=0 or RowBounds.limit = 0, all results will be queryed --> <!-- (Equivalent to the fact that the page query is not executed, but the result is still Page type) --> <property name="pageSizeZero" value="true"/> <!-- Version 3.3.0 is available - paging parameters are rationalized, false is disabled by default --> <!-- When rationalization is enabled, if pageNum<1 will query the first page, if pageNum>pages will query the last page --> <!-- When rationalization is disabled, if pageNum<1 or pageNum>pages will return empty data --> <property name="reasonable" value="true"/> <!-- Version 3.5.0 is available - In order to support the startPage(Object params) method --> <!-- Added a `params` parameter to configure the parameter mapping to get values from Map or ServletRequest --> <!-- You can configure pageNum, pageSize, count, pageSizeZero, reasonable, orderBy, and do not configure the default value for mapping --> <!-- If you do not understand this meaning, do not copy the configuration casually --> <!-- <property name="params" value="pageNum=start;pageSize=limit;"/> --> <!-- Supports passing paging parameters through Mapper interface parameters --> <property name="supportMethodsArguments" value="true"/> <!-- always return PageInfo type, check check whether the return type is PageInfo, and none returns Page --> <property name="returnPageInfo" value="check"/> </plugin> </plugins> </configuration>
3. Add a PageBean class to store paged information
public class PageBean<T> implements Serializable { private static final long serialVersionUID = 1L; private long total; //Total number of records private List<T> list; //Result set private int pageNum; //How many pages private int pageSize; //Number of records per page private int pages; //Total number of pages private int size; //Num of current page <=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, and find out the data we need
<select id="selectallList" parameterType="com.alarm.bean.AlarmParamModel" resultMap="AlarmMap"> select message_id, seqnum, message_type, process_status, distribution_status, processor, occur_time, close_time, system_id, group_id, warning_level, message_content from td_alarm_info </select>
5. Mapper interface method
public List<AlarmParamModel> selectallList(AlarmParamModel model);
6. Service interface method
Datagrid selectallList(AlarmParamModel model,int pageNum, int pageSize);
7. Service implementation class
It is important to note here that it is the main logic of pagination. pageNum represents the page number, pageSize represents the number of displayed pages, startPag method is the initial page, orderBy method is to sort the data according to a certain field, here I use the descending order of occr_time (desc)
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; }8. I noticed that I used a Datagrid class here, which is a class used to pass data to the front desk, including total (total) and rows (data)
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; } }9. Start writing the controller layer and call the method I wrote before
It should be noted here that offset and limit are the page numbers sent from the front desk and the number of displayed pages per page. Different from the offset and limit of bootstraptable, the offset represents the offset, that is, if 10 pieces of data are displayed per page, the offset represented by the second page in bootstrap is 10, and the first page and the third page are 0 and 20 respectively. And my offset here refers to pageNum.
@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; }10. Now the front desk request can get backend data and paginate it. I will post the configuration of my front desk bootstrap table.
$('#tb_departments').bootstrapTable({ url: 'http://10.1.234.134:8088/api/AlarmInfo/list', //Request URL (*) method: 'get', //Request method (*) striped: false, //Whether to display line interval color cache: false, //Whether to use cache, default is true, so in general, this property needs to be set (*) pagination: true, //Whether to display paging (*) onlyInfoPagination: true, //Set to true to display only the total number of data, not the pagination button. pagination='True' sortable: true, //Whether to enable sortOrder: "asc", //Sorting method queryParams: oTableInit.queryParams,//Pagination parameters (*) sidePagination: "server", //Pagination method: client client pagination, server server pagination (*) pageNumber:1, //Initialize the first page to load, default first page pageSize: 10, //Number of record rows per page (*) pageList: [10, 25, 50, 100], //Number of rows per page to select (*) search: false, //Does the table search be displayed? This search is a client search and will not enter the server, so I personally feel that it is of little significance to strictSearch: true, showColumns: false, //Does all columns be displayed showRefresh: false, //Does the refresh button display minimumCountColumns: 2, //The minimum number of columns allowed clickToSelect: true, //Where to enable click to select row checkboxHeader: true, //Add height: 500, //Line height, if the height attribute is not set, the table automatically feels the height of the table according to the number of records, uniqueId: "id", //The unique identification of each row is generally the primary key column showToggle: false, //Does the toggle button for detailed view and list view cardView: false, //Does the detailed view detailView: true, detailFormatter:detailFormatter , paginationHAlign:"left", paginationDetailHAlign:"right", I did not use the paging buttons that come with bootstrap. I wrote the button group myself in jq. In the next article, I will post the button code, so that it can be customized to a higher degree~ You can also use the paging buttons with bootstraptable sub-band and just change the configuration.
The above is a detailed explanation of the SpringMvc+Mybatis+Pagehelper pagination introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!