add<购物车列表>

master
liyansheng 2024-12-21 13:54:25 +08:00
parent 6d737d91b3
commit 3ba38e0c33
7 changed files with 165 additions and 0 deletions

View File

@ -0,0 +1,52 @@
package example.controller;
import example.model.Computer;
import example.service.ICartService;
import example.service.impl.CartServiceImpl;
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;
@WebServlet("/cart")
public class CartServlet extends HttpServlet {
ICartService cartService = new CartServiceImpl();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String action = req.getParameter("action");
switch (action){
case "list":
try {
toCart(req, resp);
} catch (Exception e) {
throw new RuntimeException(e);
}
break;
case "add":
String id = req.getParameter("id");
req.getSession().setAttribute("cart",id);
resp.sendRedirect("/cart");
break;
case "remove":
String removeId = req.getParameter("id");
req.getSession().removeAttribute(removeId);
resp.sendRedirect("/cart");
break;
default:
req.getRequestDispatcher("/WEB-INF/jsp/cart.jsp").forward(req,resp);
break;
}
}
private void toCart(HttpServletRequest req, HttpServletResponse resp) throws Exception {
List<Computer> computers = cartService.myCart(16);
req.setAttribute("computers",computers);
req.getRequestDispatcher("cart.jsp").forward(req,resp);
}
}

View File

@ -0,0 +1,37 @@
package example.dao;
import example.model.Computer;
import example.utils.DBUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import java.util.List;
public class CartDao {
private final QueryRunner queryRunner = new QueryRunner(DBUtils.getDataSource());
// 加入购物车
public int addCart(Integer user_id, Integer product_id) throws Exception {
String sql = "insert into cart(user_id, product_id) values(?,?)";
return queryRunner.update(sql, user_id, product_id);
}
// 移除购物车
public int deleteCart(Integer user_id, Integer product_id) throws Exception {
String sql = "delete from cart where user_id=? and product_id=?";
return queryRunner.update(sql, user_id, product_id);
}
// 某用户的购物车
public List<Computer> getCart(Integer user_id) throws Exception {
String sql = "select\n" +
"\tc2.*\n" +
"from\n" +
"\tcart c\n" +
"left join computer c2 \n" +
"on\n" +
"\tc.product_id = c2.id\n" +
"where\n" +
"\tc.user_id = ?";
return queryRunner.query(sql,new BeanListHandler<>(Computer.class),user_id);
}
}

View File

@ -0,0 +1,10 @@
package example.model;
public class Cart {
private Integer id;
private Integer userId;
private Integer productId;
}

View File

@ -0,0 +1,9 @@
package example.service;
import example.model.Computer;
import java.util.List;
public interface ICartService {
List<Computer> myCart(Integer userId) throws Exception;
}

View File

@ -0,0 +1,18 @@
package example.service.impl;
import example.dao.CartDao;
import example.model.Computer;
import example.service.ICartService;
import java.util.Collections;
import java.util.List;
public class CartServiceImpl implements ICartService {
CartDao cartDao = new CartDao();
@Override
public List<Computer> myCart(Integer userId) throws Exception {
return cartDao.getCart(userId);
}
}

38
src/main/webapp/cart.jsp Normal file
View File

@ -0,0 +1,38 @@
<%@ page contentType="text/html; charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>购物车</title>
</head>
<body>
<h1>购物车</h1>
<table border="1">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Price</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<c:forEach var="computer" items="${computers}">
<tr>
<td>${computer.id}</td>
<td>${computer.name}</td>
<td>${computer.price}</td>
<td>
<a href="/deleteComputer?id=${computer.id}">移出</a>
</td>
</tr>
</c:forEach>
</tbody>
</table>
<br/>
<br>
<a href="/">主页</a>
</body>
</html>

View File

@ -8,5 +8,6 @@
<a href="/login">登录</a>
<a href="/computerList">电脑列表</a>
<a href="/user?action=list">用户列表</a>
<a href="/cart?action=list">我的购物车</a>
</body>
</html>