图书购买系统之商品分类分页--根据主页面传来的值分类显示商品并实现动态分页功能_html 代码实现分类的书籍列表页-程序员宅基地

技术标签: 分页  项目  图书实时检索  

1.实现效果图


2.home.jsp,主页面

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title>bootstore</title>
		<!--引入bootstrap.css-->
		<link rel="stylesheet" type="text/css" href="<%=request.getContextPath()%>/bootstrap/css/bootstrap.css"/>
		<!--引入myscss.css-->
		<link rel="stylesheet" type="text/css" href="<%=request.getContextPath()%>/css/myscss.css" />
		<!--引入jquery.js-->
		<script type="text/javascript" src="<%=request.getContextPath()%>/bootstrap/js/jquery-3.1.1.js"></script>
		<!--bootstrap.js-->
		<script type="text/javascript" src="<%=request.getContextPath()%>/bootstrap/js/bootstrap.js"></script>
		<!--home.js-->
		<script type="text/javascript" src="<%=request.getContextPath()%>/foreground/book/home.js"></script>
		<!--contextPath-->
		<script type="text/javascript">
			var contextPath = "${pageContext.request.contextPath}";
		</script>
	</head>
	<body>
		<!--公用头部-->
		<!-- <div id="navhead"></div> -->
		<%@include file="/foreground/common/head.jsp"%>
		<!--<nav class="navbar navbar-default">
		  	<div class="container">
			    <div class="navbar-header">
			      	<button type="button" data-toggle="collapse" data-target="#collapse-1" aria-expanded="false" class="navbar-toggle collapsed">
			        	<span class="sr-only">Toggle navigation</span>
			        	<span class="icon-bar"></span>
				        <span class="icon-bar"></span>
				        <span class="icon-bar"></span>
				        <span class="icon-bar"></span>
			      	</button>
			      	<a href="index.html">
			      		<img src="../../img/bookimages/logo.png" />
			      	</a>
			    </div>
			    <div class="collapse navbar-collapse backcolor" id="collapse-1">
			      	<ul class="nav navbar-nav pull-right">
			      		<li><a href="#"><span class="glyphicon glyphicon-shopping-cart">购物车|</span></a></li>
			      		<li><a href="#"><span class="glyphicon glyphicon-question-sign">帮助中心|</span></a></li>
			      		<li><a href="login.html"><span class="glyphicon glyphicon-user">我的账户|</span></a></li>
			      		<li><a href="register.html"><span class="glyphicon glyphicon-pencil">新用户注册</span></a></li>
			      	</ul>
			    </div>
		  	</div>
		</nav>-->
		<!--导航栏-->
		<div class="container">
			<div class="text-nowrap text-center menu">
				<a href="<%=request.getContextPath()%>/ShowProductsServlet?currentPage=1&category=文学">文学</a>
				<a href="<%=request.getContextPath()%>/ShowProductsServlet?currentPage=1&category=生活">生活</a>
				<a href="<%=request.getContextPath()%>/ShowProductsServlet?currentPage=1&category=计算机">计算机</a>
				<a href="<%=request.getContextPath()%>/ShowProductsServlet?currentPage=1&category=外语">外语</a>
				<a href="<%=request.getContextPath()%>/ShowProductsServlet?currentPage=1&category=经管">经管</a>
				<a href="<%=request.getContextPath()%>/ShowProductsServlet?currentPage=1&category=励志">励志</a>
				<a href="<%=request.getContextPath()%>/ShowProductsServlet?currentPage=1&category=社科">社科</a>
				<a href="<%=request.getContextPath()%>/ShowProductsServlet?currentPage=1&category=学术">学术</a>
				<a href="<%=request.getContextPath()%>/ShowProductsServlet?currentPage=1&category=少儿">少儿</a>
				<a href="<%=request.getContextPath()%>/ShowProductsServlet?currentPage=1&category=艺术">艺术</a>
				<a href="<%=request.getContextPath()%>/ShowProductsServlet?currentPage=1&category=原版">原版</a>
				<a href="<%=request.getContextPath()%>/ShowProductsServlet?currentPage=1&category=科技">科技</a>
				<a href="<%=request.getContextPath()%>/ShowProductsServlet?currentPage=1&category=考试">考试</a>
				<a href="<%=request.getContextPath()%>/ShowProductsServlet?currentPage=1&category=生活百科">生活百科</a>
				<a href="<%=request.getContextPath()%>/ShowProductsServlet?currentPage=1&category=全部目录商品" id="yellow">全部目录商品</a>		
			</div>
			<div class="clearfix searchcolor">
				<form class="navbar-form navbar-right searchmargin" role="search">
					<div class="form-group">
					    <input id="searchContent" type="text" class="form-control">
					    <div id="searchResult" style="background:Snow;position:absolute;left:71%;width:15%;z-index:1;"></div>
					</div>
					<button type="button" οnclick="doSearch()" class="btn btn-info">搜索</button>
				</form>
			</div>
		</div>
		<!--新书上架-->
		<div class="container margintop">
			<div class="row">
				<div class="col-md-2"></div>
				<div class="col-md-8">
					<div class="media">
					  	<div class="media-left">
					    	<a href="#">
					      		<img class="media-object" src="<%=request.getContextPath()%>/img/otherimages/home_book.PNG" alt="图书">
					    	</a>
					  	</div>
					  	<div class="media-right paddingleft">
					    	<h2 class="bg-danger">新书上架</h2>
					    	<h5 class="htext">JAVA入门经典</h5>
					    	<p class="text-muted">《Java入门经典》,是2012年出版的图书,是2012年出版的图书是2012年出版的图书作者是Rogers Cadenhead。 《Java入门经典(第6版)》总共24章,先讲解了Java程序的编写流程、工作原理等内容;然后介绍了有关Java编程 的基本知识,包括变量、条件语句、</p>
					  		<img class="img-responsive thumbnail" src="<%=request.getContextPath()%>/img/otherimages/home_book2.PNG">
					  	</div>
					</div>
				</div>
				<div class="col-md-2"></div>
			</div>
		</div>
		<!--公告板和本周热卖-->
		<div class="container margintop">
			<div class="row">
				<div class="col-md-1"></div>
				<div class="col-md-5">
					<h2 class="bg-success">公告板</h2>
					<ul class="list-group-item">
						<li><a>图书三折起</a></li>
						<li><a>甩卖了,<java疯狂讲义10元起售></a></li>
						<li><a>新书到了,欢迎订购</a></li>
						<li><a>系统维护通知</a></li>
						<li><a>好消息好消息</a></li>
					</ul>
				</div>
				<div class="col-md-5">
					<h2 class="bg-danger">本周热卖</h2>
					<div class="pull-left text-center" id="book">
						<div class="col-sm-6">
							<img src="<%=request.getContextPath()%>/img/bookimage/20285763-1_b.jpg" /><br />
							<span>书名:java核心技术</span><br />
							<span>售价:XXX</span>
						</div>
						<div class="col-sm-6">
							<img src="<%=request.getContextPath()%>/img/bookimage/20412979-1_b.jpg" /><br />
							<span>书名:javascript</span><br />
							<span>售价:XXX</span>
						</div>
					</div>
				</div>
				<div class="col-md-1"></div>
			</div>
		</div>
		<nav class="navbar navbar-default bottom margin">
			<div class="container">
				<div class="row">
					<div class="col-md-1"></div>
					<div class="col-md-4">
						<img src="<%=request.getContextPath()%>/img/otherimages/logo.png">
					</div>
					<div class="col-md-7">
						<div style="margin-top: 10px;">
							CONTACT US<br>
						    COPYRIGHT 2017 BootStore AS Rights RESERVED
						</div>
					</div>
				</div>
			</div>
		</nav>
	</body>
</html>

3.home.js

//输入框键盘弹起事件,在这里发送ajax请求
$(function(){
	$("#searchContent").keyup(function(){
		//设置ajax请求url和请求内容
		var sendUrl = contextPath+"/AjaxShowSearchProductNameServlet?time"+new Date().getTime();
		var sendDate = {
				"value":$("#searchContent").val()
		}
		//发送ajax请求
		$.ajax({
			type:"post",
			url:sendUrl,
			data:sendDate,
			async:true,
			//请求成功的回调函数
			success:function(backData,textStatus,xmlHttprequest){
				var ss = backData.split(",");
				var childDiv = "";
				for(var i = 0;i < ss.length;i++){
					childDiv +="<div οnclick='setSearchContent(this)'>" + ss[i] + "</div>";
				}
				//显示搜索框
				$("#searchResult").html(childDiv);
				$("#searchResult").show();
			}
		});
	});
});

//选中搜索列表中的一条数据,设置为搜索框内容
function setSearchContent(caller){
	$("#searchContent").val($(caller).html());
	$("#searchResult").hide();
}

//搜索功能
function doSearch(){
	//获取要搜索的内容
	var category = $("#searchContent").val();
	//alert(contextPath+"/ShowBlurProductsServlet?category="+category);
	window.location.href = contextPath+"/ShowBlurProductsServlet?category="+category;
}


4.bookcategory.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>
<html>
	<head>
		<meta charset="UTF-8">
		<title>bookcategory</title>
		<!--引入bootstrap.css-->
		<link rel="stylesheet" type="text/css" href="<%=request.getContextPath()%>/bootstrap/css/bootstrap.css"/>
		<!--引入myscss.css-->
		<link rel="stylesheet" type="text/css" href="<%=request.getContextPath()%>/css/myscss.css" />
		<!--引入jquery.js-->
		<script type="text/javascript" src="<%=request.getContextPath()%>/bootstrap/js/jquery-3.1.1.js"></script>
		<!--bootstrap.js-->
		<script type="text/javascript" src="<%=request.getContextPath()%>/bootstrap/js/bootstrap.js"></script>
		<!--bookcategory.js-->
		<script type="text/javascript" src="<%=request.getContextPath()%>/foreground/book/bookcategory.js"></script>
	</head>
	<body>
		<!--公用头部-->
		<!-- <div id="navhead"></div> -->
		<%@include file="/foreground/common/head.jsp"%>
		<!--路径导航-->
		<div class="container">
			<ol class="breadcrumb">
			  	<li><a href="<%=request.getContextPath()%>/foreground/book/home.jsp">首页</a></li>
			  	<li><a id="navpath" href="#">全部商品</a></li>
			</ol>
		</div>
		<!--商品列表-->
		<div class="container">
			<div class="row">
				<div class="col-md-1"></div>
				<div class="col-md-10">
					<img class="img" src="<%=request.getContextPath()%>/img/otherimages/productlist.gif" />
					<div class="text-center" id="books">
						<c:forEach var="book" varStatus="bookidx" items="${bookPage.ps}">
							<div class="col-md-3">
								<div class="thumbnail">
									<img src="<%=request.getContextPath()%>/${book.imgurl}"/><br />
									<span>书名:${book.name}</span><br />
									<span>售价:${book.price}</span><br />
									<input type="image" οnclick="addCart('${book.id}','${book.name}','${book.price}','${book.num}')" src="<%=request.getContextPath()%>/img/bookimages/buybutton.gif" />
								</div>
							</div>
						</c:forEach>
					</div>
				</div>
				<div class="col-md-1"></div>
			</div>
		</div>
		<!--分页-->
		<c:if test="${bookPage.isblur!='yes'}">
			<div class="container">
			  	<nav class="text-center">
				  	<ul class="pagination text-center" >
				    	<li><a href="<%=request.getContextPath()%>/ShowProductsServlet?currentPage=1&category=${bookPage.category}"><span>«首页</span> </a></li>
				    	<li><a href="<%=request.getContextPath()%>/ShowProductsServlet?currentPage=${(bookPage.currentPage>1)?(bookPage.currentPage-1):1}&category=${bookPage.category}">上一页</a></li>
					    <li><a href="<%=request.getContextPath()%>/ShowProductsServlet?currentPage=${(bookPage.currentPage<bookPage.totalPage)?(bookPage.currentPage+1):bookPage.totalPage}&category=${bookPage.category}">下一页</a></li>
					    <li><a href="<%=request.getContextPath()%>/ShowProductsServlet?currentPage=${bookPage.totalPage}&category=${bookPage.category}"> <span>末页»</span></a></li>
					    <li class="pagespan">${bookPage.currentPage}/${bookPage.totalPage}</li>
				  	</ul>
				</nav>
			</div>
		</c:if>
	</body>
</html>


5.bookcategory.js

//1.路径导航部分
//截取从home.html传过来的URL
$(function(){
	var url = window.location.search;//从问号 (?) 开始的 URL(查询部分),此时等号后面是乱码
	url = decodeURI(url);//解码,把乱码变成正常的文字
	var substr = url.substr(url.indexOf("category=")+9);//截取字符串,截取category=之后的内容
	$("#navpath").html(substr);//将截取到的字符串放到路径导航的位置
});

//2.添加购物车部分
//思路:把添加购物车的数据存放在localStorage中
//方法传参:书的id,书名,书售价,书的库存
function addCart(id,name,price,num){
	//1.创建一个js对象
	var book = {
		"product_id":id,   //书的id
		"name":name,       //书名
		"price":price,     //售价
		"num":num,         //库存
		"buynum":1         //购买数量
	}
	//2.获取已经保存的数据
	var storebook = localStorage.getItem("carts");
	//3.将已经获取到的数据转换成数组
	var bookArr = JSON.parse(storebook);//JSON.parse() 方法解析一个JSON字符串
	//如果购物车离得数据为空,说明从来没有保存过数据
	if(bookArr == null){
		//创建一个数组
		var books = new Array();
		//把要保存的数据存到数组中
		books.push(book);
		//将数组转换成字符串
		var bookstr = JSON.stringify(books);
		//将字符串保存到localStorge中
		localStorage.setItem("carts",bookstr);
	}
	//已经存过数据了
	else{
		//1.判断我们存的书籍是否已经在购物车了,如果存在,数量+1
		var isFind = false;
		for(var i = 0;i < bookArr.length; i++){
			var b = bookArr[i];
			if(b.product_id == book.product_id){
				b.buynum++;
				isFind = true;
				break;
			}
		}
		//2.如果不存在,将书籍添加到购物车中
		if(isFind == false){
			bookArr.push(book);
		}
		//3.把bookArr重新转换成字符串保存
		var s = JSON.stringify(bookArr);
		localStorage.setItem("carts",s);
	}
	alert("添加成功!");
}


6.ShowProductsServlet.java,处理书籍信息显示

package com.chinasoft.servlet;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.chinasoft.dao.impl.ImplProductDao;
import com.chinasoft.entity.PageBean;

/**
 * @author jf
 * 处理书籍信息的显示
 */
@WebServlet("/ShowProductsServlet")
public class ShowProductsServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public ShowProductsServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//1.获取请求参数
		String currentPage = request.getParameter("currentPage");
		String category = request.getParameter("category");
		//2.根据请求参数查询数据库
		ImplProductDao implProductDao = new ImplProductDao();
		try {
			PageBean bookPage = implProductDao.findPageBooks(Integer.parseInt(currentPage), 8, category);
			//3.保存数据、跳转页面
			request.getSession().setAttribute("bookPage", bookPage);
			request.getRequestDispatcher("/foreground/book/bookcategory.jsp").forward(request, response);
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}

7.AjaxShowSearchProductNameServlet.java实现模糊查询书名

package com.chinasoft.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.chinasoft.dao.impl.ImplProductDao;
import com.chinasoft.entity.Product;

/**
 * @author jf
 * 功能:根据搜索条件返回符合条件的书名
 */
@WebServlet("/AjaxShowSearchProductNameServlet")
public class AjaxShowSearchProductNameServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public AjaxShowSearchProductNameServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//1.获取请求参数
		String value = request.getParameter("value");
		ImplProductDao implProductDao = new ImplProductDao();
		try {
			//2.根据请求模糊检索数据库
			List<Product> list = implProductDao.findBooksByBlurName(value);
			//3.把所有符合条件的书拼接成字符串
			String books = "";
			if("".equals(value)){
				
			}else{
				for(int i = 0;i<list.size();i++){
					if(i>0){
						books += ",";
					}
					books += list.get(i).getName();
				}
			}
			//4.向客户端输出字符串
			response.getWriter().write(books);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
				
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}


8.ShowBlurProductsServlet.java根据搜索框的搜索内容,搜索符合条件的书籍,跳转新页面显示书籍

package com.chinasoft.servlet;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.chinasoft.dao.impl.ImplProductDao;
import com.chinasoft.entity.PageBean;

/**
 * @author jf
 * 根据搜索框的搜索内容,搜索符合条件的书籍,跳转新页面显示书籍
 */
@WebServlet("/ShowBlurProductsServlet")
public class ShowBlurProductsServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public ShowBlurProductsServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//1.获取请求参数
		String category = request.getParameter("category");
		//2.根据请求参数查询数据库
		ImplProductDao implProductDao = new ImplProductDao();
		try {
			PageBean bookPage = implProductDao.findPageBooksByBlurName(category);
			//3.保存数据、跳转页面
			request.getSession().setAttribute("bookPage", bookPage);
			request.getRequestDispatcher("/foreground/book/bookcategory.jsp").forward(request, response);
		}catch (NumberFormatException e) {
			e.printStackTrace();
		}
		catch (SQLException e) {
			e.printStackTrace();
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}

9.ShowProductsServlet.java处理书籍信息的显示

package com.chinasoft.servlet;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.chinasoft.dao.impl.ImplProductDao;
import com.chinasoft.entity.PageBean;

/**
 * @author jf
 * 处理书籍信息的显示
 */
@WebServlet("/ShowProductsServlet")
public class ShowProductsServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public ShowProductsServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//1.获取请求参数
		String currentPage = request.getParameter("currentPage");
		String category = request.getParameter("category");
		//2.根据请求参数查询数据库
		ImplProductDao implProductDao = new ImplProductDao();
		try {
			PageBean bookPage = implProductDao.findPageBooks(Integer.parseInt(currentPage), 8, category);
			//3.保存数据、跳转页面
			request.getSession().setAttribute("bookPage", bookPage);
			request.getRequestDispatcher("/foreground/book/bookcategory.jsp").forward(request, response);
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}

10.ProductDao.java接口

package com.chinasoft.dao;

import java.sql.SQLException;
import java.util.List;

import com.chinasoft.entity.PageBean;
import com.chinasoft.entity.Product;

public interface ProductDao {
	//查找所有的书籍
	public List<Product> findAllBooks() throws SQLException;
	//根据书的id查找指定的书籍
	public Product findBookById(String id) throws SQLException;
	//查找某一分类下的书籍总数
	public long count(String category) throws SQLException;
	//分页查询,查找当前页下、每页显示指定size下、以及当前分类下的书籍
	public List<Product> findBooks(int currentPage, int pageSize,String category) throws SQLException;
	//根据指定书名搜索书籍
	public Product searchBookByName(String name) throws SQLException;
	//模糊检索,根据模糊条件查找数据库,返回list集合
	public List<Product> findBooksByBlurName(String name) throws SQLException;
	//模糊检索,根据模糊条件查找数据库,返回pageBean
	public PageBean findPageBooksByBlurName(String name) throws SQLException;
	//分页查询,返回pageBean,pegeBean封装了当前页的所有信息
	public PageBean findPageBooks(int currentPage, int pageSize,String category) throws SQLException;
}

11.ImplProductDao.java,ProductDao接口实现类

package com.chinasoft.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import com.chinasoft.dao.ProductDao;
import com.chinasoft.entity.PageBean;
import com.chinasoft.entity.Product;
import com.chinasoft.util.C3P0Util;

public class ImplProductDao implements ProductDao{

	/**
	 * 查找所有的书籍
	 */
	@Override
	public List<Product> findAllBooks() throws SQLException {
		//1.创建QueryRunner对象
		QueryRunner qr = new QueryRunner(C3P0Util.getDataSource());
		List<Product> list = qr.query("select * from products", new BeanListHandler<>(Product.class));
		return list;
	}

	/**
	 * 根据书的id查找指定的书籍
	 */
	@Override
	public Product findBookById(String id) throws SQLException {
		QueryRunner qr = new QueryRunner(C3P0Util.getDataSource());
		String sql = "select * from products where id=?";
		Product product = qr.query(sql,new BeanHandler<Product>(Product.class),id);
		return product;
	}

	/**
	 * 查找某一分类下的书籍总数
	 */
	@Override
	public long count(String category) throws SQLException {
		QueryRunner qr = new QueryRunner(C3P0Util.getDataSource());
		String sql = null;
		long count = 0;
		if ("全部商品目录".equals(category)) {
			sql = "select COUNT(*) from products";
			count = qr.query(sql, new ScalarHandler<>());
		}
		else{
			sql = "select COUNT(*) from products where category=?";
			count = qr.query(sql, new ScalarHandler<>(),category);
		}
		return count;
	}

	/**
	 *
	 * 分页查询,查找当前页下、每页显示指定size下、以及当前分类下的所有书籍
	 */
	@Override
	public List<Product> findBooks(int currentPage, int pageSize, String category) throws SQLException {
		QueryRunner qr = new QueryRunner(C3P0Util.getDataSource());
		String sql = null;
		List<Product> products = null;
		if ("全部商品目录".equals(category)) {
			sql = "select * from products limit ?,?";
			if (currentPage>=1) {
				products = qr.query(sql, new BeanListHandler<>(Product.class),(currentPage-1)*pageSize,pageSize);
			}
		}
		else{
			sql = "select * from products where category=? limit ?,?";
			if (currentPage>=1) {
				products = qr.query(sql, new BeanListHandler<>(Product.class), category,(currentPage-1)*pageSize,pageSize);
			}
			
		}
		return products;
	}

	/**
	 * 根据指定书名搜索书籍
	 */
	@Override
	public Product searchBookByName(String name) throws SQLException {
		QueryRunner qr = new QueryRunner(C3P0Util.getDataSource());
		String sql = "select * from products where name=?";
		Product product = qr.query(sql,new BeanHandler<Product>(Product.class),name);
		return product;
	}

	/**
	 * 模糊检索,根据模糊条件查找数据库,返回list
	 */
	@Override
	public List<Product> findBooksByBlurName(String name) throws SQLException {
		//1.创建QueryRunner对象
		QueryRunner qr = new QueryRunner(C3P0Util.getDataSource());
		List<Product> list = qr.query("select * from products where name like ?", new BeanListHandler<>(Product.class),"%"+name+"%");
		return list;
	}
	
	/**
	 * 模糊检索,根据模糊条件查找数据库,返回pagebean
	 */
	@SuppressWarnings("unused")
	@Override
	public PageBean findPageBooksByBlurName(String name) throws SQLException {
		// 1.获取符合条件的书的集合
		List<Product> list = findBooksByBlurName(name);
		// 2.获取搜索到的所有书籍的个数
		int totalcount = list.size();
		// 3.设置当前页和最大页
		int currentPage = 1;
		int totalPage = 1;
		if (totalcount == 0) {
			currentPage = 0;
			totalPage = 0;
		}
		// 4.创建PageBean对象
		PageBean pageBean = new PageBean();
		pageBean.setCurrentPage(currentPage);
		pageBean.setTotalCount(totalcount);
		pageBean.setTotalPage(totalcount);
		pageBean.setCurrentCount(totalcount);
		pageBean.setIsblur("yes");
		if (list!=null) {
			pageBean.setPs(list);
		}
		pageBean.setCategory(name);
		return pageBean;
	}

	/**
	 * 分页查询,返回pageBean,pegeBean封装了当前页的所有信息
	 */
	@Override
	public PageBean findPageBooks(int currentPage, int pageSize, String category) throws SQLException {
		// 1.获取当前类别下,一页显示pageSize条,当前页所对应的所有书籍
		ImplProductDao impl = new ImplProductDao();
		List<Product> products = impl.findBooks(currentPage, pageSize, category);
		// 2.获取当前类别下所有书籍的个数
		long totalcount = impl.count(category);
		//如果当前分类下 的图书个数为0,则currentPage为0
		if (totalcount == 0) {
			currentPage = 0;
		}
		// 3.计算要显示的总页数
		int totalpage = (int) Math.ceil(totalcount / (double) pageSize);
		// 4.创建PageBean对象
		PageBean pageBean = new PageBean();
		pageBean.setCurrentPage(currentPage);
		pageBean.setTotalCount(totalcount);
		pageBean.setTotalPage(totalpage);
		pageBean.setCurrentCount(pageSize);
		pageBean.setPs(products);
		pageBean.setCategory(category);
		return pageBean;
	}

}





版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/java_jfei/article/details/70036928

智能推荐

什么是内部类?成员内部类、静态内部类、局部内部类和匿名内部类的区别及作用?_成员内部类和局部内部类的区别-程序员宅基地

文章浏览阅读3.4k次,点赞8次,收藏42次。一、什么是内部类?or 内部类的概念内部类是定义在另一个类中的类;下面类TestB是类TestA的内部类。即内部类对象引用了实例化该内部对象的外围类对象。public class TestA{ class TestB {}}二、 为什么需要内部类?or 内部类有什么作用?1、 内部类方法可以访问该类定义所在的作用域中的数据,包括私有数据。2、内部类可以对同一个包中的其他类隐藏起来。3、 当想要定义一个回调函数且不想编写大量代码时,使用匿名内部类比较便捷。三、 内部类的分类成员内部_成员内部类和局部内部类的区别

分布式系统_分布式系统运维工具-程序员宅基地

文章浏览阅读118次。分布式系统要求拆分分布式思想的实质搭配要求分布式系统要求按照某些特定的规则将项目进行拆分。如果将一个项目的所有模板功能都写到一起,当某个模块出现问题时将直接导致整个服务器出现问题。拆分按照业务拆分为不同的服务器,有效的降低系统架构的耦合性在业务拆分的基础上可按照代码层级进行拆分(view、controller、service、pojo)分布式思想的实质分布式思想的实质是为了系统的..._分布式系统运维工具

用Exce分析l数据极简入门_exce l趋势分析数据量-程序员宅基地

文章浏览阅读174次。1.数据源准备2.数据处理step1:数据表处理应用函数:①VLOOKUP函数; ② CONCATENATE函数终表:step2:数据透视表统计分析(1) 透视表汇总不同渠道用户数, 金额(2)透视表汇总不同日期购买用户数,金额(3)透视表汇总不同用户购买订单数,金额step3:讲第二步结果可视化, 比如, 柱形图(1)不同渠道用户数, 金额(2)不同日期..._exce l趋势分析数据量

宁盾堡垒机双因素认证方案_horizon宁盾双因素配置-程序员宅基地

文章浏览阅读3.3k次。堡垒机可以为企业实现服务器、网络设备、数据库、安全设备等的集中管控和安全可靠运行,帮助IT运维人员提高工作效率。通俗来说,就是用来控制哪些人可以登录哪些资产(事先防范和事中控制),以及录像记录登录资产后做了什么事情(事后溯源)。由于堡垒机内部保存着企业所有的设备资产和权限关系,是企业内部信息安全的重要一环。但目前出现的以下问题产生了很大安全隐患:密码设置过于简单,容易被暴力破解;为方便记忆,设置统一的密码,一旦单点被破,极易引发全面危机。在单一的静态密码验证机制下,登录密码是堡垒机安全的唯一_horizon宁盾双因素配置

谷歌浏览器安装(Win、Linux、离线安装)_chrome linux debian离线安装依赖-程序员宅基地

文章浏览阅读7.7k次,点赞4次,收藏16次。Chrome作为一款挺不错的浏览器,其有着诸多的优良特性,并且支持跨平台。其支持(Windows、Linux、Mac OS X、BSD、Android),在绝大多数情况下,其的安装都很简单,但有时会由于网络原因,无法安装,所以在这里总结下Chrome的安装。Windows下的安装:在线安装:离线安装:Linux下的安装:在线安装:离线安装:..._chrome linux debian离线安装依赖

烤仔TVの尚书房 | 逃离北上广?不如押宝越南“北上广”-程序员宅基地

文章浏览阅读153次。中国发达城市榜单每天都在刷新,但无非是北上广轮流坐庄。北京拥有最顶尖的文化资源,上海是“摩登”的国际化大都市,广州是活力四射的千年商都。GDP和发展潜力是衡量城市的数字指...

随便推点

java spark的使用和配置_使用java调用spark注册进去的程序-程序员宅基地

文章浏览阅读3.3k次。前言spark在java使用比较少,多是scala的用法,我这里介绍一下我在项目中使用的代码配置详细算法的使用请点击我主页列表查看版本jar版本说明spark3.0.1scala2.12这个版本注意和spark版本对应,只是为了引jar包springboot版本2.3.2.RELEASEmaven<!-- spark --> <dependency> <gro_使用java调用spark注册进去的程序

汽车零部件开发工具巨头V公司全套bootloader中UDS协议栈源代码,自己完成底层外设驱动开发后,集成即可使用_uds协议栈 源代码-程序员宅基地

文章浏览阅读4.8k次。汽车零部件开发工具巨头V公司全套bootloader中UDS协议栈源代码,自己完成底层外设驱动开发后,集成即可使用,代码精简高效,大厂出品有量产保证。:139800617636213023darcy169_uds协议栈 源代码

AUTOSAR基础篇之OS(下)_autosar 定义了 5 种多核支持类型-程序员宅基地

文章浏览阅读4.6k次,点赞20次,收藏148次。AUTOSAR基础篇之OS(下)前言首先,请问大家几个小小的问题,你清楚:你知道多核OS在什么场景下使用吗?多核系统OS又是如何协同启动或者关闭的呢?AUTOSAR OS存在哪些功能安全等方面的要求呢?多核OS之间的启动关闭与单核相比又存在哪些异同呢?。。。。。。今天,我们来一起探索并回答这些问题。为了便于大家理解,以下是本文的主题大纲:[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-JCXrdI0k-1636287756923)(https://gite_autosar 定义了 5 种多核支持类型

VS报错无法打开自己写的头文件_vs2013打不开自己定义的头文件-程序员宅基地

文章浏览阅读2.2k次,点赞6次,收藏14次。原因:自己写的头文件没有被加入到方案的包含目录中去,无法被检索到,也就无法打开。将自己写的头文件都放入header files。然后在VS界面上,右键方案名,点击属性。将自己头文件夹的目录添加进去。_vs2013打不开自己定义的头文件

【Redis】Redis基础命令集详解_redis命令-程序员宅基地

文章浏览阅读3.3w次,点赞80次,收藏342次。此时,可以将系统中所有用户的 Session 数据全部保存到 Redis 中,用户在提交新的请求后,系统先从Redis 中查找相应的Session 数据,如果存在,则再进行相关操作,否则跳转到登录页面。此时,可以将系统中所有用户的 Session 数据全部保存到 Redis 中,用户在提交新的请求后,系统先从Redis 中查找相应的Session 数据,如果存在,则再进行相关操作,否则跳转到登录页面。当数据量很大时,count 的数量的指定可能会不起作用,Redis 会自动调整每次的遍历数目。_redis命令

URP渲染管线简介-程序员宅基地

文章浏览阅读449次,点赞3次,收藏3次。URP的设计目标是在保持高性能的同时,提供更多的渲染功能和自定义选项。与普通项目相比,会多出Presets文件夹,里面包含着一些设置,包括本色,声音,法线,贴图等设置。全局只有主光源和附加光源,主光源只支持平行光,附加光源数量有限制,主光源和附加光源在一次Pass中可以一起着色。URP:全局只有主光源和附加光源,主光源只支持平行光,附加光源数量有限制,一次Pass可以计算多个光源。可编程渲染管线:渲染策略是可以供程序员定制的,可以定制的有:光照计算和光源,深度测试,摄像机光照烘焙,后期处理策略等等。_urp渲染管线

推荐文章

热门文章

相关标签