版权声明:有问题可联系博主QQ:15577969,大家一起相互交流和学习。 https://blog.csdn.net/qq15577969/article/details/83349672
BookDaoMapper.java
/**
* 根据多条件(图书分类,图书名称,是否借阅)动态查询图书信息
*
* @param bookType
* 图书分类
* @param bookName
* 图书名称
* @param isBorrow
* 是否借阅
* @param startQuery
* 从第几条开始查询
* @param pageSize
* 每页显示的数量
* @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);
/**
* 根据多条件(图书分类,图书名称,是否借阅)动态查询图书信息条数
*
* @param bookType
* 图书分类
* @param bookName
* 图书名称
* @param isBorrow
* 是否借阅
* @return
*/
Integer findCountByConditions(@Param("bookType") Integer bookType,
@Param("bookName") String bookName,
@Param("isBorrow") Integer isBorrow);
BookDaoMapper.xml
<!-- 接口的全路径名 -->
<mapper namespace="com.bookssys.dao.BookDaoMapper">
<!-- 1对多的时候用collection,多对1用association -->
<resultMap type="Book_info" id="BookInfoBookType">
<!-- 主表的主键列 property表示实体类中的属性名 column表示对应列的别名 -->
<id property="book_id" column="bookid" />
<!-- sql语句中查找出来的数据,如果sql语句中有别名则column填的是别名,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" />
<!-- 主表写在association中 -->
<association property="bookType" javaType="Book_type">
<id property="id" column="typeid" />
<result property="type_name" column="typename" />
</association>
</resultMap>
<!-- id必须和方法名保持一致 select中的 resultMap必须和 resultMap中的id一致,表示调用这个名字的resultMap
或者是选取实体类的全路径 -->
<!-- 根据多条件(图书分类,图书名称,是否借阅)动态查询图书信息 -->
<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
<!-- 使用if+trim实现多条件查询P78 示例在P79页 -->
<trim prefix="WHERE" prefixOverrides="and|or">
bi.book_type=bt.id
<if test="bookType !=null and bookType>0"> <!-- test中的bookType是Dao中方法中的注解 0是index.jsp中的图书类型中的(请选择) -->
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>
<!-- 根据多条件(图书分类,图书名称,是否借阅)动态查询图书信息条数 -->
<select id="findCountByConditions" resultType="int"><!-- resultType返回类型是Integer类型,直接写int或java.lang.Integer -->
SELECT
COUNT(bi.book_id)
FROM
book_info AS bi,
book_type AS bt
<!-- 使用if+trim实现多条件查询P78 示例在P79页 -->
<trim prefix="WHERE" prefixOverrides="and|or">
bi.book_type=bt.id
<if test="bookType !=null and bookType>0"> <!-- test中的bookType是Dao中方法中的注解 0是index.jsp中的图书类型中的(请选择) -->
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>
PageUtil.java
package com.bookssys.util;
/**
* 分页工具类
*
* @author Administrator
*
*/
public class PageUtil {
/**
* 计算总页数的公共方法
*
* @param totalSize
* 信息总条数
* @param pageSize
* 每页条数
* @return
*/
public static final Integer calTotalPage(Integer totalSize, Integer pageSize) {
int totalPage = 1;
// 计算总页数
totalPage = (totalSize % pageSize == 0) ? (totalSize / pageSize)
: (totalSize / pageSize + 1);
return totalPage;
}
/**
* 页码控制方法
*
* @param pageIndex
* 当前页数
* @param totalPage
* 总的页数
* @return
*/
public static final Integer checkPageIndex(Integer pageIndex,
Integer totalPage) {
// 页码控制
if (pageIndex < 1) {// 当前页数不能小于最小页数
pageIndex = 1;
} else if (pageIndex > totalPage) { // 当前页数不能大于最大页数
pageIndex = totalPage;
}
return pageIndex;
}
}
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 {
/*注入业务*/
@Resource
private BookBiz bookBiz;
/*每定义一个biz都要重新注入一个业务*/
@Resource
private BookTypeBiz bookTypeBiz;
private final Integer pageSize=5;/*每页显示5条数据*/
/**
* 根据多条件动态查询图书信息
*
* @param bookType
* 图书分类
* @param bookName
* 图书名称
* @param isBorrow
* 是否借阅
* @param model
* @return
*/
/* 一个控制器 */
@RequestMapping(value = "findBookByConditions.html")
public String findBookinfoByConditions(
// @RequestParam(required = false)表示这不是必填项,可以为空,不传参数过来
@RequestParam(required = false) Integer bookType,
@RequestParam(required = false,defaultValue="") String bookName,/*defaultValue=""给定一个默认值,默认为空,要不然会报错*/
@RequestParam(required = false) Integer isBorrow,
@RequestParam(required = false,defaultValue="1") Integer pageIndex,//可以不传第几页,不传第几页的时候默认显示第一页
Model model) {
//转换编码 查找之前要先转换编码
try {
bookName=new String(bookName.getBytes("iso-8859-1"),"utf-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//查询符合条件信息的总条数
int totalSize=bookBiz.findCountByConditions(bookType, bookName, isBorrow);
int totalPage=0;//总页数
//此数组用于显示具体页数,便于用户跳转
int[] pageArray=null;
if (totalSize>0) {//在总条数大于0的条件下才进行分页
//计算总页数 调用公共类的计算方法
totalPage=PageUtil.calTotalPage(totalSize, pageSize);
//页码控制 调用公共类的
pageIndex=PageUtil.checkPageIndex(pageIndex, totalPage);
/*获得所有图书信息并保存 分页显示*/
List<Book> infoList = bookBiz.findBookinfoByConditions(
bookType, bookName, isBorrow,(pageIndex-1)*pageSize,pageSize);
model.addAttribute("infoList", infoList);
pageArray=new int[totalPage];//定义数组的长度和总页数一样
}
/*获得图书的所有类型并保存*/
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";
}
}
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>图书信息</title>
</head>
<body>
<%
String path = request.getContextPath() + "/";
%>
<form action="sec/findBookByConditions.html">
图书分类:<select name="bookType">
<!-- name中的bookType必须要和控制器Controller中的条件一致 -->
<option value="0">---请选择---</option>
<c:forEach items="${typeList }" var="type">
<option value="${type.id }"
<c:if test="${type.id==bookType }"><!-- 回显选中的图书类型:当前图书分类等于控制器中保存的图书类型的时候,显示当前选中的图书类型 -->
selected="selected"
</c:if>>${type.type_name
}</option>
</c:forEach>
</select> 图书名称:<input type="text" name="bookName" value="${bookName }" />
<!-- 数据回显 -->
是否借阅:<select name="isBorrow">
<option value="-1">---请选择---</option>
<option value="0"
<c:if test="${isBorrow==0 }">
selected="selected"
</c:if>>可以借阅</option>
<option value="1"
<c:if test="${isBorrow==1 }">
selected="selected"
</c:if>>已借阅</option>
</select> <input type="submit" value="查询" />
</form>
<table border="1" width="660px">
<tr>
<td>图书编号</td>
<td>图书分类</td>
<td>图书名称</td>
<td>作者</td>
<td>出版社</td>
<td>操作</td>
</tr>
<c:forEach items="${infoList }" var="info">
<tr>
<td>${info.book_code }</td>
<td>${info.bookType.type_name }</td>
<!-- bookType是实体类中的对象 -->
<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 }">申请借阅</a>
</c:if> <c:if test="${info.is_borrow==1 }">
<span>已借阅</span>
</c:if></td>
</tr>
</c:forEach>
</table>
<div>
<!-- 在带条件的前提下查询到相应信息点击上一页或下一页则显示对应信息而不是显示全部图书的上一页或下一页信息 -->
<%-- 拦截器点击上一页或者下一页时,地址不会重复出现/sec的方法:1.在/sec前加${pageContext.request.contextPath } --%>
<a
href="${pageContext.request.contextPath }/findBookByConditions.html?pageIndex=${pageIndex-1 }
&bookType=${bookType}&bookName=${bookName}&isBorrow=${isBorrow}">上一页</a>
<!-- 遍历显示页数 -->
<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}">下一页</a>
</div>
<script type="text/javascript" src="statics/js/jquery-1.8.3.js"></script>
</body>
</html>