一.项目版本
1.数据库版本
1)将购物车保存到数据库中(永久的)
2)优点:只要你登录了,并且选购了,不管你换不换设备,数据仍然在,方便了用户。
3) 缺点:太占数据库的内存,性能差,效率低。
2. session版本(我们今天主要讲的内容是该版本)
1)将购物车保存到session中 (临时的)
2)优点:性能佳 效率高。
3)缺点:会话具有时效性,超时数据会消失。
3.提示语法:
1)存到session: session.setAttribute (键String,值Object)。
2)从session取值:session.getAttribute(键) 返回Object。
3)我们是将订单项的集合List<OrderItem>保存到session中 。
4)实体类:Goods(商品类):gid gname gprice ginfo gpath(图片路径);
OrderItem(订单项类):sumPrice(商品总价) = goods.getBprice()(单价)*gnumber(数量);
5)doshop.jsp:接收gid ,根据gid拿到商品对象 形成订单项 加到集合中保存到session。
二.项目图文展示
1.购物车首页的实现
1)编写 Goods(商品类) 实体类,DBhelper类(代码略)
2) 编写Dao类(查询所有商品&查询单个商品)代码如下:
public class GoodsDao {
/**
* 查询所有的商品
* @return 返回商品集合
*/
public ArrayList<Goods> getAll(){
ArrayList<Goods> glist=new ArrayList<>();
Connection con=null;
PreparedStatement ps=null;
ResultSet rs=null;
try {
con=DBHelper.getCon();
String sql="select*from goods";
ps=con.prepareStatement(sql);
rs=ps.executeQuery();
while(rs.next()) {
glist.add(new Goods(rs.getInt(1),rs.getString(2),rs.getDouble(3),rs.getString(4),rs.getString(5)));
}
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.closeDb(con, ps, rs);
}
return glist;
}
/**
* 根据商品编号查询商品对象
* @param bid 要查询的商品编号
* @return 返回查询到的商品,否则返回空
*/
public Goods getById(int bid) {
Connection con=null;
PreparedStatement ps=null;
ResultSet rs=null;
Goods g=null;
try {
con=DBHelper.getCon();
ps=con.prepareStatement("select*from goods where bid="+bid);
rs=ps.executeQuery();
if(rs.next()) {
g=new Goods(rs.getInt(1),rs.getString(2),rs.getDouble(3),rs.getString(4),rs.getString(5));
}
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}finally {
DBHelper.closeDb(con, ps, rs);
}
return g;
}
3)编写购物车首页代码
<%@page import="com.zking.entity.Goods"%>
<%@page import="java.util.ArrayList"%>
<%@page import="com.zking.dao.GoodsDao"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>
<!-- 引用jQuery库 -->
<script type="text/javascript" src="js/jquery-3.3.1.js"></script>
<script type="text/javascript" src="js/bootstrap.js"></script>
<link rel="stylesheet" type="text/css" href="css/bootstrap.css">
</head>
<script type="text/javascript">
function gm(bid) {
//跳转到do页面 顺便传个编号
location.href="dogwc.jsp?bid="+bid;
}
</script>
<body>
<table class="table table-hover"><!--表格 鼠标可移上移下 -->
<tr>
<td>商品编号</td>
<td>商品名称</td>
<td>商品单价</td>
<td>商品介绍</td>
<td>商品图片</td>
<td>操作</td>
</tr>
<%
GoodsDao gd=new GoodsDao();
ArrayList<Goods> glist=gd.getAll();
for(Goods g:glist){
%>
<tr>
<td><%=g.getBid() %></td>
<td><%=g.getBname() %></td>
<td><%=g.getBprice() %></td>
<td><%=g.getBinfo() %></td>
<td>
<img alt="" src="<%=g.getBface()%>">
</td>
<td>
<!-- <a href="#">加入购物车 </a> 状态: 红色-danger 绿色-success 黄色-warning -->
<button onclick="gm(<%=g.getBid() %>)" class="btn btn-info">添加购物车</button>
</td>
</tr>
<%} %>
</table>
</body>
</html>
4)购物车首页界面图展示
2.订单页面的实现(商品加入购物车)
1)编写OrderItem(订单项类)实体类 (代码如下)
public class OrderItem {
private Goods goods;//商品对象
private int gnumber;//商品数量
private double sumPrice;//商品总价
public OrderItem() {
super();
}
public OrderItem(int gnumber, double sumPrice) {
super();
this.gnumber = gnumber;
this.sumPrice = sumPrice;
}
public OrderItem(Goods goods, int gnumber, double sumPrice) {
super();
this.goods = goods;
this.gnumber = gnumber;
this.sumPrice = sumPrice;
}
public Goods getGoods() {
return goods;
}
public void setGoods(Goods goods) {
this.goods = goods;
}
public int getGnumber() {
return gnumber;
}
public void setGnumber(int gnumber) {
this.gnumber = gnumber;
}
public double getSumPrice() {
return sumPrice;
}
public void setSumPrice() {
this.sumPrice = goods.getBprice()*gnumber;
}
@Override
public String toString() {
return "OrderItem [goods=" + goods + ", gnumber=" + gnumber + ", sumPrice=" + sumPrice + "]";
}
}
2)dogwc.jsp页面(在商品主页点击加入购物车后携带商品编号进入的处理页面)
<%@page import="java.util.ArrayList"%>
<%@page import="com.zking.dao.GoodsDao"%>
<%@page import="com.zking.entity.OrderItem"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
//获取商品订单的数量
String number=request.getParameter("gn");
int count=1;
if(number!=null){
count=Integer.valueOf(number);
}
//接收商品的编号
String id=request.getParameter("bid");
int bid=Integer.valueOf(id);
//构造小订单对象
OrderItem oi=new OrderItem();
//给属性赋值
oi.setGoods(new GoodsDao().getById(bid));
//订单中的商品数量
oi.setGnumber(1);
//订单总价
oi.setSumPrice();
//获取session中的订单集合
ArrayList<OrderItem> olist=(ArrayList<OrderItem>)session.getAttribute("olist");
if(olist==null){
//创建订单集合
olist=new ArrayList<OrderItem>();
}
boolean b=true;//表示默认没有相同订单
//遍历订单集合,判断是否已存在相同商品订单
for(int i=0;i<olist.size();i++){
if(bid==olist.get(i).getGoods().getBid()){
//判断count是否是1,如果从index页面过来就是1,从spcar页面过来就不是1
if(count==1){
//修改数量:原来的数量+1
olist.get(i).setGnumber(olist.get(i).getGnumber()+1);
//修改总价
olist.get(i).setSumPrice();
}else{
//修改数量:原来的数量修改为count
olist.get(i).setGnumber(count);
//修改总价
olist.get(i).setSumPrice();
}
//表示有相同订单
b=false;
}
}
if(b){
//把订单放到ArrayList集合中;
olist.add(oi);
}
//遍历订单集合
double sumPrice=0;
for(int i=0;i<olist.size();i++){
sumPrice+=olist.get(i).getSumPrice();
}
//把订单放到ArrayList集合中
olist.add(oi);
//把集合放到session中
session.setAttribute("olist",olist);
session.setAttribute("sumPrice",sumPrice);
//跳转页面
response.sendRedirect("spcar.jsp");
%>
3)spcar.jsp(订单页面的代码)
<%@page import="com.zking.entity.OrderItme"%>
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!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>
<!-- 应用jQuery库 -->
<script type="text/javascript" src="js/jquery-3.3.1.js"></script>
<script type="text/javascript" src="js/bootstrap.js"></script>
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css"/>
<script type="text/javascript">
function n(q,w,e) {
var t=document.getElementById(q).value;
if (t>0) {
if (e==1) {
t++;
location.href="dogwc.jsp?bid="+w+"&gn="+t;
}else if(e==2){
t--;
location.href="dogwc.jsp?bid="+w+"&gn="+t;
}
}else
alert("不能");
}
function a(obj,bid) {
var gn=obj.value;
location.href="dogwc.jsp?bid="+bid+"&gn="+gn;
}
</script>
</head>
<body>
<h1> <a href="Index.jsp"><span class="glyphicon glyphicon-home"></span></a></h1>
<table class="table table-hover">
<tr>
<td>商品图片</td>
<td>商品名称</td>
<td>商品单价</td>
<td>商品介绍</td>
<td>商品数量</td>
<td>商品总价</td>
<td><a>
<span class="glyphicon glyphicon-cog"></span>
</a>
</td>
</tr>
<%
ArrayList<OrderItme> li= (ArrayList<OrderItme>)session.getAttribute("li");
for(int i=0;i<li.size();i++){
%>
<tr>
<td><img src="<%=li.get(i).getGo().getBface() %>"></td>
<td><%=li.get(i).getGo().getBname() %></td>
<td><%=li.get(i).getGo().getBprice() %></td>
<td><%=li.get(i).getGo().getBinfo() %></td>
<td>
<button onclick="n(<%=li.get(i).getGo().getBid()%>,<%=li.get(i).getGo().getBid()%>,2)" class="btn btn-info">-</button>
<input id="<%=li.get(i).getGo().getBid()%>" onblur="a(this,<%=li.get(i).getGo().getBid() %>)" style="width:40px" type="text" value="<%=li.get(i).getGnu()%>">
<button onclick="n(<%=li.get(i).getGo().getBid()%>,<%=li.get(i).getGo().getBid()%>,1)" class="btn btn-info">+</button>
</td>
<td><%=li.get(i).getSum()%></td>
<td>
<a href="del.jsp?bid=<%=li.get(i).getGo().getBid()%>">
<span class="glyphicon glyphicon-trash"></span>
</a>
</td>
<%} %>
</table>
<p align="right" style="margin-right:40px">
<button class="btn btn-info"><%=session.getAttribute("p") %></button>
</p>
</body>
</html>
4)订单页面的效果图
这世上没有神仙,也无需立庙,因为每一缕升起的炊烟都是漂浮人间的思念。