家居網購專案實現010
以下皆為部分程式碼,詳見 https://github.com/liyuelian/furniture_mall.git
24.bugFix-新增購物車按鈕動態處理
24.1需求分析/圖解
- 如某個家居的庫存量為0,前臺的“add to cart”按鈕顯示為“暫時缺貨”
- 後臺也要求新增校驗,只有在庫存>0時,才能新增到購物車
24.2程式碼實現
-
修改customer/index.jsp
-
CartServlet新增判斷,如果在新增購物車時,該家居庫存為0,就停止業務
24.3完成測試
25.功能23-附加功能-訂單管理
25.1需求分析/圖解
點選訂單管理,前往order.jsp頁面進行訂單管理
點選詳情的眼睛圖示,可以顯示該訂單詳情,即顯示某個訂單的所有訂單項
25.2思路分析
點選訂單管理按鈕,跳轉到訂單管理頁面order.jsp。根據使用者id,在訂單管理頁面中顯示使用者對應的訂單。點選訂單詳情,跳轉到order_detail.jsp中,根據訂單號顯示訂單所有項。
25.3程式碼實現
25.3.1dao層
OrderDAO
/**
* 根據使用者id,查詢該使用者的所有訂單Order
*
* @param id 使用者id
* @return 返回訂單
*/
public List<Order> queryOrderByMemberId(int id);
OrderDAOImpl
@Override
public List<Order> queryOrderByMemberId(int id) {
String sql = "SELECT `id`,`create_time` AS createTime,`price`,`status`,`member_id` AS memberId " +
"FROM `order` " +
"WHERE `member_id`=?";
return queryMulti(sql, Order.class, id);
}
test包-OrderDAOImplTest
@Test
public void queryOrderByMemberId() {
List<Order> orders = orderDAO.queryOrderByMemberId(2);
for (Order order : orders) {
System.out.println(order);
}
}
OrderItemDAO
/**
* 根據訂單id-orderId查詢對應的表單項orderItem
*
* @param orderId 訂單id
* @return 返回表單項orderItem
*/
public List<OrderItem> queryOrderItemByOrderId(String orderId);
OrderItemDAOImpl
@Override
public List<OrderItem> queryOrderItemByOrderId(String orderId) {
String sql =
"SELECT `id`,`name`,`price`,`count`,`total_price` AS totalPrice,`order_id` AS orderId " +
"FROM `order_item` " +
"WHERE `order_id`=?";
return queryMulti(sql, OrderItem.class, orderId);
}
OrderItemDAOImplTest
@Test
public void queryOrderItemByOrderId() {
List<OrderItem> orderItems = orderItemDAO.queryOrderItemByOrderId("16722358644142");
for (OrderItem orderItem : orderItems) {
System.out.println(orderItem);
}
}
25.3.2service層
OrderService
/**
* 根據使用者id返回訂單order
* @param id 使用者id
* @return 訂單
*/
public List<Order> queryOrderByMemberId(int id);
/**
* 根據訂單id返回對應的訂單項orderItem
* @param orderId 訂單id
* @return 返回訂單項
*/
public List<OrderItem> queryOrderItemByOrderId(String orderId);
OrderServiceImpl
@Override
public List<Order> queryOrderByMemberId(int id) {
return orderDAO.queryOrderByMemberId(id);
}
@Override
public List<OrderItem> queryOrderItemByOrderId(String orderId) {
return orderItemDAO.queryOrderItemByOrderId(orderId);
}
test包-OrderServiceImpl
@Test
public void queryOrderByMemberId() {
List<Order> orders = orderService.queryOrderByMemberId(3);
for (Order order : orders) {
System.out.println(order);
}
}
@Test
public void queryOrderItemByOrderId() {
List<OrderItem> orderItems = orderService.queryOrderItemByOrderId("16722370522643");
for (OrderItem orderItem : orderItems) {
System.out.println(orderItem);
}
}
25.3.3web層
OrderServlet,增加兩個方法
/**
* 顯示訂單order
*
* @param req
* @param resp
* @throws ServletException
* @throws IOException
*/
protected void showOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//獲取當前使用者id
Member member = (Member) req.getSession().getAttribute("member");
if (member == null) {
//重定向到登入頁面
resp.sendRedirect(req.getContextPath() + "/views/member/login.jsp");
return;
}
int id = DataUtils.parseInt(member.getId().toString(), 0);
//根據使用者id查詢對應的訂單
List<Order> orders = orderService.queryOrderByMemberId(id);
//將訂單資料返回顯示
req.setAttribute("orders", orders);
//請求轉發到order.jsp顯示
req.getRequestDispatcher("/views/order/order.jsp").forward(req, resp);
}
/**
* 顯示訂單項orderItem
*
* @param req
* @param resp
* @throws ServletException
* @throws IOException
*/
protected void showOrderItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//獲取當前orderid
String orderId = req.getParameter("orderId");
//根據orderId返回對應的訂單項orderItem
List<OrderItem> orderItems = orderService.queryOrderItemByOrderId(orderId);
//計算該order中所有orderItem的總商品數量和總價格
Integer totalCount = 0;
BigDecimal totalPrice = BigDecimal.valueOf(0);
for (OrderItem orderItem : orderItems) {
totalCount += orderItem.getCount();
totalPrice = totalPrice.add(orderItem.getTotalPrice());
}
System.out.println("totalCount=>" + totalCount);
System.out.println("totalPrice=>" + totalPrice);
//將訂單項資料返回顯示
//訂單id
req.setAttribute("orderId", orderId);
//訂單商品總數
req.setAttribute("totalCount", totalCount);
//訂單商品總額
req.setAttribute("totalPrice", totalPrice);
//訂單項
req.setAttribute("orderItems", orderItems);
//請求轉發到order_detail.jsp
req.getRequestDispatcher("/views/order/order_detail.jsp").forward(req, resp);
}
25.3.4前端頁面修改
略
25.4完成測試
使用者只有在登入情況下但能進入訂單管理
點選詳情的眼睛圖案,即可檢視該訂單詳情