家居网购项目实现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完成测试
用户只有在登录情况下但能进入订单管理
点击详情的眼睛图案,即可查看该订单详情