When writing projects using SSM framework, sometimes the data obtained from the database is too large and will not be displayed in the web page. In order to avoid this situation, data paging can be used to effectively manage data.
What's wrong? I hope you guys can correct me!
1. First look at the effect
2. To complete the paging function, it is necessary to correspond to four functions: first page, previous page, next page and last page
To realize this function, you need to define the following variables in the entity class:
private int pageSize = 10;//How much data per page private int pageCount; //Total pages private int total; //Total data private int nowPage=1; //Current page number private int offset; //First data subscript on each page
My entity class here is named PageService.java (it may not be accurate, just understand it)
To establish the corresponding GetSet method, we need to pay attention to setTotal and setNowPage. Other methods can be generated by default.
public void setTotal(int total) { //Total set data pageCount = total/pageSize; //Total number of pages = total number of data / number of data per page (rounded to the last) if(total%pageSize!=0){ //If the remainder is not 0, it indicates that there is additional data, and the number of pages needs to be increased by one pageCount++; } this.total = total; //Total number of data assigned to }
public void setNowPage(int nowPage) { //Set current page number if(nowPage<=1){ //Ensure that the number of pages is not less than 1 nowPage = 1; } offset = (nowPage-1)*pageSize;//The subscript algorithm of the first data on each page. The subscript of the first number on the first page is 0, the subscript of the first data on the second page is 10... And so on this.nowPage = nowPage;//Copy to current page number }
3. Then you need to write corresponding sql statements in xxxMapper.xml
<select id="selectAllByPage" parameterType="com.hospital.pojo.PageService" resultType="com.hospital.pojo.Patient"> select pid,name,age from patient ORDER BY pid ASC LIMIT #{offset},#{pageSize} </select>
In parameterType, write the entity class with paging related variables defined above
Write the entity class variable corresponding to the data queried by sql in resultType
ORDER BY pid ASC is arranged in ascending order according to pid
LIMIT #{offset},#{pageSize} means that 10 data displays are limited from the first data subscript of each page, which means that one page displays pageSize (the ten data I define here)
4. Write in the corresponding xxmapper.java
List<XXX> selectAllByPage(PageService pageService);
Note the value of the parameter. XXX refers to the entity class corresponding to your database table
5. Write in xxservice interface
List<XXX> selectAllByPage(PageService pageService);
Consistent with the above xxxMapper.java, XXX refers to the entity class corresponding to your database table
6. Write in the implementation class of xxserviceimpl.java
@Service("xxxService") //In the outermost layer public class xxxServiceImpl implements xxxService{ @Autowired private xxxMapper xxxMapper; @Override public List<Patient> selectAllByPage(PageService pageService) { return patientMapper.selectAllByPage(pageService); }
7. Write the corresponding implementation function in the Controller class
@Controller public class xxxController { @Autowired private XxxService xxxService; @RequestMapping("search") public String search(Integer nowpage,Model model){ PageService page = new PageService();//Create an object to manage paging if(nowpage==null){//Make sure the current page number is not empty nowpage=1; } page.setNowPage(nowpage); page.setTotal(xxxService.getPatientCount(page));//Get the total number of data. I'll put the corresponding sql below model.addAttribute("total",page.getTotal()); model.addAttribute("pageCount",page.getPageCount()); model.addAttribute("nowPage",page.getNowPage()); model.addAttribute("pageSize",page.getPageSize()); model.addAttribute("offset",page.getOffset()); List<XXX> list = xxxService.selectAllByPage(page); model.addAttribute("list",list); model.addAttribute("errorCount",xxxService.getPatientErrorCount(page));//Get the total number of invalid data. I put the corresponding sql below. I just think that the data with empty name is invalid data return "Test.jsp"; } }
Write relevant sql statements in xxxMapper.xml to realize the total number of query data and the number of invalid data
<select id="getPatientCount" parameterType="com.hospital.pojo.PageService" resultType="int"> select count(*) from patient where name!="" </select> <select id="getPatientErrorCount" parameterType="com.hospital.pojo.PageService" resultType="int"> select count(*) from patient where name is null </select>
Synchronize with the corresponding xxxMapper.java, xxxService.java and xxxServiceImpl.java, similar to the above. Note the value represented by the parameter here..
8. Finally, write in the jsp page
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> </head> <body> <form method="post" action="search.action"> <input type="submit" value="Query all patient information"/> </form> <hr> <form> <table> <tr><td>number</td><td>full name</td><td>Age</td></tr> <c:forEach items="${list }" var="patient"> <tr> <td><input type="text" value="${patient.pid }"/></td> <td><input type="text" value="${patient.name }"/></td> <td><input type="text" value="${patient.age }"/></td> </tr> </c:forEach> </table> <a href="searchPatient.action?nowpage=1">home page</a> <a href="searchPatient.action?nowpage=${nowPage-1 }">previous page</a> <c:if test="${nowPage<pageCount }"> <a href="searchPatient.action?nowpage=${nowPage+1 }">next page</a> </c:if> <c:if test="${nowPage>=pageCount }"> <a href="searchPatient.action?nowpage=${pageCount }">next page</a> </c:if> <a href="searchPatient.action?nowpage=${pageCount }">Last page</a> How many pages should it be divided into: ${pageCount }<br> Current pages: ${nowPage }<br> Current page subscript: ${offset }<br> How much data per page: ${pageSize }<br> Total data quantity: ${total }<br> Number of invalid data: ${errorCount }<br> </form> </body> </html>
I have deleted everything irrelevant to paging in the jsp page, and the rest should be understandable.
Finally, I attach my project structure chart:
I hope I can help you!