本文实例为大家分享了java网上图书商城订单模块的具体代码,供大家参考,具体内容如下
1.我的订单---查
按用户查询订单
分页查询 PageBean<Order>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
< div class = "divMain" >
< div class = "divTitle" >
< span style = "margin-left: 150px;margin-right: 280px;" >商品信息</ span >
< span style = "margin-left: 40px;margin-right: 38px;" >金额</ span >
< span style = "margin-left: 50px;margin-right: 40px;" >订单状态</ span >
< span style = "margin-left: 50px;margin-right: 50px;" >操作</ span >
</ div >
< table align = "center" border = "0" width = "100%" cellpadding = "0" cellspacing = "0" >
< c:forEach items = "${pb.beanList }" var = "order" >
< tr class = "tt" >
< td width = "320px" >订单号:< a href="<c:url value = '/OrderServlet?method=load&oid=${order.oid }' />">${order.oid }</ a ></ td >
< td width = "200px" >下单时间:${order.ordertime }</ td >
< td > </ td >
< td > </ td >
< td > </ td >
< td > </ td >
</ tr >
< tr style = "padding-top: 10px; padding-bottom: 10px;" >
< td colspan = "2" >
< c:forEach items = "${order.orderItemList }" var = "orderItem" >
< a class = "link2" href="<c:url value = '/BookServlet?method=load&bid=${orderItem.book.bid }' />">
< img border = "0" width = "70" src="<c:url value = '/${orderItem.book.image_b }' />"/>
</ a >
</ c:forEach >
</ td >
< td width = "115px" >
< span class = "price_t" >¥${order.total }</ span >
</ td >
< td width = "142px" >
< c:choose >
< c:when test = "${order.status eq 1 }" >(等待付款)</ c:when >
< c:when test = "${order.status eq 2 }" >(准备发货)</ c:when >
< c:when test = "${order.status eq 3 }" >(等待确认)</ c:when >
< c:when test = "${order.status eq 4 }" >(交易成功)</ c:when >
< c:when test = "${order.status eq 5 }" >(已取消)</ c:when >
</ c:choose >
</ td >
< td >
< a href="<c:url value = '/OrderServlet?method=load&oid=${order.oid }' />">查看</ a >< br />
< c:if test = "${order.status eq 1 }" >
< a href="<c:url value = '/OrderServlet?method=paymentPre&oid=${order.oid }' />">支付</ a >< br />
< a href="<c:url value = '/OrderServlet?method=load&oid=${order.oid }&btn=cancel' />">取消</ a >< br />
</ c:if >
< c:if test = "${order.status eq 3 }" >
< a href="<c:url value = '/OrderServlet?method=load&oid=${order.oid }&btn=confirm' />">确认收货</ a >< br />
</ c:if >
</ td >
</ tr >
</ c:forEach >
</ table >
</ div >
|
2.订单模块之生成订单
OrderServlet
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
public String createOrder(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
/*
* 1. 获取所有购物车条目的id,查询之
*/
String cartItemIds = req.getParameter("cartItemIds");
List<CartItem> cartItemList = cartItemService.loadCartItems(cartItemIds);
if(cartItemList.size() == 0) {
req.setAttribute("code", "error");
req.setAttribute("msg", "您没有选择要购买的图书,不能下单!");
return "f:/jsps/msg.jsp";
}
/*
* 2. 创建Order
*/
Order order = new Order();
order.setOid(CommonUtils.uuid());//设置主键
order.setOrdertime(String.format("%tF %<tT", new Date()));//下单时间
order.setStatus(1);//设置状态,1表示未付款
order.setAddress(req.getParameter("address"));//设置收货地址
User owner = (User)req.getSession().getAttribute("sessionUser");
order.setOwner(owner);//设置订单所有者
BigDecimal total = new BigDecimal("0");
for(CartItem cartItem : cartItemList) {
total = total.add(new BigDecimal(cartItem.getSubtotal() + ""));
}
order.setTotal(total.doubleValue());//设置总计
/*
* 3. 创建List<OrderItem>
* 一个CartItem对应一个OrderItem
*/
List<OrderItem> orderItemList = new ArrayList<OrderItem>();
for(CartItem cartItem : cartItemList) {
OrderItem orderItem = new OrderItem();
orderItem.setOrderItemId(CommonUtils.uuid());//设置主键
orderItem.setQuantity(cartItem.getQuantity());
orderItem.setSubtotal(cartItem.getSubtotal());
orderItem.setBook(cartItem.getBook());
orderItem.setOrder(order);
orderItemList.add(orderItem);
}
order.setOrderItemList(orderItemList);
/*
* 4. 调用service完成添加
*/
orderService.createOrder(order);
// 删除购物车条目
cartItemService.batchDelete(cartItemIds);
/*
* 5. 保存订单,转发到ordersucc.jsp
*/
req.setAttribute( "order" , order);
return "f:/jsps/order/ordersucc.jsp" ;
}
|
OrderDao
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
public void add(Order order) throws SQLException {
/*
* 1. 插入订单
*/
String sql = "insert into t_order values(?,?,?,?,?,?)";
Object[] params = {order.getOid(), order.getOrdertime(),
order.getTotal(),order.getStatus(),order.getAddress(),
order.getOwner().getUid()};
qr.update(sql, params);
/*
* 2. 循环遍历订单的所有条目,让每个条目生成一个Object[]
* 多个条目就对应Object[][]
* 执行批处理,完成插入订单条目
*/
sql = "insert into t_orderitem values(?,?,?,?,?,?,?,?)" ;
int len = order.getOrderItemList().size();
Object[][] objs = new Object[len][];
for ( int i = 0 ; i < len; i++){
OrderItem item = order.getOrderItemList().get(i);
objs[i] = new Object[]{item.getOrderItemId(),item.getQuantity(),
item.getSubtotal(),item.getBook().getBid(),
item.getBook().getBname(),item.getBook().getCurrPrice(),
item.getBook().getImage_b(),order.getOid()};
}
qr.batch(sql, objs);
}
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。