SSM framework complete paging query

SSM framework complete paging query

BookDaoMapper.java

/** * Dynamically query book information based on multiple conditions (book classification, book name, whether to borrow or not) * * @param bookType * Book classification * @param bookName * Book name * @param isBorrow * Whether to borrow * @param startQuery * Query from the first few * @param pageSize * The number displayed on each page * @return */ List<Book_info> findBookByConditions ( @Param("bookType") Integer bookType, @Param("bookName") String bookName, @Param("isBorrow") Integer isBorrow, @Param("startQuery") Integer startQuery , @Param("pageSize") Integer pageSize) ; /** * According to multiple conditions (book classification, book name, whether to borrow or not) dynamically query the number of book information * * @param bookType * Book classification * @param bookName * Book name * @param isBorrow * Whether to borrow * @return */ Integer findCountByConditions ( @Param("bookType") Integer bookType, @Param("bookName") String bookName, @Param("isBorrow") Integer isBorrow) ; Copy code

BookDaoMapper.xml

<!-- The full path name of the interface --> <mapper namespace= "com.bookssys.dao.BookDaoMapper" > <!-- Use collection for one- to-many, and association for many-to- one --> <resultMap type= "Book_info" id= "BookInfoBookType" > <!-- The primary key column property of the main table indicates the attribute name in the entity class column indicates the alias of the corresponding column --> <id property= "book_id" column= "bookid"/> <!-- The data found in the sql statement, if there is an alias in the sql statement, the column is filled with the alias, and the property represents the attribute name in the entity class --> <result property= "book_code" column= "bookcode"/> <result property= "book_name" column= "bookname"/> <result property= "book_author" column= "bookauthor"/> <result property= "publish_press" column= "publishpress"/> <result property= "is_borrow" column= "isborrow"/> <!-- The main table is written in association --> <association property= "bookType" javaType= "Book_type" > <id property= "id" column= "typeid"/> <result property= "type_name" column= "typename"/> </association> </resultMap> <!-- id must be consistent with the method name. The resultMap in select must be the same as the id in resultMap, which means that the resultMap of this name is called Or select the full path of the entity class --> <!-- Dynamically query book information based on multiple conditions (book classification, book name, whether to borrow or not) --> <select id= "findBookByConditions" resultMap= "BookInfoBookType" > SELECT bi.book_id AS bookid, bt.id AS typeid, bi.book_code AS bookcode, bt.type_name AS typename, bi.book_name AS bookname, bi.book_author AS bookauthor, bi.publish_press AS publishpress, bi.is_borrow AS isborrow FROM book_info AS bi, book_type AS bt <!-- Use if +trim to realize multi-condition query P78 example on P79 page --> <trim prefix= "WHERE" prefixOverrides= "and|or" > bi.book_type=bt.id < if test= "bookType !=null and bookType>0" > <!-- The bookType in test is the annotation 0 in the method in Dao is the book type in index.jsp (please select) --> AND bt.id=#{bookType} </if > < if test= "bookName!=null and bookName!=''" > AND bi. book_name LIKE CONCAT ( '%' ,#{bookName}, '%' ) </if > < if test = "isBorrow!=null and isBorrow!=-1" > AND bi.is_borrow=#{isBorrow} </if > </trim> limit #{startQuery},#{pageSize} </select> <!-- According to multiple conditions (book classification, book name, whether to borrow or not) dynamically query the number of book information --> <select id= "findCountByConditions" resultType= "int" ><!-- The return type of resultType is Integer, write int or java.lang.Integer directly --> SELECT COUNT (bi.book_id) FROM book_info AS bi, book_type AS bt <!-- Use if +trim to realize multi-condition query P78 example on P79 page --> <trim prefix = "WHERE" prefixOverrides= "and|or" > bi.book_type=bt.id < if test= "bookType !=null and bookType>0" > <!-- The bookType in test is the annotation 0 in the method in Dao is the book type in index.jsp (please select) --> AND bt.id=#{bookType} </if > < if test= "bookName!=null and bookName!=''" > AND bi. book_name LIKE CONCAT ( '%' ,#{bookName}, '%' ) </if > < if test = "isBorrow!=null and isBorrow!=-1" > AND bi.is_borrow=#{isBorrow} </if > </trim> </select> </mapper> Copy code

 PageUtil.java

package com.bookssys.util; /** * Paging tools * * @author Administrator * */ public class PageUtil { /** * Public method to calculate the total number of pages * * @param totalSize * Total number of information * @param pageSize * Number of items per page * @return */ public static final Integer calTotalPage (Integer totalSize, Integer pageSize) { int totalPage = 1 ; //Calculate the total number of pages totalPage = (totalSize% pageSize == 0 )? (totalSize/pageSize) : (totalSize/pageSize + 1 ); return totalPage; } /** * Page number control method * * @param pageIndex * current page number * @param totalPage * Total number of pages * @return */ public static final Integer checkPageIndex (Integer pageIndex, Integer totalPage) { //Page number control if (pageIndex < 1 ) {//The current page number cannot be less than the minimum page number pageIndex = 1 ; } else if (pageIndex> totalPage) { //The current number of pages cannot be greater than the maximum number of pages pageIndex = totalPage; } return pageIndex; } } Copy code

 BookController.java

package com.bookssys.controller; import java.io.UnsupportedEncodingException; import java.util.Date; import java.util.List; import javax.annotation.Resource; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web .bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import com.bookssys.biz.BookBiz; import com.bookssys.entity.Book; import com.bookssys.biz.BookTypeBiz; import com.bookssys.entity.BookType; import com.bookssys.util.PageUtil; @Controller public class BookController { /*Inject business*/ @Resource private BookBiz bookBiz; /*A business must be re-injected every time a biz is defined*/ @Resource private BookTypeBiz bookTypeBiz; private final Integer pageSize = 5 ; /*Display 5 pieces of data per page*/ /** * Dynamically query book information based on multiple conditions * * @param bookType * Book classification * @param bookName * Book name * @param isBorrow * Whether to borrow * @param model * @return */ /* A controller*/ @RequestMapping(value = "findBookByConditions.html") public String findBookinfoByConditions ( //@RequestParam(required = false) means this is not a required item, it can be empty, no parameters are passed over @RequestParam(required = false) Integer bookType, @RequestParam(required = false,defaultValue="") String bookName, /*defaultValue="" Given a default value, the default is empty, otherwise an error will be reported*/ @RequestParam(required = false) Integer isBorrow, @RequestParam(required = false,defaultValue="1") Integer pageIndex,//Yes The first page will not be uploaded, and the first page will be displayed by default when the first page is not passed. Model model) { //Before converting the code search, you must first convert the code try { bookName = new String(bookName.getBytes( "iso-8859-1" ), "utf-8" ); } catch (UnsupportedEncodingException e) { //TODO Auto-generated catch block e.printStackTrace(); } //Query the total number of eligible information int totalSize=bookBiz.findCountByConditions(bookType, bookName, isBorrow); int totalPage = 0 ; //Total number of pages //This array is used to display the number of specific pages, which is convenient for users to jump int [] pageArray = null ; if (totalSize> 0 ) { //Only paging is performed when the total number of entries is greater than 0. //Calculate the total number of pages. Calculation method of public class totalPage=PageUtil.calTotalPage(totalSize, pageSize); //Page number control calls the public class pageIndex=PageUtil.checkPageIndex(pageIndex, totalPage); /*Get all the book information and save the page display*/ List<Book> infoList = bookBiz.findBookinfoByConditions( bookType, bookName, isBorrow,(pageIndex- 1 )*pageSize,pageSize); model.addAttribute( "infoList" , infoList); pageArray = new int [totalPage]; //The length of the defined array is the same as the total number of pages } /*Get all types of books and save them*/ List<Book_type> typeList=bookTypeBiz.findAllBookType(); model.addAttribute( "typeList" , typeList); model.addAttribute( "bookName" , bookName); model.addAttribute( "bookType" , bookType); model.addAttribute( "isBorrow" , isBorrow); model.addAttribute( "pageIndex" , pageIndex); model.addAttribute( "pageArray" , pageArray); return "index" ; } } Copy code

 WEB-INF/jsp/index.jsp

<%@ 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 HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" > < html > < head > < meta http- equiv = "Content-Type" content = "text/html; charset=UTF-8" > < title > Book information </title > </head > < body > <% String path = request.getContextPath() + "/"; %> < form action = "sec/findBookByConditions.html" > Book category: < select name = "bookType" > <!-- The bookType in the name must be consistent with the conditions in the Controller --> < option value = "0 " > ---Please select --- </option > < c:forEach items = "${typeList }" var = "type" > < option value = "${type.id }" < c:if test = "${type.id==bookType }" ><!-- Echo the selected book type: When the current book category is equal to the book type saved in the controller, display the currently selected book type --> selected="selected" </c:if > >${type.type_name } </option > </c:forEach > </select > Book name: < input type = "text" name = "bookName" value = "${bookName }"/> <!-- Data echo--> Whether to borrow: < select name = "isBorrow" > < option value = "-1" > ---Please select --- </option > < option value = "0" < c:if test ="${isBorrow==0 }" > selected="selected" </c:if > >Can borrow </option > < option value = "1" < c:if test = "${isBorrow==1 }" > selected="selected" </c:if > >Borrowed </option > </select > < input type = "submit" value = "Query"/> </form > < table border = "1" width = "660px" > < tr > < td > Book number </td > < td > Book classification </td > < td >Book name </td > < td > Author </td > < td > Publisher </td > < td > Operation </td > </tr > < c:forEach items = "${infoList }" var = "info" > < tr > < td > ${info.book_code} </td > < td > ${info.bookType.type_name} </td > <! - bookType is an object in the entity class --> <td > ${info.book_name} </td > < td > ${info.book_author} </td > < td > ${info.publish_press} </td > < td > < c:if test = "${ == 0} info.is_borrow " > < A the href = " JavaScript: void (0) " class = " borrowStatus " book_id = " $ {} info.book_id " > application borrow </A > </C:if > <c:if test = "${info.is_borrow==1 }" > < span > Borrowed </span > </c:if > </td > </tr > </c:forEach > </table > < div > <!-- Query the corresponding information on the premise with conditions. Click the previous page or the next page to display the corresponding information instead of displaying the previous or next page information of all books --> <%-- When the interceptor clicks on the previous or next page, the address will not repeat the/sec method: 1. Add ${pageContext.request.contextPath} before/sec --%> < a href = "${pageContext.request.contextPath }/findBookByConditions.html?pageIndex=${pageIndex-1} &bookType=${bookType}&bookName=${bookName}&isBorrow=${isBorrow}" > Previous page </a >   <!-- Traverse the number of displayed pages--> < c:forEach items = "${pageArray }" varStatus = "i" > < a href = "${pageContext.request.contextPath }/findBookByConditions.html?pageIndex=${i.index+1} &bookType=${bookType}&bookName=${bookName}&isBorrow=${isBorrow}" > ${i.index+1 } </a >   </c:forEach > < a href = "${pageContext.request.contextPath }/findBookByConditions.html?pageIndex=${pageIndex+1} &bookType=${bookType}&bookName=${bookName}&isBorrow=${isBorrow}" > Next page </a > </div > < script type = "text/javascript" src = "statics/js/jquery-1.8 .3.js" > </script > </body > </html > Copy code