Java paging data through laypage plug-in of layui

Keywords: Front-end Mybatis xml Spring encoding

Recently, you need to paginate the data on a page of spring MVC + mybatis project. The general idea is to create two hidden input boxes on the page to save the pageNum of the current page and the number of pieces of data displayed on each page pageSize. Modify the corresponding values each time you paginate, and then transfer the pageNum and pageSize to the background pageHelper

1. MyBatis layer (xml)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-// Mapper 3.0//EN" 
<mapper namespace="com.zghky.dao.***">
	<cache-ref namespace="com.zghky.dao.***"/>
	<resultMap id="getTestMap" type="Op">
		<id column="id" property="id" />
		<result column="geography_position" property="account.geographyPosition"/>
		<result column="op_type" property="opType"/>
		<result column="op_time" property="opTime"/>
		<result column="op_table_name" property="opTableName"/>
		<result column="op_record_id" property="opRecordId"/>
		<result column="op_record_name" property="opRecordName"/>
		<result column="user_id" property=""/>
		<result column="user_name" property="account.userName"/>
	<select id="getOperationLog" resultMap="getTestMap">
    	select, t.user_id, t.geography_position, t.op_type, 
		t.op_time, t.op_table_name, t.op_record_id, 
		t.op_record_name, a.user_name from op t left join 
		account a on t.user_id = where t.op_record_name is not null 
		order by desc

Note: (1) in resultMap, although the SQL statement is similar to and t.op'u type, the column should be written as ID and opType, because the database field name queried is still ID and opType;

(2) Because I have two properties in the test table: account and otherField are the linkage of the other two tables, that is, to store all the properties of the two tables

The two properties pageNum and pageSize are in the otherField table, and the userId, userName and geography  position properties are in the account table

Therefore, to write the corresponding data storage field in the resultMap, you need to set the property to and account.geography  position;

private Account account;
private OtherField otherField = new OtherField();

public Account getAccount() {
	return account;

public void setAccount(Account account) {
	this.account = account;

public OtherField getOtherField() {
	return otherField;

public void setOtherField(OtherField otherField) {
	this.otherField = otherField;

(3) The value after order by should be unique. If it is repeated, some data will not be displayed in the paging;

(4) Set the data display not to be set here in MyBatis, such as where op.operation "record" name is not null above, so that the number of displayed data in paging is not correct;

(5) The id of < resultMap > should be the same as that of < Select > where the type of < resultMap > is set to save the table of query data.

2. Dao layer

public interface OpMapper {
	public List<Op> getOp();

3. Service layer

public class OperationLogService {

	private OpMapper opMapper;
	@Transactional(readOnly = true)
	public PageInfo<Op> getOp(Op op){
		List<Op> ops = opMapper.getOp();
		PageInfo<Op> opInfo = new PageInfo<>(ops);
		return opInfo;

4. Controller layer

public class OpController {

	private OpService opService;
	@RequestMapping(value = "/getOp", method = RequestMethod.POST)
	public PageInfo<Op> getOp(String page, String size) {
		Op op = new Op();
		OtherField otherField = new OtherField();
		int pages = Integer.parseInt(page);
		int sizes = Integer.parseInt(size);
		return opService.getOp(op);

5. Front end html

<div class="content">
	<div id="manager">
		<div id="list-manager">
			<table id="op-list" class="table table-hover list-manager">
				<tbody id="tbody"></tbody>
		<!-- Paged navigation -->
		<div id="page"></div>
		<!-- Hide the current page and number of items, with an initial value of 1,10 -->
		<input type="hidden" id="currPage" value="1">
		<input type="hidden" id="limit" value="10">

It mainly sets two input boxes to save the current page and limit the number of entries per page

<input type="hidden" id="currPage" value="1">
<input type="hidden" id="limit" value="10">

6. Front end js

var total = "";
	total = "";

/*Request data*/
function getOpInfo(){
	var page = $("#currPage").val();
	var size = $("#limit").val();
		url: path + 'op/getOp',
		type: "post",
		async: false,
		dataType: 'json',
		data: {
			"page": page,
			"size": size
		success: successFunction,
		error: errorFunction

/*Data request successful*/
function successFunction(data){
	$("#currPage").val(; / / reassign the current page
    $("#limit").val(data.limit); / / reassign the number of entries on this page
    var page =;
    var limit = data.limit;
    total =;
    var html = '';
    if(data.list.length > 0) {
    	$.each(data.list, function(index, item) {
    		// Display data processing
            html += '......';
    } else {
    	html += '<tr><td colspan="7">No data available</td></tr>';

/*Data request failed*/
function errorFunction(){
	var html = '<tr><td colspan="7">No data available</td></tr>';

/*Time stamp date mm / DD / yyyy HHM / S*/
function formatDateTime(time, format){
	var t = new Date(time);
	var tf = function(i){
		return (i < 10 ? '0' : '') + i
	return format.replace(/yyyy|MM|dd|HH|mm|ss/g, function(a){
			case 'yyyy':
				return tf(t.getFullYear());
			case 'MM':
				return tf(t.getMonth() + 1);
			case 'mm':
				return tf(t.getMinutes());
			case 'dd':
				return tf(t.getDate());
			case 'HH':
				return tf(t.getHours());
			case 'ss':
				return tf(t.getSeconds());

function toPage(){
	layui.use('laypage', function(){
		var laypage = layui.laypage;
			elem: 'page',
			limits: [10, 20, 30, 40, 50, 100, 200, 300, 400],
            count: total,
            layout: ['prev', 'page', 'next', 'limit', 'skip', 'count'],
            jump: function(data, first){
                if(!first){// Called when you click Page in the lower right corner

Posted by w00kie on Thu, 07 May 2020 07:05:21 -0700