add<订单页>

master
liyansheng 2024-12-21 14:30:26 +08:00
parent 3ba38e0c33
commit ed4b1b4f3e
7 changed files with 220 additions and 0 deletions

View File

@ -0,0 +1,31 @@
package example.controller;
import example.service.IOrdersService;
import example.service.impl.OrdersServiceImpl;
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;
@WebServlet("/orders")
public class OrdersServlet extends HttpServlet {
IOrdersService ordersService=new OrdersServiceImpl();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String action = req.getParameter("action");
switch (action) {
case "all":
ordersService.allOrder(req, resp);
break;
case "delete":
// ordersService.delete(req, resp);
break;
}
}
}

View File

@ -0,0 +1,29 @@
package example.dao;
import example.model.Orders;
import example.utils.DBUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import java.util.List;
public class OrdersDao {
private final QueryRunner queryRunner = new QueryRunner(DBUtils.getDataSource());
public List<Orders> getAllOrders() {
String sql = "select id,user_id userId,order_date orderDate,total_price totalPrice,address ,remark from orders o ";
try {
return queryRunner.query(sql, new BeanListHandler<>(Orders.class));
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
OrdersDao ordersDao = new OrdersDao();
System.out.println(ordersDao.getAllOrders());
}
}

View File

@ -0,0 +1,77 @@
package example.model;
import java.time.LocalDateTime;
public class Orders {
private Integer id;
private Integer userId;
private LocalDateTime orderDate;
private Double totalPrice;
private String address;
private String remark;
@Override
public String toString() {
return "Orders{" +
"id=" + id +
", userId=" + userId +
", orderDate=" + orderDate +
", totalPrice=" + totalPrice +
", address='" + address + '\'' +
", remark='" + remark + '\'' +
'}';
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public LocalDateTime getOrderDate() {
return orderDate;
}
public void setOrderDate(LocalDateTime orderDate) {
this.orderDate = orderDate;
}
public Double getTotalPrice() {
return totalPrice;
}
public void setTotalPrice(Double totalPrice) {
this.totalPrice = totalPrice;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
}

View File

@ -0,0 +1,12 @@
package example.service;
import example.model.Orders;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
public interface IOrdersService {
void allOrder(HttpServletRequest req, HttpServletResponse resp);
}

View File

@ -0,0 +1,26 @@
package example.service.impl;
import example.dao.OrdersDao;
import example.model.Orders;
import example.service.IOrdersService;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
public class OrdersServiceImpl implements IOrdersService {
OrdersDao ordersDao=new OrdersDao();
@Override
public void allOrder(HttpServletRequest req, HttpServletResponse resp) {
List<Orders> allOrders = ordersDao.getAllOrders();
req.setAttribute("Orders",allOrders);
try {
req.getRequestDispatcher("orders.jsp").forward(req,resp);
} catch (Exception e) {
e.printStackTrace();
}
}
}

View File

@ -9,5 +9,6 @@
<a href="/computerList">电脑列表</a>
<a href="/user?action=list">用户列表</a>
<a href="/cart?action=list">我的购物车</a>
<a href="/orders?action=all">所有订单</a>
</body>
</html>

View File

@ -0,0 +1,44 @@
<%@ 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>订单编号</th>
<th>用户ID</th>
<th>订单日期</th>
<th>总金额</th>
<th>地址</th>
<th>商品信息</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<c:forEach var="order" items="${Orders}">
<tr>
<td>${order.id}</td>
<td>${order.userId}</td>
<td>${order.orderDate}</td>
<td>${order.totalPrice}</td>
<td>${order.address}</td>
<td>${order.remark}</td>
<td>
<a href="/orders?action=delete&id=${order.id}">删除</a>
</td>
</tr>
</c:forEach>
</tbody>
</table>
<br/>
<br>
<a href="/">主页</a>
</body>
</html>