实现购物车
1、 分页显示商品表中信息
2、 点击购买,显示购物车中的商品
CREATE TABLE tb_goods(
id VARCHAR(20) PRIMARY KEY,
name VARCHAR(20) NOT NULL,
price DOUBLE NOT NULL,
company VARCHAR(100) NOT NULL,
leaveDate DATE NOT NULL,
descs VARCHAR(500)
)
实体类
package com.neu.entity;
import java.util.Date;
public class Goods {
private String id;
private String name;
private Double price;
private String company;
private Date leaveDate;
private String descs;
private Integer cartNum;
public Goods() {
super();
// TODO Auto-generated constructor stub
}
public Goods(String id, String name, Double price, String company, Date leaveDate, String descs) {
super();
this.id = id;
this.name = name;
this.price = price;
this.company = company;
this.leaveDate = leaveDate;
this.descs = descs;
this.cartNum = 1;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
public Date getLeaveDate() {
return leaveDate;
}
public void setLeaveDate(Date leaveDate) {
this.leaveDate = leaveDate;
}
public String getDescs() {
return descs;
}
public void setDescs(String descs) {
this.descs = descs;
}
public Integer getCartNum() {
return cartNum;
}
public void setCartNum(Integer cartNum) {
this.cartNum = cartNum;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((company == null) ? 0 : company.hashCode());
result = prime * result + ((descs == null) ? 0 : descs.hashCode());
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((leaveDate == null) ? 0 : leaveDate.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((price == null) ? 0 : price.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Goods other = (Goods) obj;
if (company == null) {
if (other.company != null)
return false;
} else if (!company.equals(other.company))
return false;
if (descs == null) {
if (other.descs != null)
return false;
} else if (!descs.equals(other.descs))
return false;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (leaveDate == null) {
if (other.leaveDate != null)
return false;
} else if (!leaveDate.equals(other.leaveDate))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (price == null) {
if (other.price != null)
return false;
} else if (!price.equals(other.price))
return false;
return true;
}
@Override
public String toString() {
return "Goods [id=" + id + ", name=" + name + ", price=" + price + ", company=" + company + ", leaveDate="
+ leaveDate + ", descs=" + descs + "]";
}
}
数据访问层
package com.neu.dao;
import java.util.List;
import com.neu.entity.Goods;
public interface GoodsDao {
List<Goods> getAll(int pageNum,int pageSize) throws Exception;
Goods getById(String id) throws Exception;
int getCount() throws Exception;
}
package com.neu.dao;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.neu.entity.Goods;
public class GoodsDaoImpl implements GoodsDao {
@Override
public List<Goods> getAll(int pageNum,int pageSize) throws Exception {
Connection connection = JDBCUtil.getConnection();
String sql = "select * from tb_goods order by id limit ?,?";
ResultSet rs = JDBCUtil.executeQuery(connection, sql, new Object[] {(pageNum-1)*pageSize,pageSize});
List<Goods> list = new ArrayList<>();
Goods goods = null;
String id;
String name;
Double price;
String company;
Date leaveDate;
String descs;
while(rs.next()) {
id = rs.getString("id");
name = rs.getString("name");
price = rs.getDouble("price");
company = rs.getString("company");
leaveDate = rs.getDate("leaveDate");
descs = rs.getString("descs");
goods = new Goods(id, name, price, company, leaveDate, descs);
list.add(goods);
}
JDBCUtil.closeConnection(connection);
return list;
}
@Override
public Goods getById(String id) throws Exception {
Connection connection = JDBCUtil.getConnection();
String sql = "select * from tb_goods where id = ?";
ResultSet rs = JDBCUtil.executeQuery(connection, sql, new Object[] {id});
Goods goods = null;
String name;
Double price;
String company;
Date leaveDate;
String descs;
if(rs.next()) {
name = rs.getString("name");
price = rs.getDouble("price");
company = rs.getString("company");
leaveDate = rs.getDate("leaveDate");
descs = rs.getString("descs");
goods = new Goods(id, name, price, company, leaveDate, descs);
}
JDBCUtil.closeConnection(connection);
return goods;
}
@Override
public int getCount() throws Exception {
String sql = "select count(*) from tb_goods";
Connection connection = JDBCUtil.getConnection();
ResultSet rs = JDBCUtil.executeQuery(connection, sql, null);
int count = 0;
if(rs.next()) {
count = rs.getInt(1);
}
JDBCUtil.closeConnection(connection);
return count;
}
}
业务逻辑层
package com.neu.service;
import java.util.List;
import com.neu.entity.Goods;
public interface GoodsService {
List<Goods> getAll(int pageNum,int pageSize) throws Exception;
Goods getById(String id) throws Exception;
int getCount() throws Exception;
}
package com.neu.service;
import java.util.List;
import com.neu.dao.GoodsDao;
import com.neu.dao.GoodsDaoImpl;
import com.neu.entity.Goods;
public class GoodsServiceImpl implements GoodsService {
private GoodsDao goodsDao = new GoodsDaoImpl();
@Override
public List<Goods> getAll(int pageNum,int pageSize) throws Exception {
return goodsDao.getAll(pageNum,pageSize);
}
@Override
public int getCount() throws Exception {
return goodsDao.getCount();
}
@Override
public Goods getById(String id) throws Exception {
return goodsDao.getById(id);
}
}
表示层
package com.neu.servlet;
import java.io.IOException;
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.neu.entity.Goods;
import com.neu.service.GoodsService;
import com.neu.service.GoodsServiceImpl;
/**
* Servlet implementation class GetAllServlet
*/
@WebServlet("/GetAllServlet")
public class GetAllServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public GetAllServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
int pageNum = 1;
int pageSize = 3;
if(request.getParameter("pageNum")!=null) {
pageNum = Integer.parseInt(request.getParameter("pageNum"));
}
GoodsService goodsService = new GoodsServiceImpl();
try {
List<Goods> list = goodsService.getAll(pageNum,pageSize);
request.setAttribute("list", list);
int count = goodsService.getCount();
int num = count%pageSize == 0?count/pageSize:count/pageSize+1;
request.setAttribute("num", num);
request.setAttribute("currNum", pageNum);
request.getRequestDispatcher("/index.jsp").forward(request, response);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
package com.neu.servlet;
import java.io.IOException;
import java.util.ArrayList;
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.neu.entity.Goods;
import com.neu.service.GoodsService;
import com.neu.service.GoodsServiceImpl;
/**
* Servlet implementation class ShoppingServlet
*/
@WebServlet("/ShoppingServlet")
public class ShoppingServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public ShoppingServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String goodsId = request.getParameter("goodsId");
GoodsService goodsService = new GoodsServiceImpl();
try {
Goods goods = goodsService.getById(goodsId);
List<Goods> goodsList = (List<Goods>) request.getSession().getAttribute("goodsList");
if(goodsList == null) {
goodsList = new ArrayList<>();
request.getSession().setAttribute("goodsList", goodsList);
}
boolean f = true;
for(Goods good : goodsList) {
if(good.equals(goods)) {
f=false;
good.setCartNum(good.getCartNum()+1);
break;
}
}
if(f) {
goodsList.add(goods);
}
double sum = 0;
for(Goods good : goodsList) {
sum += good.getCartNum()*good.getPrice();
}
request.setAttribute("sum", sum);
request.getRequestDispatcher("/cart.jsp").forward(request, response);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
<%@ 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>Insert title here</title>
</head>
<body>
<h1 style="color: blue">商品信息浏览</h1>
<table border="1">
<tr>
<th>编号</th>
<th>名称</th>
<th>价格</th>
<th>出厂日期</th>
<th>生产厂家</th>
<th>描述</th>
<th>操作</th>
</tr>
<c:forEach items="${ list }" var="goods">
<tr>
<td>${ goods.id }</td>
<td>${ goods.name }</td>
<td>${ goods.price }</td>
<td>${ goods.leaveDate }</td>
<td>${ goods.company }</td>
<td>${ goods.descs==null?"暂无描述":goods.descs }</td>
<td>
<a href="${ pageContext.request.contextPath }/ShoppingServlet?goodsId=${ goods.id }">点击购买</a>
</td>
</tr>
</c:forEach>
<tr>
<td colspan="7" align="center">
<a href="${ pageContext.request.contextPath }/GetAllServlet">首页</a>
<c:if test="${ currNum <= 1 }"><a>上一页</a></c:if>
<c:if test="${ currNum > 1 }">
<a href="${ pageContext.request.contextPath }/GetAllServlet?pageNum=${currNum-1}">上一页</a>
</c:if>
<c:if test="${ currNum >= num }"><a>下一页</a></c:if>
<c:if test="${ currNum < num }">
<a href="${ pageContext.request.contextPath }/GetAllServlet?pageNum=${currNum+1}">下一页</a>
</c:if>
<a href="${ pageContext.request.contextPath }/GetAllServlet?pageNum=${num}">尾页</a>
</td>
</tr>
</table>
</body>
</html>
<%@ 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>Insert title here</title>
</head>
<body>
<h1 style="color: blue">我的购物车</h1>
<table border="1" width="400">
<tr>
<th>编号</th>
<th>名称</th>
<th>单价</th>
<th>数量</th>
<th>小计</th>
</tr>
<c:forEach items="${ goodsList }" var="goods">
<tr>
<td>${ goods.id }</td>
<td>${ goods.name }</td>
<td>${ goods.price }</td>
<td>${ goods.cartNum }</td>
<td style="color: red">${ goods.price*goods.cartNum }</td>
</tr>
</c:forEach>
<tr>
<td colspan="5" align="center">
总计:<span style="color: red">${ sum }</span>
</td>
</tr>
<tr>
<td colspan="5" align="center">
<a href="${ pageContext.request.contextPath }/GetAllServlet">返回商品信息页面</a>
</td>
</tr>
</table>
</body>
</html>