0%

JavaWeb书城项目(九)———订单模块

整个项目源代码可以到我的github上下载。上一部分我们已经完成了购物车模块,这一部分我们完成订单模块,主要包括生成订单,查询所有订单,发货,查看订单详情,查看我的订单,签收订单。

订单模块的分析

订单模块的分析


订单模块的实现

创建订单模块的数据库表

为订单创建一个t_order表,为订单项创建一个t_order_item表。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
use book;

create table t_order(
`order_id` varchar(50) primary key,
`create_time` datetime,
`price` decimal(11,2),
`status` int,
`user_id` int,
foreign key(`user_id`) references t_user(`id`)
);

create table t_order_item(
`id` int primary key auto_increment,
`name` varchar(100),
`count` int,
`price` decimal(11,2),
`total_price` decimal(11,2),
`order_id` varchar(50),
foreign key(`order_id`) references t_order(`order_id`)
);

创建订单模块的数据模型

创建一个Order类和一个OrderItem类,Order类属性如下:

1
2
3
4
5
6
private String orderId;
private Date createTime;
private BigDecimal price;
// 0未发货,1已发货,2已签收
private Integer status = 0;
private Integer userId;

OrderItem类属性如下:

1
2
3
4
5
6
private Integer id;
private String name;
private Integer count;
private BigDecimal price;
private BigDecimal totalPrice;
private String orderId;

并为其生成构造方法,Get方法,Set方法,以及toString方法。

编写订单模块的Dao程序和测试

OrderDao接口

1
2
3
4
5
6
7
8
9
10
public interface OrderDao {
public int saveOrder(Order order);

public List<Order> queryOrders();

public List<Order> queryOrdersByUserId(Integer id);

public int updateOrder(Order order);
}

OrderDaoImpl实现类

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
public class OrderDaoImpl extends BaseDao implements OrderDao {

/**
* 保存订单
* @param order
* @return
*/
@Override
public int saveOrder(Order order) {
String sql = "insert into t_order(`order_id`,`create_time`,`price`,`status`,`user_id`) values(?,?,?,?,?)";

return update(sql, order.getOrderId(), order.getCreateTime(), order.getPrice(), order.getStatus(), order.getUserId());
}

/**
* 查询全部订单
* @return
*/
@Override
public List<Order> queryOrders() {
String sql = "select `order_id` orderId,`create_time` createTime,`price`,`status`,`user_id` userId from t_order";
return queryForList(Order.class, sql);
}

/**
* 根据UserId查询订单
* @param id
* @return
*/
@Override
public List<Order> queryOrdersByUserId(Integer id) {
String sql = "select `order_id` orderId,`create_time` createTime,`price`,`status`,`user_id` userId from t_order where user_id=?";
return queryForList(Order.class, sql, id);
}

/**
* 修改订单
* @param order
* @return
*/
@Override
public int updateOrder(Order order) {
String sql = "update t_order set `create_time`=?,`price`=?,`status`=? where order_id=?";
return update(sql, order.getCreateTime(), order.getPrice(), order.getStatus(), order.getOrderId());
}
}

测试

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
public class OrderDaoTest {

OrderDao orderDao = new OrderDaoImpl();
@Test
public void saveOrder() {
orderDao.saveOrder(new Order("12345678",new Date(),new BigDecimal(100),0,2));
orderDao.saveOrder(new Order("123", new Date(), new BigDecimal(1005), 0, 5));
orderDao.saveOrder(new Order("456", new Date(), new BigDecimal(505), 1, 6));
orderDao.saveOrder(new Order("789", new Date(), new BigDecimal(605), 2, 6));
}

@Test
public void queryOrders() {
System.out.println(orderDao.queryOrders());
}

@Test
public void queryOrdersByUserId() {
System.out.println(orderDao.queryOrdersByUserId(6));
}

@Test
public void updateOrder() {
orderDao.updateOrder(new Order("456", new Date(), new BigDecimal(611), 2, 6));
}
}

OrderDaoItem接口

1
2
3
4
5
public interface OrderItemDao {
public int saveOrderItem(OrderItem orderItem);

public List<OrderItem> queryOrderItemsById(String id);
}

OrderDaoItemImpl实现类

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
public class OrderItemDaoImpl extends BaseDao implements OrderItemDao {

/**
* 保存订单项
* @param orderItem
* @return
*/
@Override
public int saveOrderItem(OrderItem orderItem) {
String sql = "insert into t_order_item(`name`,`count`,`price`,`total_price`,`order_id`) values(?,?,?,?,?)";

return update(sql, orderItem.getName(), orderItem.getCount(), orderItem.getPrice(), orderItem.getTotalPrice(), orderItem.getOrderId());
}

/**
* 根据order_id查询订单项
* @param id
* @return
*/
@Override
public List<OrderItem> queryOrderItemsById(String id) {
String sql = "select `id`,`name`,`count`,`price`,`total_price` totalPrice,`order_id` orderId from t_order_item where order_id=?";

return queryForList(OrderItem.class, sql, id);
}
}

测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class OrderItemDaoTest {

OrderItemDao orderItemDao = new OrderItemDaoImpl();
@Test
public void saveOrderItem() {
orderItemDao.saveOrderItem(new OrderItem(null,"java 从入门到精通", 1,new BigDecimal(100),new
BigDecimal(100),"456"));
orderItemDao.saveOrderItem(new OrderItem(null,"javaScript 从入门到精通", 2,new
BigDecimal(100),new BigDecimal(200),"12345678"));
orderItemDao.saveOrderItem(new OrderItem(null,"Netty 入门", 1,new BigDecimal(100),new
BigDecimal(100),"456"));
}

@Test
public void queryOrderItemsById() {
System.out.println(orderItemDao.queryOrderItemsById("12345678"));
}
}

编写订单模块的Service和测试

OrderService接口

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
public interface OrderService {

/**
* 创建订单
* @param cart
* @param userId
* @return
*/
public String createOrder(Cart cart, Integer userId);

/**
* 展示所有订单
* @return
*/
public List<Order> showAllOrders();

/**
* 发货
* @param order
*/
public void sendOrder(Order order);

/**
* 查询订单详情
* @param id
* @return
*/
public List<OrderItem> showOrderDetail(String id);

/**
* 展示我的订单
* @param id
* @return
*/
public List<Order> showMyOrders(Integer id);

/**
* 签收
* @param order
*/
public void receiverOrder(Order order);
}

OrderServiceImpl实现类

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
59
public class OrderServiceImpl implements OrderService {
private OrderDao orderDao = new OrderDaoImpl();
private OrderItemDao orderItemDao = new OrderItemDaoImpl();
private BookDao bookDao = new BookDaoImpl();

@Override
public String createOrder(Cart cart, Integer userId) {
// 订单号==唯一性
String orderId = System.currentTimeMillis()+""+userId;
// 创建一个订单对象
Order order = new Order(orderId, new Date(), cart.getTotalPrice(), 0, userId);
// 保存订单
orderDao.saveOrder(order);
// 遍历购物车中每一个商品项转换成为订单项保存到数据库
for (Map.Entry<Integer, CartItem>entry : cart.getItems().entrySet()){
// 获取每一个购物车中的商品项
CartItem cartItem = entry.getValue();
// 转换为每一个订单项
OrderItem orderItem = new OrderItem(null, cartItem.getName(), cartItem.getCount(), cartItem.getPrice(), cartItem.getTotalprice(), orderId);
// 保存订单项到数据库
orderItemDao.saveOrderItem(orderItem);
// 更新库存和销量
Book book = bookDao.queryBookById(cartItem.getId());
book.setSales( book.getSales() + cartItem.getCount() );
book.setStock( book.getStock() - cartItem.getCount() );
bookDao.updateBook(book);
}
// 清空购物车
cart.clear();
return orderId;
}

@Override
public List<Order> showAllOrders() {
return orderDao.queryOrders();
}

@Override
public void sendOrder( Order order ) {
order.setStatus(1);
orderDao.updateOrder(order);
}

@Override
public List<OrderItem> showOrderDetail(String id) {
return orderItemDao.queryOrderItemsById(id);
}

@Override
public List<Order> showMyOrders(Integer id) {
return orderDao.queryOrdersByUserId(id);
}

@Override
public void receiverOrder(Order order) {
order.setStatus(2);
orderDao.updateOrder(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
public class OrderServiceImplTest {

private OrderService orderService = new OrderServiceImpl();

@Test
public void createOrder() {
Cart cart = new Cart();
cart.addItem(new CartItem(1, "java从入门到精通", 1, new BigDecimal(1000),new BigDecimal(1000)));
cart.addItem(new CartItem(1, "java从入门到精通", 1, new BigDecimal(1000),new BigDecimal(1000)));
cart.addItem(new CartItem(2, "数据结构与算法", 1, new BigDecimal(100),new BigDecimal(100)));
System.out.println("订单号是:" + orderService.createOrder(cart, 2));
}

@Test
public void showAllOrders() {
System.out.println(orderService.showAllOrders());
}

@Test
public void sendOrder() {
orderService.sendOrder(new Order("16366346078552",new Date(), new BigDecimal(2100),0,2));
}

@Test
public void showOrderDetail() {
System.out.println(orderService.showOrderDetail("16366346078552"));
}

@Test
public void showMyOrders() {
System.out.println(orderService.showMyOrders(2));
}

@Test
public void receiverOrder() {
orderService.receiverOrder(new Order("16366346078552",new Date(), new BigDecimal(2100),1,2));
}
}

编写订单模块的Web层和页面联调

生成订单

  1. OrderServlet添加createOrder方法
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
/**
* 生成订单
* @param req
* @param resp
* @throws ServletException
* @throws IOException
*/
protected void createOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 先获取购物车对象
Cart cart = (Cart) req.getSession().getAttribute("cart");
// 获取Userid
User loginUser = (User) req.getSession().getAttribute("user");

if (loginUser == null) {
req.getRequestDispatcher("/pages/user/login.jsp").forward(req, resp);
return ;
}

Integer userId = loginUser.getId();
String orderId = orderService.createOrder(cart, userId);

req.getSession().setAttribute("orderId",orderId);

resp.sendRedirect(req.getContextPath()+"/pages/cart/checkout.jsp");
}
  1. 修改cart.jsp页面,结账的请求地址
    修改cart.jsp

  2. 修改checkout.jsp页面
    修改checkout.jsp

    展示全部订单

  3. OrderServlet添加showAllOrders方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**
* 展示全部订单
* @param req
* @param resp
* @throws ServletException
* @throws IOException
*/
protected void showAllOrders(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 1.通过orderService查询所有订单
List<Order> orders = orderService.showAllOrders();
// 2.把全部订单保存到Request域
req.setAttribute("orders", orders);
// 3.请求转发到 /pages/manager/order_manager.jsp
req.getRequestDispatcher("/pages/manager/order_manager.jsp");
}
  1. 修改manager_menu.jsp页面,订单管理请求地址
    修改manager_menu.jsp

  2. 修改order_manager.jsp页面
    修改order_manager.jsp

    展示我的订单

  3. OrderServlet添加showMyOrders方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**
* 展示我的订单
* @param req
* @param resp
* @throws ServletException
* @throws IOException
*/
protected void showMyOrders(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 获取Userid
User loginUser = (User) req.getSession().getAttribute("user");

if (loginUser == null) {
req.getRequestDispatcher("/pages/user/login.jsp").forward(req, resp);
return ;
}

Integer userId = loginUser.getId();
List<Order> myOrders = orderService.showMyOrders(userId);

req.getSession().setAttribute("myOrders", myOrders);

resp.sendRedirect(req.getContextPath()+"/pages/order/order.jsp");
}
  1. 修改login_success_menu.jsp页面,我的订单请求地址
    修改login_menu.jsp

  2. 修改order.jsp页面
    修改order.jsp

显示订单详情

  1. OrderServlet增加showOrderDetail方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/**
* 显示订单详情
* @param req
* @param resp
* @throws ServletException
* @throws IOException
*/
protected void showOrderDetail(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 1. 获取Orderid
String orderId = req.getParameter("orderId");
if (orderId != null && orderId != "") {
// 2. 查询订单项
List<OrderItem> orderItems = orderService.showOrderDetail(orderId);
// 3. 把订单项保存到Request域
req.setAttribute("orderItems", orderItems);
// 4. 请求转发到 /pages/order/order_detail.jsp
req.getRequestDispatcher("/pages/order/order_detail.jsp").forward(req, resp);
}
}
  1. order目录下新建order_detail.jsp页面
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
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>我的订单</title>

<%-- 静态包含 base标签,css样式,jQuery文件 --%>
<%@ include file="/pages/common/head.jsp"%>

<style type="text/css">
h1 {
text-align: center;
margin-top: 200px;
}
</style>
</head>
<body>

<div id="header">
<img class="logo_img" alt="" src="static/img/logo.gif" >
<span class="wel_word">我的订单</span>

<%-- 静态包含登录成功之后的菜单 --%>
<%@ include file="/pages/common/login_success_menu.jsp"%>
</div>

<div id="main">

<table>
<tr>
<td>商品名称</td>
<td>数量</td>
<td>单价</td>
<td>金额</td>
</tr>
<c:forEach items="${requestScope.orderItems}" var="orderItem">
<tr>
<td>${orderItem.name}</td>
<td>${orderItem.count}</td>
<td>${orderItem.price}</td>
<td>${orderItem.totalPrice}</td>
</tr>
</c:forEach>
</table>


</div>

<%-- include包含脚部信息 --%>
<%@ include file="/pages/common/footer.jsp"%>

</body>
</html>
  1. 修改order.jsp页面,查看详情请求地址
    修改order.jsp

  2. 修改order_manager.jsp页面,查看详情请求地址
    修改order_manager.jsp

    发货

  3. OrderServlet增加sendOrder方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**
* 发货
* @param req
* @param resp
* @throws ServletException
* @throws IOException
*/
protected void sendOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 1. 获取Order
Order order = WebUtils.copyParamToBean(req.getParameterMap(), new Order());
// 2. 调用orderService的sendOrder方法发货
orderService.sendOrder(order);
// 3. 重定向回 /pages/manager/order_manager.jsp
resp.sendRedirect(req.getContextPath() + "/pages/manager/order_manager.jsp");
}
  1. 修改WebUtilscopyParamToBean方法,因为BeanUtils无法直接将String转为Date,所以要先注册
    修改WebUtils
  2. 修改order_manager.jsp页面, 点击发货请求地址
    修改order_manager.jsp

    签收

  3. OrderServlet增加receiverOrder方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**
* 签收
* @param req
* @param resp
* @throws ServletException
* @throws IOException
*/
protected void receiverOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 1. 获取Order
Order order = WebUtils.copyParamToBean(req.getParameterMap(), new Order());
// 2. 调用orderService的receiverOrder方法签收
orderService.receiverOrder(order);
// 3. 重定向回 /orderServlet?action=showMyOrders
resp.sendRedirect(req.getContextPath() + "/orderServlet?action=showMyOrders");
}
  1. 修改order.jsp页面,点击签收请求地址
    修改order.jsp
-------------本文结束感谢您的阅读-------------