文章目录
- 二十、书城项目第七阶段——订单模块
- 001-订单模块分析
- 002-创建订单模块的数据库表
- 003-创建订单模块的数据模型
- 004-编写订单模块的Dao程序和测试
- 005-编写订单模块的Service程序和测试
- 006-编写订单模块的Web层和页面联调
- 007-修改bug
- 008-其他功能实现
二十、书城项目第七阶段——订单模块
001-订单模块分析
这里只实现:点击”去结账“,生成订单功能。其他功能自己实现。
002-创建订单模块的数据库表
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`)
);
003-创建订单模块的数据模型
package com.atguigu.bean;
import java.math.BigDecimal;
import java.sql.Date;
public class Order {
private String orderId;
private Date createTime;
private BigDecimal price;
// 0表示未发货,1表示已发货,2表示已签收
private Integer status = 0;
private Integer userId;
public Order() {
}
public Order(String orderId, Date createTime, BigDecimal price, Integer status, Integer userId) {
this.orderId = orderId;
this.createTime = createTime;
this.price = price;
this.status = status;
this.userId = userId;
}
public String getOrderId() {
return orderId;
}
public void setOrderId(String orderId) {
this.orderId = orderId;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public BigDecimal getPrice() {
return price;
}
public void setPrice(BigDecimal price) {
this.price = price;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
@Override
public String toString() {
return "Order{" +
"orderId='" + orderId + '\'' +
", createTime=" + createTime +
", price=" + price +
", status=" + status +
", userId=" + userId +
'}';
}
}
package com.atguigu.bean;
import java.math.BigDecimal;
public class OrderItem {
private Integer id;
private String name;
private Integer count;
private BigDecimal price;
private BigDecimal totalPrice;
private String orderId;
public OrderItem() {
}
public OrderItem(Integer id, String name, Integer count, BigDecimal price, BigDecimal totalPrice, String orderId) {
this.id = id;
this.name = name;
this.count = count;
this.price = price;
this.totalPrice = totalPrice;
this.orderId = orderId;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getCount() {
return count;
}
public void setCount(Integer count) {
this.count = count;
}
public BigDecimal getPrice() {
return price;
}
public void setPrice(BigDecimal price) {
this.price = price;
}
public BigDecimal getTotalPrice() {
return totalPrice;
}
public void setTotalPrice(BigDecimal totalPrice) {
this.totalPrice = totalPrice;
}
public String getOrderId() {
return orderId;
}
public void setOrderId(String orderId) {
this.orderId = orderId;
}
@Override
public String toString() {
return "OrderItem{" +
"id=" + id +
", name='" + name + '\'' +
", count=" + count +
", price=" + price +
", totalPrice=" + totalPrice +
", orderId='" + orderId + '\'' +
'}';
}
}
004-编写订单模块的Dao程序和测试
OrderDao 接口:
package com.atguigu.dao;
import com.atguigu.bean.Order;
public interface OrderDao {
//保存订单
public int saveOrder(Order order);
}
OrderDao 实现:
package com.atguigu.dao.dao_impl;
import com.atguigu.bean.Order;
import com.atguigu.dao.BaseDao;
import com.atguigu.dao.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 接口:
package com.atguigu.dao;
import com.atguigu.bean.OrderItem;
public interface OrderItemDao {
//保存订单项
public int saveOrderItem(OrderItem orderItem);
}
OrderItemDao 实现:
package com.atguigu.dao.dao_impl;
import com.atguigu.bean.OrderItem;
import com.atguigu.dao.BaseDao;
import com.atguigu.dao.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());
}
}
OrderDao测试:
package com.atguigu.test;
import com.atguigu.bean.Order;
import com.atguigu.dao.OrderDao;
import com.atguigu.dao.dao_impl.OrderDaoImpl;
import org.junit.jupiter.api.Test;
import java.math.BigDecimal;
import java.util.Date;
class OrderDaoImplTest {
@Test
//保存订单
void saveOrder() {
OrderDao orderDao = new OrderDaoImpl();
orderDao.saveOrder(new Order("1234567890",new Date(),new BigDecimal(100),0, 1));
}
}
OrderItemDao测试:
package com.atguigu.test;
import com.atguigu.bean.OrderItem;
import com.atguigu.dao.OrderItemDao;
import com.atguigu.dao.dao_impl.OrderItemDaoImpl;
import org.junit.jupiter.api.Test;
import java.math.BigDecimal;
class OrderItemDaoImplTest {
@Test
//保存订单项
void saveOrderItem() {
OrderItemDao orderItemDao = new OrderItemDaoImpl();
orderItemDao.saveOrderItem(new OrderItem(null,"java 从入门到精通", 1,new BigDecimal(100),new BigDecimal(100),"1234567890"));
orderItemDao.saveOrderItem(new OrderItem(null,"javaScript 从入门到精通", 2,new BigDecimal(100),new BigDecimal(200),"1234567890"));
orderItemDao.saveOrderItem(new OrderItem(null,"Netty 入门", 1,new BigDecimal(100),new BigDecimal(100),"1234567890"));
}
}
005-编写订单模块的Service程序和测试
OrderService 接口:
package com.atguigu.service;
import com.atguigu.bean.Cart;
public interface OrderService {
//生成订单
public String createOrder(Cart cart, Integer userId);
}
OrderService 实现类:
package com.atguigu.service.service_impl;
import com.atguigu.bean.Cart;
import com.atguigu.bean.CartItem;
import com.atguigu.bean.Order;
import com.atguigu.bean.OrderItem;
import com.atguigu.dao.OrderDao;
import com.atguigu.dao.OrderItemDao;
import com.atguigu.dao.dao_impl.OrderDaoImpl;
import com.atguigu.dao.dao_impl.OrderItemDaoImpl;
import com.atguigu.service.OrderService;
import java.util.Date;
import java.util.Map;
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;
}
}
OrderService测试:
package com.atguigu.test;
import com.atguigu.bean.Cart;
import com.atguigu.bean.CartItem;
import com.atguigu.service.OrderService;
import com.atguigu.service.service_impl.OrderServiceImpl;
import org.junit.jupiter.api.Test;
import java.math.BigDecimal;
class OrderServiceImplTest {
@Test
//生成订单
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));
}
}
006-编写订单模块的Web层和页面联调
点击”去结账“,生成订单。先跳转到OrderServlet处理数据,再跳转到check.jsp显示页面。
package com.atguigu.web;
import com.atguigu.bean.Cart;
import com.atguigu.bean.User;
import com.atguigu.service.OrderService;
import com.atguigu.service.service_impl.OrderServiceImpl;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
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.setAttribute("orderId", orderId);
// 请求转发到/pages/cart/checkout.jsp
// req.getRequestDispatcher("/pages/cart/checkout.jsp").forward(req, resp);
req.getSession().setAttribute("orderId",orderId);
//使用请求转发会出现表单重复提交bug,改用请求重定向。
resp.sendRedirect(req.getContextPath()+"/pages/cart/checkout.jsp");
}
}
修改cart.jsp:
修改ckeckout.jsp:
007-修改bug
商品被买了之后,主页面显示的商品库存应该减少。
修改OrderServiceImpl:
008-其他功能实现