Paging plugin PageHepler
In the article on the realization of the paging function, the article portal: SpringBoot case-employee management-paging query-implementation_entropy240's blog-CSDN blog
The main code to implement the paging function is as follows:
Mapper interface
package com.example.tlias.mapper;
import com.example.tlias.pojo.Emp;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface EmpMapper {
/**
* 获取数据总数
*
* @return
*/
@Select("select count(*) from emp")
public long CountAll();
/**
* 获取每页数据信息
*
* @return
*/
@Select("select * from emp limit #{start},#{pageSize}")
public List<Emp> GetData(Integer start, Integer pageSize);
}
business realization layer
package com.example.tlias.service.impl;
import com.example.tlias.mapper.EmpMapper;
import com.example.tlias.pojo.Emp;
import com.example.tlias.pojo.PageBean;
import com.example.tlias.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class EmpServiceImpl implements EmpService {
@Autowired
private EmpMapper empMapper;
@Override
public PageBean Page(Integer page, Integer pageSize) {
// 获取总记录数
long count = empMapper.CountAll();
Integer start = page * pageSize - pageSize;
// 获取分页查询结果列表
List<Emp> list = empMapper.GetData(start, pageSize);
// 封装PageBean对象
PageBean pageBean = new PageBean(count, list);
return pageBean;
}
}
Using the MyBatis paging plugin PageHepler, you can complete the functions of counting the total number of records and specifying paging parameters . We only need to perform normal query functions . For more information on the PageHepler plugin, see : How to use the paging plugin (pagehelper.github.io) https: //pagehelper.github.io/docs/howtouse/
The specific key code is as follows
Mapper interface
package com.example.tlias.mapper;
import com.example.tlias.pojo.Emp;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface EmpMapper {
// todo 原始分页代码
// /**
// * 获取数据总数
// *
// * @return
// */
// @Select("select count(*) from emp")
// public long CountAll();
//
// /**
// * 获取每页数据信息
// *
// * @return
// */
// @Select("select * from emp limit #{start},#{pageSize}")
// public List<Emp> GetData(Integer start, Integer pageSize);
/**
* 员工信息的查询
* 使用pagehepler插件
*
* @return
*/
@Select("select * from emp")
public List<Emp> list();
}
business realization layer
package com.example.tlias.service.impl;
import com.example.tlias.mapper.EmpMapper;
import com.example.tlias.pojo.Emp;
import com.example.tlias.pojo.PageBean;
import com.example.tlias.service.EmpService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class EmpServiceImpl implements EmpService {
@Autowired
private EmpMapper empMapper;
@Override
// todo 原始方法代码
// public PageBean Page(Integer page, Integer pageSize) {
// // 获取总记录数
// long count = empMapper.CountAll();
// Integer start = page * pageSize - pageSize;
// // 获取分页查询结果列表
// List<Emp> list = empMapper.GetData(start, pageSize);
// // 封装PageBean对象
// PageBean pageBean = new PageBean(count, list);
// return pageBean;
//
// }
// todo 使用pageHepler插件代码
public PageBean Page(Integer page, Integer pageSize) {
// 设置分页参数
PageHelper.startPage(page, pageSize);
// 执行正常查询操作
List<Emp> empList = empMapper.list();
Page<Emp> p = (Page<Emp>) empList;
// 封装分页结果PageBean
PageBean pageBean = new PageBean(p.getTotal(), p.getResult());
return pageBean;
}
}
In the business implementation layer, first call the startPage() method in the PageHepler plug-in, set the query page number (page) and page capacity (pageSize), then call the list() method in the Mapper interface to obtain all the result sets, and then store the results The collection performs type conversion, and finally encapsulates the query results. The parameters can be set by directly calling the gettotal() and getResult() methods in pageHepler.
The gettotal() method obtains the total data capacity, and the executed SQL statement is: select count(*) from data table
The getReault() method obtains the data list displayed on the page: the executed SQL statement is: select * from data table limit #{start}, #{pageSize}, where the start parameter and the passed page parameter are calculated.
run test
Restart the SpringBoot project, and still start postman for testing. The request address and request parameters are as follows:
The result of the operation is:
{
"code": 1,
"msg": "success",
"data": {
"total": 17,
"rows": [
{
"id": 1,
"username": "jinyong",
"password": "123456",
"name": "金庸",
"gender": 1,
"image": "1.jpg",
"job": 4,
"entrydate": "2000-01-01",
"deptId": 2,
"creteTime": null,
"updateTime": "2023-08-07T15:44:50"
},
{
"id": 2,
"username": "zhangwuji",
"password": "123456",
"name": "张无忌",
"gender": 1,
"image": "2.jpg",
"job": 2,
"entrydate": "2015-01-01",
"deptId": 2,
"creteTime": null,
"updateTime": "2023-08-07T15:44:50"
},
{
"id": 3,
"username": "yangxiao",
"password": "123456",
"name": "杨逍",
"gender": 1,
"image": "3.jpg",
"job": 2,
"entrydate": "2008-05-01",
"deptId": 2,
"creteTime": null,
"updateTime": "2023-08-07T15:44:50"
}
]
}
}
We did not write the above two SQL statements, which are automatically executed by the PageHepler plug-in to obtain the required parameters
summary
- Introduce dependencies
- use
- // Set paging parameters
PageHelper.startPage(pageNum, pageSize);
// Execute normal query operation
List<Emp> empList = empMapper.list();
Page<Emp> p = (Page<Emp>) empList;
- // Set paging parameters