目录
一.效果预览
二.实现思路
2.1 实现结算
①形成订单,并赋值
②将订单插入到数据表tb_order中,同时将该订单所对应的订单项插入到数据表tb_orderitem中
2.2 查看我的订单
①底层代码
②前端绑值
2.3 查看订单详情
①页面跳转,传参
②底层代码
③前端绑值
一.效果预览
二.实现思路
2.1 实现结算
①形成订单,并赋值
//从session中拿购物车
List<OrderItem> items =(List<OrderItem>)session.getAttribute("mycart");
//从session中拿用户
User user = (User)session.getAttribute("user");
//形成一个订单
Order order = new Order();
//给订单赋值 缺少oid!!!!!!!
order.setItems(items);
order.calcSum();//务必先给集合赋值
order.setUser(user);
order.setOaddress(user.getUaddress());//调用增加订单的方法
int n = iob.addOrder(order);
if(n>0) {
//说明结算成功
session.setAttribute("mycart", null);
//跳转到我的订单页面
resp.sendRedirect("orders.do");
}
else {
resp.sendRedirect("cart.jsp?index=5");
}
②将订单插入到数据表tb_order中,同时将该订单所对应的订单项插入到数据表tb_orderitem中
package com.zking.entity;
/**
* 实体类订单类
* @author PC
*
*/
import java.io.Serializable;
import java.util.List;
public class Order implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private int oid;//订单编号
private double osumprice;//订单总价
private User user;//用户对象
private String oaddress;//订单地址
//一个订单由多个订单项组成的√
private List<OrderItem> items=null;
//计算订单总价
public void calcSum() {
double sum = 0;
for (OrderItem oi : items) {
sum+=oi.getOiprice();//累加求小计的和
}
this.osumprice=sum;//再计算总价
}
public int getOid() {
return oid;
}
public void setOid(int oid) {
this.oid = oid;
}
public double getOsumprice() {
return osumprice;
}
public void setOsumprice(double osumprice) {
this.osumprice = osumprice;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String getOaddress() {
return oaddress;
}
public void setOaddress(String oaddress) {
this.oaddress = oaddress;
}
public List<OrderItem> getItems() {
return items;
}
public void setItems(List<OrderItem> items) {
this.items = items;
}
public Order() {
// TODO Auto-generated constructor stub
}
public Order(int oid, double osumprice, User user, String oaddress, List<OrderItem> items) {
this.oid = oid;
this.osumprice = osumprice;
this.user = user;
this.oaddress = oaddress;
this.items = items;
}
public Order(double osumprice, User user, String oaddress, List<OrderItem> items) {
this.osumprice = osumprice;
this.user = user;
this.oaddress = oaddress;
this.items = items;
}
@Override
public String toString() {
return "Order [oid=" + oid + ", osumprice=" + osumprice + ", user=" + user + ", oaddress=" + oaddress
+ ", items=" + items + "]";
}
}
package com.zking.entity;
import java.io.Serializable;
/**
* 实体类:订单项类
* @author PC
*
*/
public class OrderItem implements Serializable{
private static final long serialVersionUID = 1L;
private int oiid;//订单项编号
private int oid;//订单编号
// private int gid;//商品编号
private Goods goods;//商品对象 可以间接拿到其所有数据
private int oinum;//购买数量
private double oiprice;//小计
public void calcxj() {
//小计=单价*数量
this.oiprice=this.goods.getGprice()*this.oinum;
String ss = String.format("%.1f",oiprice);
double s = Double.parseDouble(ss);
this.oiprice=s;
}
public int getOiid() {
return oiid;
}
public void setOiid(int oiid) {
this.oiid = oiid;
}
public int getOid() {
return oid;
}
public void setOid(int oid) {
this.oid = oid;
}
public Goods getGoods() {
return goods;
}
public void setGoods(Goods goods) {
this.goods = goods;
}
public int getOinum() {
return oinum;
}
public void setOinum(int oinum) {
this.oinum = oinum;
}
public double getOiprice() {
return oiprice;
}
public void setOiprice(double oiprice) {
this.oiprice = oiprice;
}
public OrderItem() {
// TODO Auto-generated constructor stub
}
public OrderItem(int oiid, int oid, Goods goods, int oinum, double oiprice) {
this.oiid = oiid;
this.oid = oid;
this.goods = goods;
this.oinum = oinum;
this.oiprice = oiprice;
}
public OrderItem(int oid, Goods goods, int oinum, double oiprice) {
this.oid = oid;
this.goods = goods;
this.oinum = oinum;
this.oiprice = oiprice;
}
@Override
public String toString() {
return "OrderItem [oiid=" + oiid + ", oid=" + oid + ", goods=" + goods + ", oinum=" + oinum + ", oiprice="
+ oiprice + "]";
}
}
package com.zking.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.zking.entity.Order;
import com.zking.entity.OrderItem;
import com.zking.entity.User;
import com.zking.util.DBHelper;
/**
* 数据访问层 tb_order
* @author PC
*
*/
public class OrderDao implements IOrderDao {
private Connection con = null;
private PreparedStatement ps = null;
private ResultSet rs = null;
boolean flag;
/**
* 根据用户拿到订单
* @param u 用户对象
* @return 订单集合
*/
public List<Order> getAllByUser(User u){
List<Order> ls = new ArrayList<Order>();
try {
con=DBHelper.getCon();
String sql="select * from (select * from tb_order order by oid desc) where uuid="+u.getUuid();
ps=con.prepareStatement(sql);
rs=ps.executeQuery();
while(rs.next()) {
Order o =new Order();
o.setOid(rs.getInt(1));
String ss = String.format("%.1f",rs.getDouble(2));
double s = Double.parseDouble(ss);
o.setOsumprice(s);
o.setUser(u);
o.setOaddress(rs.getString(4));
ls.add(o);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.myClose(con, ps, rs);
}
return ls;
}
/**
* 单个查询
* @param oid
* @return
*/
public Order getOrder(String oid) {
Order o = new Order();
OrderItemDao odd = new OrderItemDao();
try {
con=DBHelper.getCon();
String sql="select * from tb_orderitem where oid="+oid;
ps=con.prepareStatement(sql);
rs=ps.executeQuery();
if(rs.next()) {
o.setOid(rs.getInt(1));
o.setOsumprice(rs.getDouble(2));
o.setOaddress(rs.getString(4));
//拿的该订单所对应的订单项集合
List<OrderItem> items = odd.getAllByOid(oid);
o.setItems(items);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.myClose(con, ps, rs);
}
return o;
}
/**
* 增加订单
* @param order 订单对象
* @return 影响行数
*/
public int addOrder(Order order) {
int n =0;
int oid = getID("tb_order", "oid");///!!!赋值 订单编号
OrderItemDao odao = new OrderItemDao();
try {
con=DBHelper.getCon();
String sql="insert into tb_order(oid,osumprice,uuid,oaddress) values(?,?,?,?)";
ps=con.prepareStatement(sql);
//给占位符赋值
ps.setInt(1, oid);
ps.setDouble(2, order.getOsumprice());
ps.setInt(3, order.getUser().getUuid());
ps.setString(4, order.getOaddress());
n=ps.executeUpdate();
if(n>0) {//说明订单下单成功
//那么就把该订单所对应的订单项全部加入到数据库中
//拿到该订单所对应的订单项集合
List<OrderItem> items = order.getItems();
//循环遍历
for (OrderItem oi : items) {
//接下来我需要将所有的oi都要进tb_orderitem表
oi.setOid(oid);//关键
odao.addOrderItem(oi);
//把对应商品的销量+购买数量 库存-购买数量
new GoodsDao().updateXL(oi.getGoods().getGid(),oi.getOinum());
}
}
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.myClose(con, ps, rs);
}
return n;
}
/**
* 删除订单
* @param oid
* @return
*/
public boolean deleteOrderByOid(int oid) {
try {
con=DBHelper.getCon();
String sql="delete from tb_order where oid="+oid;
ps=con.prepareStatement(sql);
if(ps.executeUpdate()>0) {
flag = true;
}
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.myClose(con, ps, rs);
}
return flag;
}
/**
* 标识列实现 ②取表的最大序号+1
* @param tabName 表名
* @param colName 列名
* @return
*/
public int getID(String tabName,String colName) {//表名 列名
int n = 0;
try {
con=DBHelper.getCon();
String sql="select nvl(max("+colName+"),0)+1 from "+tabName;
ps=con.prepareStatement(sql);
rs=ps.executeQuery();
if(rs.next()) {
n=rs.getInt(1);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.myClose(con, ps, rs);
}
return n;
}
}
2.2 查看我的订单
①底层代码
package com.zking.servlet;
import java.io.IOException;
import java.io.PrintWriter;
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 javax.servlet.http.HttpSession;
import com.zking.biz.GoodsBiz;
import com.zking.biz.IGoodsBiz;
import com.zking.biz.IOrderBiz;
import com.zking.biz.IUserBiz;
import com.zking.biz.OrderBiz;
import com.zking.biz.UserBiz;
import com.zking.entity.Goods;
import com.zking.entity.Order;
import com.zking.entity.User;
/**
* 订单们控制器 多个订单
* @author PC
*
*/
@WebServlet("/orders.do")
public class OrdersServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//设置编码
req.setCharacterEncoding("utf-8");
resp.setContentType("text/html; charset=UTF-8");
//拿session和out
HttpSession session = req.getSession();
PrintWriter out = resp.getWriter();
//从session中拿用户
User u =(User)session.getAttribute("user");
//servlet调用biz
IOrderBiz iob = new OrderBiz();
//调用获取订单集合方法
List<Order> ordersls = iob.getAllByUser(u);
//存起来
req.setAttribute("ordersls", ordersls);
//转发到myorder.jsp进行绑值
req.getRequestDispatcher("myorder.jsp?index=6").forward(req, resp);
}
}
②前端绑值
<%@ 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 lang="en">
<head>
<title>我的订单</title>
<!-- 支持手机端 -->
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<!-- 引入CSS -->
<link rel="stylesheet" href="CSS/bootstrap.css" />
<!-- 引入Bootstrap图标的样式 -->
<link rel="stylesheet" href="font/bootstrap-icons.css" />
<style>
.navbar-collapse {
/*1代表弹性扩大占用父容器剩余部分 0代表不占用*/
flex-grow: 0;
}
.active {
/*加粗*/
font-weight: bold;
}
.text-right button{
width: 120px;/*宽度*/
height: 30px;/*高度*/
line-height: 10px;/*行高*/
margin-top: 10px;/*上间距*/
}
</style>
</head>
<body>
<!--页面包含 -->
<jsp:include page="header.jsp"></jsp:include>
<!-- 我的订单 -->
<div class="container">
<!--当订单集合为空时 -->
<c:if test="${empty ordersls}">
<div style="margin-top: 50px;margin-bottom:50px" class="h3 text-center text-pink">亲爱的${user.uname},订单空空如也喔<a href="index.do?index=1">快去奖励自己吧!</a></div>
<div class="text-center">
<img width="700px" src="img/bz11.jpg">
</div>
</c:if>
<!--当订单集合不为空时 -->
<c:if test="${not empty ordersls}">
<!-- text-center居中 -->
<table style="margin-top: 20px;" class="table table-hover text-center ">
<!-- class="bg-primary" 加颜色 -->
<thead class="bg-primary" style="color: aliceblue;">
<tr>
<th scope="col">订单编号</th>
<th scope="col">收货地址</th>
<th scope="col">订单总价</th>
<th scope="col">购买者</th>
<th scope="col">操作</th>
</tr>
</thead>
<tbody>
<!--绑定订单的值 -->
<c:forEach items="${ordersls}" var="o">
<tr style="height:80px;line-height: 50px">
<th scope="row">${o.oid}</th>
<td>${o.oaddress}</td>
<td>${o.osumprice}¥</td>
<td>${o.user.uname}</td>
<td>
<a href="deleteOrder.do?oid=${o.oid}" onclick="return confirm('确定删除嘛?(^.^)')" >删除订单 </a>
<a href="orderInfo.do?oid=${o.oid}">查看订单详情 </a>
</td>
</tr>
</c:forEach>
</tbody>
</table>
</c:if>
</div>
<!-- 引入jQuery的类库 -->
<!-- 再引入Bootstrap的类库 -->
<script src="js/jquery-3.6.4.js"></script>
<script src="js/bootstrap.js"></script>
</body>
</html>
2.3 查看订单详情
①页面跳转,传参
//跳转 转发到订单详情页面进行绑值
req.getRequestDispatcher("orderinfo.jsp").forward(req, resp);
②底层代码
package com.zking.servlet;
import java.io.IOException;
import java.io.PrintWriter;
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 javax.servlet.http.HttpSession;
import com.zking.biz.IOrderBiz;
import com.zking.biz.IUserBiz;
import com.zking.biz.OrderBiz;
import com.zking.biz.UserBiz;
import com.zking.entity.Order;
import com.zking.entity.User;
/**
* 查看订单详情控制器
* @author PC
*
*/
@WebServlet("/orderInfo.do")
public class OrderInfoServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//设置编码
req.setCharacterEncoding("utf-8");
resp.setContentType("text/html; charset=UTF-8");
//拿session和out
HttpSession session = req.getSession();
PrintWriter out = resp.getWriter();
//接收oid
String oid=req.getParameter("oid");
//servlet调biz层
IOrderBiz iob = new OrderBiz();
//调用单个查询的方法
Order order = iob.getOrder(oid);
//存起来
req.setAttribute("myorder",order);//键 值
//跳转 转发到订单详情页面进行绑值
req.getRequestDispatcher("orderinfo.jsp").forward(req, resp);
//两种思路
/**
* select * from tb_orderitem where oid=?;
* 1.根据订单编号查找到对应的订单项集合 存起来 去订单详情页面绑定值
*
* select * from tb_order where oid=?; 单个查询 在order实体类写了一个items
* 2.根据订单编号查找到对应的订单 把订单存起来 去订单详情界面 即可以绑定该订单的值又可以绑定订单详情的值 √
*
*
*/
}
}
③前端绑值
<%@ 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 lang="en">
<head>
<title>查看订单详情</title>
<!-- 支持手机端 -->
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<!-- 引入CSS -->
<link rel="stylesheet" href="CSS/bootstrap.css" />
<!-- 引入Bootstrap图标的样式 -->
<link rel="stylesheet" href="font/bootstrap-icons.css" />
<style>
.navbar-collapse {
/*1代表弹性扩大占用父容器剩余部分 0代表不占用*/
flex-grow: 0;
}
.active {
/*加粗*/
font-weight: bold;
}
.text-right button{
width: 120px;/*宽度*/
height: 30px;/*高度*/
line-height: 10px;/*行高*/
margin-top: 10px;/*上间距*/
}
</style>
</head>
<body>
<!-- 订单详情 -->
<div class="container" style="margin-top: 40px">
<!--当订单为空时 -->
<c:if test="${empty myorder}">
<jsp:forward page="orders.do?index=6"></jsp:forward>
</c:if>
<!--当购物车集合不为空时 -->
<c:if test="${not empty myorder}">
<!-- text-center居中 -->
<table style="margin-top: 20px;" class="table table-hover text-center ">
<!-- class="bg-primary" 加颜色 -->
<thead class="bg-primary" style="color: aliceblue;">
<tr>
<th scope="col">订单项编号</th>
<th scope="col">订单编号</th>
<th scope="col">商品名称</th>
<th scope="col">商品价格</th>
<th scope="col">商品图片</th>
<th scope="col">购买数量</th>
<th scope="col">小计</th>
</tr>
</thead>
<tbody>
<!--对象不能自己遍历 foreach 跟实体类一致 -->
<!--绑对应订单项的值 -->
<c:forEach items="${myorder.items}" var="oi">
<tr style="height:80px;line-height: 50px">
<th scope="row">${oi.oiid}</th>
<td>${oi.oid}</td>
<td>${oi.goods.gname}</td>
<td>${oi.goods.gprice}元</td>
<td><img width="80px" src="${oi.goods.gpath}"></td>
<td>${oi.oinum}</td>
<td><span class="xx">${oi.oiprice}</span>元</td>
</tr>
</c:forEach>
</tbody>
</table>
<!-- 合计 -->
<div class="text-right">
<h2>合计:<span id="sums">229.3</span>元</h2>
<a href="orders.do?index=6"> <button type="button" class="btn btn-success" >点击返回</button></a>
</div>
</c:if>
</div>
<!-- 引入jQuery的类库 -->
<!-- 再引入Bootstrap的类库 -->
<script src="js/jquery-3.6.4.js"></script>
<script src="js/bootstrap.js"></script>
<script type="text/javascript">
//加载DOM jquery
$(function(){
//统计价格
var sum = 0.0;
//遍历
$(".xx").each(function(i,v){//下标 值
//累计求和
sum+=parseFloat(v.innerHTML);
});
//保留1位小数
sum=sum.toFixed(2);
//给span标签赋值
$("#sums").html(sum);
})
//即要拿id 就直接把id设置成gid
function myf(gid) {
//拿文本框的数量
var num=$("#"+gid).val();
//跳转到servlet
location.href="updateCart.do?gid="+gid+"&num="+num;
}
/* //如果仍然出现很多位小数 则采用以下方法保留1位小数
String s = String.format("%.1f",this.oiprice);
this.oiprice=Double.parseDouble(s); */
</script>
</body>
</html>