BookDaoMapper.java

/** * Dynamically query book information based on multiple criteria (book category, book name, whether to borrow) **@paramBookType * Book category *@paramBookName * bookName *@paramIsBorrow * Whether to borrow *@paramStartQuery * Where to start the query *@paramPageSize * Number of pages to display *@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);
/** * Dynamically query the number of books according to multiple conditions (book classification, book name, whether to borrow) **@paramBookType * Book category *@paramBookName * bookName *@paramIsBorrow * Whether to borrow *@return* /
	Integer findCountByConditions(@Param("bookType") Integer bookType,
			@Param("bookName") String bookName,
			@Param("isBorrow") Integer isBorrow);
Copy the code

BookDaoMapper.xml

<! -- Full path name of the interface --> < namespace="com.bookssys.dao.BookDaoMapper"> <! --1Use collection when you have more than one pair1Use association --> <resultMap type="Book_info" id="BookInfoBookType"> <! --> <id property= --> <id property="book_id" column="bookid"/ > <! --> <result property= --> <result property= --> <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"/ > <! -- Primary table in association --> <association property="bookType" javaType="Book_type"> 
			<id property="id" column="typeid" />
			<result property="type_name" column="typename"/> </association> </resultMap> <! The id in the select resultMap must be the same as the ID in the resultMap, indicating that the name of the resultMap is called or the full path of the entity class is selected --> <! --> <select id= (select id, id, id, id, 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 <! - the use ofifP78 Example on page P79 --> <trim prefix="WHERE" prefixOverrides="and|or">
			bi.book_type=bt.id
			<if test="bookType ! =null and bookType>0"> <! BookType in test is an annotation in the Dao method0Id =#{bookType} </if>
			<if test="bookName! =null and bookName! = "">
				AND bi.book_name LIKE  CONCAT(The '%',#{bookName},The '%')
			</if>			
			<if test="isBorrow! =null and isBorrow! = 1">
				AND bi.is_borrow=#{isBorrow}
			</if> </trim> limit #{startQuery},#{pageSize} </select> <! --> <select id= (select id, id, id, id, id)"findCountByConditions" resultType="int"> <! -- resultType The return type is Integer and can be written directlyintOr Java. Lang. Integer - >SELECT
			COUNT(bi.book_id)FROM book_info AS bi, book_type AS bt <! - the use ofifP78 Example on page P79 --> <trim prefix="WHERE" prefixOverrides="and|or">
			bi.book_type=bt.id
			<if test="bookType ! =null and bookType>0"> <! BookType in test is an annotation in the Dao method0Id =#{bookType} </if>
			<if test="bookName! =null and bookName! = "">
				AND bi.book_name LIKE  CONCAT(The '%',#{bookName},The '%')
			</if>			
			<if test="isBorrow! =null and isBorrow! = 1">
				AND bi.is_borrow=#{isBorrow}
			</if>
		</trim>
	</select>
</mapper>
Copy the code

 PageUtil.java

package com.bookssys.util;

/** ** pagination tool class **@author Administrator
 * 
 */
public class PageUtil {
	/** * Public method for counting total pages **@paramTotalSize * Total number of messages *@paramPageSize * Number of items per page *@return* /
	public static final Integer calTotalPage(Integer totalSize, Integer pageSize) {
		int totalPage = 1;
		// Count the total pages
		totalPage = (totalSize % pageSize == 0)? (totalSize / pageSize) : (totalSize / pageSize +1);
		return totalPage;
	}

	/** * page number control method **@paramPageIndex * Number of current pages *@paramTotalPage * Total pages *@return* /
	public static final Integer checkPageIndex(Integer pageIndex, Integer totalPage) {
		// Page control
		if (pageIndex < 1) {// The current number of pages cannot be smaller than the minimum number of pages
			pageIndex = 1;
		} else if (pageIndex > totalPage) { The current number of pages cannot be greater than the maximum number of pages
			pageIndex = totalPage;
		}
		returnpageIndex; }}Copy the 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;
	/* For every biz defined, a business is reinjected */
	@Resource
	private BookTypeBiz bookTypeBiz;
	
	private final Integer pageSize=5;/* Display 5 data per page */

	/** * Select ** * from library **@paramBookType * Book category *@paramBookName * bookName *@paramIsBorrow * Whether to borrow *@param model
	 * @return* /

	/* A controller */
	@RequestMapping(value = "findBookByConditions.html")
	public String findBookinfoByConditions(
			// @RequestParam(required = false)This parameter is not required and can be left blank@RequestParam(required = false) Integer bookType,
			@RequestParam(required = false,defaultValue="") String bookName,/*defaultValue="" given a defaultValue, the default is null, otherwise an error */
			@RequestParam(required = false) Integer isBorrow, 
			@RequestParam(required = false,defaultValue="1")Integer pageIndex,// The first page is displayed by default. {
		
		// Convert the code before the search
		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 matching information items
		int totalSize=bookBiz.findCountByConditions(bookType, bookName, isBorrow);
		int totalPage=0;/ / the total number of pages
		
		// This array is used to display the specific number of pages, easy to jump to
		int[] pageArray=null;
		if (totalSize>0) {// Paging is performed only when the total number of entries is greater than 0
			// Count the total page count calls the public class count method
			totalPage=PageUtil.calTotalPage(totalSize, pageSize);
			
			// Page number control calls public class
			pageIndex=PageUtil.checkPageIndex(pageIndex, totalPage);				
				
			/* Get all book information and save pagination display */
			List<Book> infoList = bookBiz.findBookinfoByConditions(
					bookType, bookName, isBorrow,(pageIndex-1)*pageSize,pageSize);
			model.addAttribute("infoList", infoList);
					
			pageArray=new int[totalPage];// Define the length of the array as the total number of pages
		
		}
			/* Get all types of books and save */
			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 the 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"%>
<! DOCTYPEhtml 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">
			<! -- bookType in name must match the condition in Controller Controller -->
			<option value="0">-- Please select --</option>
			<c:forEach items="${typeList }" var="type">
				<option value="${type.id }"
					<c:if test="${type.id==bookType }"><! -- Displays the selected book type: displays the currently selected book type when the current book type is equal to the book type saved in the controller -->
								selected="selected"
							</c:if>>${type.type_name
					}</option>
			</c:forEach>
		</select>Book Name:<input type="text" name="bookName" value="${bookName }" />
		<! -- Data output -->Whether to borrow:<select name="isBorrow">
			<option value="1">-- Please select --</option>
			<option value="0"
				<c:if test="${isBorrow==0 }">
								selected="selected"
						</c:if>"> < p style =" max-width: 100%</option>
			<option value="1"
				<c:if test="${isBorrow==1 }">
								selected="selected"
						</c:if>> have to borrow</option>
		</select> <input type="submit" value="Query" />
	</form>
	<table border="1" width="660px">
		<tr>
			<td>Book number</td>
			<td>Book classification</td>
			<td>The name of the book</td>
			<td>The author</td>
			<td>Press.</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="${info.is_borrow==0 }">
						<a href="javascript:void(0)" class="borrowStatus"
							book_id="${info.book_id }">Apply for borrowing</a>
					</c:if> <c:if test="${info.is_borrow==1 }">
						<span>Have to borrow</span>
					</c:if></td>
			</tr>
		</c:forEach>
	</table>
	<div>
		<! Click the previous or next page to display the corresponding information instead of displaying the previous or next page information of all books --><%-- the address does not appear again/SEC when the interceptor clicks on the previous or next page: 1. Before/SEC plus ${pageContext. Request. ContextPath} -- % ><a
			href="${pageContext.request.contextPath }/findBookByConditions.html? pageIndex=${pageIndex-1 } &bookType=${bookType}&bookName=${bookName}&isBorrow=${isBorrow}">The previous page</a>&nbsp;
		<! Page count -->
		<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>&nbsp;
		</c:forEach>
		<a
			href="${pageContext.request.contextPath }/findBookByConditions.html? pageIndex=${pageIndex+1 } &bookType=${bookType}&bookName=${bookName}&isBorrow=${isBorrow}">The next page</a>
	</div>
	<script type="text/javascript" src="The statics/js/jquery - 1.8.3. Js." "></script>
</body>
</html>
Copy the code