書城專案第七階段:訂單
阿新 • • 發佈:2021-11-29
專案第七階段:訂單
1、訂單模組的分析:
1.1、建立訂單模組的資料庫表
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`) );
1.2、建立訂單模組的資料模型
/**
* 訂單
*/
public class Order {
private String orderId;
private Date createTime;
private BigDecimal price;
// 0 未發貨,1 已發貨,2 表示已簽收
private Integer status = 0;
private Integer userId;
/** * 訂單項 */ public class OrderItem { private Integer id; private String name; private Integer count; private BigDecimal price; private BigDecimal totalPrice; private String orderId;
1.3、編寫訂單模組的 Dao 程式和測試
OrderDao 介面
public interface OrderDao {
public int saveOrder(Order order);
}
OrderDao 實現
public class OrderDaoImpl extends BaseDao implements OrderDao { @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()); } }
OrderItemDao 介面
public interface OrderItemDao {
public int saveOrderItem(OrderItem orderItem);
}
OrderItemDao 實現
public class OrderItemDaoImpl extends BaseDao implements OrderItemDao {
@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());
}
}
測試
public class OrderDaoTest {
@Test
public void saveOrder() {
OrderDao orderDao = new OrderDaoImpl();
orderDao.saveOrder(new Order("1234567891",new Date(),new BigDecimal(100),0,1));
}
}
public class OrderItemDaoTest {
@Test
public void saveOrderItem() {
OrderItemDao orderItemDao = new OrderItemDaoImpl();
orderItemDao.saveOrderItem(new OrderItem(null,"java 從入門到精通", 1,new BigDecimal(100),new BigDecimal(100),"1234567891"));
orderItemDao.saveOrderItem(new OrderItem(null,"javaScript 從入門到精通", 2,new BigDecimal(100),new BigDecimal(200),"1234567891"));
orderItemDao.saveOrderItem(new OrderItem(null,"Netty 入門", 1,new BigDecimal(100),new BigDecimal(100),"1234567891"));
}
}
1.4、編寫訂單模組的 Service 和測試
OrderService 介面
public interface OrderService {
public String createOrder(Cart cart, Integer userId);
}
OrderService 實現類
public class OrderServiceImpl implements OrderService {
private OrderDao orderDao = new OrderDaoImpl();
private OrderItemDao orderItemDao = new OrderItemDaoImpl();
@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);
}
// 清空購物車
cart.clear();
return orderId;
}
}
測試
public class OrderServiceTest {
@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)));
OrderService orderService = new OrderServiceImpl();
System.out.println( "訂單號是:" + orderService.createOrder(cart, 1));
}
}
1.5、編寫訂單模組的 web 層和頁面聯調
修改 OrderService 程式:
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;
}
}
OrderServlet 程式:
public class OrderServlet extends BaseServlet{
private OrderService orderService = new OrderServiceImpl();
protected void createOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 先獲取 Cart 購物車物件
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();
//呼叫 orderService.createOrder(Cart,Userid);生成訂單
String orderId = orderService.createOrder(cart, userId);
req.getSession().setAttribute("orderId",orderId);
resp.sendRedirect(req.getContextPath()+"/pages/cart/checkout.jsp");
}
}
修改 pages/cart/cart.jsp 頁面,結賬的請求地址:
修改 pages/cart/checkout.jsp 頁面,輸出訂單號: