Skip to content
This repository has been archived by the owner on Dec 20, 2024. It is now read-only.

Commit

Permalink
20241215 完善购物车功能,小bug修复
Browse files Browse the repository at this point in the history
1. 完善购物车功能,现在点击结算后会清除购物车
2. 其他bug修复
  • Loading branch information
Steve5wutongyu6 committed Dec 15, 2024
1 parent 5577059 commit 43d5981
Show file tree
Hide file tree
Showing 9 changed files with 418 additions and 194 deletions.
533 changes: 363 additions & 170 deletions src/main/homework.sql

Large diffs are not rendered by default.

8 changes: 5 additions & 3 deletions src/main/java/action/AllOrderServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import bean.Olist;
import dao.OlistDAO;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
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.sql.SQLException;
import java.util.List;
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/action/SettlementServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@
import bean.Product;
import bean.User;
import dao.CartItemDAO;
import dao.ProductDAO;
import dao.OrderDAO;
import dao.ProductDAO;
import utils.JsonUtil;
import utils.UUIDUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
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 java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/bean/Olist.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ public Olist() {
super();
}

public Olist(String listid, String uid, String time, String item) {
this.listid = listid;
this.uid = uid;
this.time = time;
this.item = item;
}

@Override
public String toString() {
return "Olist{" +
Expand Down Expand Up @@ -51,11 +58,4 @@ public String getItem() {
public void setItem(String item) {
this.item = item;
}

public Olist(String listid, String uid, String time, String item) {
this.listid = listid;
this.uid = uid;
this.time = time;
this.item = item;
}
}
29 changes: 24 additions & 5 deletions src/main/java/dao/CartItemDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,33 @@ public List<CartItem> findCartItemsByUid(String uid) throws SQLException {

public void clearCartByUid(String uid) {
// 清空购物车(清空给定uid的cart和cartitem)
String sql = "DELETE FROM cart WHERE uid = ?";
String sqlGetOids = "SELECT oid FROM cart WHERE uid = ?";
String sqlDeleteCart = "DELETE FROM cart WHERE uid = ?";
String sqlDeleteCartItems = "DELETE FROM cartitem WHERE oid = ?";

try (Connection conn = DBUtil.getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, uid);
System.out.println(pstmt.toString()); // 打印SQL语句
pstmt.executeUpdate();
PreparedStatement pstmtGetOids = conn.prepareStatement(sqlGetOids);
PreparedStatement pstmtDeleteCart = conn.prepareStatement(sqlDeleteCart);
PreparedStatement pstmtDeleteCartItems = conn.prepareStatement(sqlDeleteCartItems)) {

// 获取所有与uid对应的oid
pstmtGetOids.setString(1, uid);
ResultSet rs = pstmtGetOids.executeQuery();

// 删除cartitem表中对应的oid项
while (rs.next()) {
String oid = rs.getString("oid");
pstmtDeleteCartItems.setString(1, oid);
pstmtDeleteCartItems.executeUpdate();
}

// 删除cart表中对应的uid项
pstmtDeleteCart.setString(1, uid);
pstmtDeleteCart.executeUpdate();

} catch (SQLException e) {
e.printStackTrace();
}
}

}
7 changes: 3 additions & 4 deletions src/main/java/dao/OrderDAO.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package dao;

import bean.Order;
import utils.DBUtil;

import java.sql.Connection;
Expand All @@ -12,10 +11,10 @@ public class OrderDAO {
/**
* 保存订单信息到数据库
*
* @param uid 用户ID
* @param uid 用户ID
* @param listid 订单ID
* @param time 下单时间
* @param item 商品信息的JSON字符串
* @param time 下单时间
* @param item 商品信息的JSON字符串
* @throws SQLException 如果发生SQL异常
*/
public void saveOrder(String uid, String listid, String time, String item) throws SQLException {
Expand Down
6 changes: 6 additions & 0 deletions src/main/webapp/allorders.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,31 @@
font-family: Arial, sans-serif;
margin: 20px;
}
table {
width: 100%;
border-collapse: collapse;
}
th, td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
th {
background-color: #f2f2f2;
}
tr:hover {
background-color: #f1f1f1;
}
a {
text-decoration: none;
color: blue;
}
a:hover {
text-decoration: underline;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/webapp/cart.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
</table>
<h2>总价: <%= request.getAttribute("totalPrice") %> 元</h2>
<div class="buttons-container">
<a href="ProductListServlet">继续购物</a>
<a href="SelectAllProductByPidAndCidServlet">继续购物</a>
<a href="SettlementServlet">结算</a>
</div>
</body>
Expand Down
2 changes: 2 additions & 0 deletions src/main/webapp/ofinish.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
text-align: center;
margin-top: 50px;
}
h1 {
color: green;
}
p {
font-size: 18px;
}
Expand Down

0 comments on commit 43d5981

Please sign in to comment.