小米商城——分页展示商品

GoodsServlet

 package web.servlet;

import com.alibaba.druid.util.StringUtils;
import domain.Cart;
import domain.Goods;
import domain.PageBean;
import service.CartService;
import service.GoodsService;
import service.Impl.CartServiceimpl;
import service.Impl.GoodsServiceImpl;
import utils.IsEmpty;

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 java.io.IOException;
import java.util.List;

/**
 * * author:张永辉; 2019/9/11; 15:05
 * *    程序 源于现实问题 解决现实问题...
 * 

* *************************** 功能三:商品的分页展示,即(分页)展示商品的Servlet *********************************************** * */ @WebServlet(name = "GoodsServlet" ,value = "/goodsservlet") public class GoodsServlet extends BaseServlet { // 一个将商品进行分页展示的方法: (指定类型的商品) public String getGoodsListByTypeId(HttpServletRequest request, HttpServletResponse response) { try { // 第一步:获取参数,包括当前页页码、当前页数据量、以及商品的类型id(即,为了确定是计算机、手机、平板、等的哪个类型) String typeId = request.getParameter("typeId"); // typeId是用来组成contion条件的; String pageNum_ = request.getParameter("pageNum"); String pageSize_ = request.getParameter("pageSize"); // 第二步:为 当前页页码、当前页数据量 设置默认值: int pageNum=1; int pageSize = 2; // 当前页数据量 if (!IsEmpty.isEmpty(pageNum_)) { pageNum=Integer.parseInt(pageNum_); if (pageNum < 1) { pageNum=1; } } if (!IsEmpty.isEmpty(pageSize_)) { pageSize=Integer.parseInt(pageSize_); if (pageSize < 1) { pageSize = 2; } } // 第三步:设置一个“条件”,即,查询商品、统计商品的时候,都需要指定商品的类型(计算机、手机、平板、等); String condition = ""; if (!IsEmpty.isEmpty(typeId)) { condition = "typeId=" + typeId; } // 第四步:编写该Servlet(商品的分页展示)所对应的业务层: GoodsService goodsService = new GoodsServiceImpl(); // 注意:这里的通过pageNum, pageSize, condition这三个参数最终查找出来的结果应是pageBean(分页实体类)类型的; PageBean pageBean=goodsService.findPageByWhere( pageNum, pageSize, condition); // // 然后将查找出来的pageBean存入域中;(JSP再从域中将pageBean取出来后,对pageBean的属性们进行一一遍历、继而实现分页展示) request.setAttribute("pageBean", pageBean); // (前端需要),将typeId也存入域中: request.setAttribute("typeId", typeId); return "/goodsList.jsp"; } catch (NumberFormatException e) { e.printStackTrace(); return "redirect:/index.jsp"; } } // 一个通过商品的id号来获取商品的方法: public String getGoodsById(HttpServletRequest request,HttpServletResponse response) throws Exception{ //第一步: 获取参数,id; String id = request.getParameter("id"); //第二步: 调用业务层: GoodsService goodsService=new GoodsServiceImpl(); // 第三步:判断id是否为空: if(StringUtils.isEmpty(id)){ return "redirect:/index.jsp"; } //第四步: 开始编写一个业务层的通过商品的id号来获取商品的方法: Goods goods=goodsService.findGoodsById(Integer.parseInt(id)); // 第五步:(应前端需求),将查询到的结果,即商品,给存入域中: request.setAttribute("goods", goods); return "/goodsDetail.jsp"; } }




业务层:GoodsServiceImpl.java

package service.Impl;

import dao.GoodsDao;
import dao.GoodsTypeDao;
import dao.Impl.GoodsDaoImpl;
import dao.Impl.GoodsTypeDaoImpl;
import domain.Goods;
import domain.GoodsType;
import domain.PageBean;
import org.apache.commons.beanutils.BeanUtils;
import service.GoodsService;

import java.util.List;

/**
 * * author:张永辉; 2019/9/11; 15:14
 * *    程序 源于现实问题 解决现实问题...
 * 

* ************************* 实现分页展示的业务层 ************************************************* */ public class GoodsServiceImpl implements GoodsService { GoodsDao goodsDao = new GoodsDaoImpl(); // 一个通过指定“条件”(商品类别)进行查找的方法,返回值为pageBean ; @Override public PageBean findPageByWhere(int pageNum, int pageSize, String condition) { // 数据库操作层的一个通过指定“条件”(商品类别)进行查找的方法,返回的是当前页的数据(List类型) List data = goodsDao.findPageByWhere(pageNum, pageSize, condition); // 数据库操作层的一个方法,用来统计指定“条件”(商品类别)下的数据库总数据量; long totalSize = goodsDao.getCount(condition); // pageBean的各个参数 PageBean pageBean = new PageBean<>(pageNum, pageSize, totalSize, data); return pageBean; } @Override public Goods findGoodsById(int id) { GoodsDao goodsDao = new GoodsDaoImpl(); Goods goods = goodsDao.findGoodsById(id); // 因为刚才商品的详情的实体类里面又新增了一个goodsType属性,所以需要获取到并set一下(赋值) // 通过GoodsTypeDao获取到goodsType的值; GoodsTypeDao goodsTypeDao = new GoodsTypeDaoImpl(); GoodsType goodsType = goodsTypeDao.findById(goods.getTypeid()); // 之后对goods实体类的goodsType属性进行赋值 goods.setGoodsType(goodsType); return goods; } }




分页的实体类:PageBean.java

package domain;

import java.util.List;

/**
 * * author:张永辉; 2019/9/11; 15:15
 * *    程序 源于现实问题 解决现实问题...
 * 

* ************************************************************************** */ public class PageBean { private int pageNum; private int pageSize; private long totalSize; private int pageCount; private List data; private int startPage; private int endPage; @Override public String toString() { return "PageBean{" + "pageNum=" + pageNum + ", pageSize=" + pageSize + ", totalSize=" + totalSize + ", pageCount=" + pageCount + ", data=" + data + ", startPage=" + startPage + ", endPage=" + endPage + '}'; } public int getPageNum() { return pageNum; } public void setPageNum(int pageNum) { this.pageNum = pageNum; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public long getTotalSize() { return totalSize; } public void setTotalSize(long totalSize) { this.totalSize = totalSize; } public int getPageCount() { return pageCount; } public void setPageCount(int pageCount) { this.pageCount = pageCount; } public List getData() { return data; } public void setData(List data) { this.data = data; } public PageBean(int pageNum, int pageSize, long totalSize, List data) { this.pageNum = pageNum; this.pageSize = pageSize; this.totalSize = totalSize; this.data = data; this.pageCount = (int) (totalSize % pageSize == 0 ? (totalSize / pageSize) : ((totalSize / pageSize) + 1)); // 计算导航的开始页和结束页 // 正常的情况 this.startPage = pageNum - 4; this.endPage = pageNum + 5; // 当前页码小于5 的情况:开始页为1,终止页为10 if (pageNum < 5) { this.startPage = 1; this.endPage = 10; } // 当前页大于(总页数-5)的情况,即到了末尾的时候的情况:开始页为pageCount - 9;终止页为末尾页; if (pageNum > (pageCount - 5)) { this.startPage = pageCount - 9; this.endPage = pageCount; } // 总页数小于10 的情况:开始页为1;终止页为总页数; if (pageCount < 10) { this.startPage = 1; this.endPage = pageCount; } } public int getStartPage() { return startPage; } public void setStartPage(int startPage) { this.startPage = startPage; } public int getEndPage() { return endPage; } public void setEndPage(int endPage) { this.endPage = endPage; } public PageBean() { } }




Dao层(数据库操作层)GoodsDaoImpl.java

package dao.Impl;

import dao.GoodsDao;
import domain.Goods;
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 utils.DruidDataSource01;
import utils.IsEmpty;

import javax.management.Query;
import java.sql.SQLException;
import java.util.List;

/**
 * * author:张永辉; 2019/9/11; 17:32
 * *    程序 源于现实问题 解决现实问题...
 * 

* ************************************************************************** */ public class GoodsDaoImpl implements GoodsDao { @Override public List findPageByWhere(int pageNum,int pageSize,String condition) { try { QueryRunner queryRunner = new QueryRunner(DruidDataSource01.getDruidDataSource()); String sql = "select * from tb_goods "; if (!IsEmpty.isEmpty2(condition)) { sql = sql + " where " + condition; } sql += " order by id limit ?,?"; List goods = queryRunner.query(sql, new BeanListHandler<>(Goods.class), (pageNum-1)*pageSize, pageSize); return goods; } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException("分页查询失败" + e); } } @Override public long getCount(String condition) { try { String sql = "select count(*) from tb_goods"; if (!IsEmpty.isEmpty2(condition)) { sql = sql + " where " + condition; } QueryRunner queryRunner = new QueryRunner(DruidDataSource01.getDruidDataSource()); return (long)queryRunner.query(sql, new ScalarHandler<>()); } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException("查询商品个数失败", e); } } @Override public Goods findGoodsById(Integer id) { try { QueryRunner queryRunner = new QueryRunner(DruidDataSource01.getDruidDataSource()); return (Goods) queryRunner.query("select * from tb_goods where id=?", new BeanHandler<>(Goods.class), id); } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException("根据id查询指定的商品失败" + e); } } }



你可能感兴趣的:(javaWeb)