1、订单的提交分析和实体的创建
定义订单实体,order和orderitem
public class Order {
/*`oid` varchar(32) NOT NULL,
`ordertime` datetime DEFAULT NULL,
`total` double DEFAULT NULL,
`state` int(11) DEFAULT NULL,
`address` varchar(30) DEFAULT NULL,
`name` varchar(20) DEFAULT NULL,
`telephone` varchar(20) DEFAULT NULL,
`uid` varchar(32) DEFAULT NULL*/
private String oid;//该订单的订单号
private Date ordertime;//下单时间
private double total;//该订单的总金额
private int state;//订单支付状态 1代表已付款 0代表未付款
private String address;//收货地址
private String name;//收货人
private String telephone;//收货人电话
private User user;//该订单属于哪个用户
//该订单中有多少订单项
List<OrderItem> orderItems = new ArrayList<OrderItem>();
public List<OrderItem> getOrderItems() {
return orderItems;
}
public void setOrderItems(List<OrderItem> orderItems) {
this.orderItems = orderItems;
}
public class OrderItem {
/*`itemid` varchar(32) NOT NULL,
`count` int(11) DEFAULT NULL,
`subtotal` double DEFAULT NULL,
`pid` varchar(32) DEFAULT NULL,
`oid` varchar(32) DEFAULT NULL*/
private String itemid;//订单项的id
private int count;//订单项内商品的购买数量
private double subtotal;//订单项小计
private Product product;//订单项内部的商品
private Order order;//该订单项属于哪个订单
cart里的用户和order里的用户不一定一样,因为买东西和收货人不一定是同一个。
2、订单提交业务分析:
当你提交订单,在web层封装一个order对象,将order对象传递给service层,service分别调用dao的两个方法,调用方法的时候把order对象传进去,order对象封装了订单的所有信息,在dao层分别执行两个sql语句,从order中取数据执行,最终放到数据库。
3、订单提交的代码实现
//提交订单
public void submitOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
//判断用户是否已经登录 未登录下面代码不执行
User user = (User) session.getAttribute("user");
if(user==null){
//没有登录
response.sendRedirect(request.getContextPath()+"/login.jsp");
return;
}
//目的:封装好一个Order对象 传递给service层
Order order = new Order();
//1、private String oid;//该订单的订单号
String oid = CommonsUtils.getUUID();
order.setOid(oid);
//2、private Date ordertime;//下单时间
order.setOrdertime(new Date());
//3、private double total;//该订单的总金额
//获得session中的购物车
Cart cart = (Cart) session.getAttribute("cart");
double total = cart.getTotal();
order.setTotal(total);
//4、private int state;//订单支付状态 1代表已付款 0代表未付款
order.setState(0);
//5、private String address;//收货地址
order.setAddress(null);
//6、private String name;//收货人
order.setName(null);
//7、private String telephone;//收货人电话
order.setTelephone(null);
//8、private User user;//该订单属于哪个用户
order.setUser(user);
//9、该订单中有多少订单项List<OrderItem> orderItems = new ArrayList<OrderItem>();
//获得购物车中的购物项的集合map
Map<String, CartItem> cartItems = cart.getCartItems();
for(Map.Entry<String, CartItem> entry : cartItems.entrySet()){
//取出每一个购物项
CartItem cartItem = entry.getValue();
//创建新的订单项
OrderItem orderItem = new OrderItem();
//1)private String itemid;//订单项的id
orderItem.setItemid(CommonsUtils.getUUID());
//2)private int count;//订单项内商品的购买数量
orderItem.setCount(cartItem.getBuyNum());
//3)private double subtotal;//订单项小计
orderItem.setSubtotal(cartItem.getSubtotal());
//4)private Product product;//订单项内部的商品
orderItem.setProduct(cartItem.getProduct());
//5)private Order order;//该订单项属于哪个订单
orderItem.setOrder(order);
//将该订单项添加到订单的订单项集合中
order.getOrderItems().add(orderItem);
}
//order对象封装完毕
//传递数据到service层
ProductService service = new ProductService();
service.submitOrder(order);
}
修改Product.dao
//向orders表插入数据
public void addOrders(Order order) throws SQLException {
QueryRunner runner = new QueryRunner();
String sql = "insert into orders values(?,?,?,?,?,?,?,?)";
Connection conn = DataSourceUtils.getConnection();
runner.update(conn,sql, order.getOid(),order.getOrdertime(),order.getTotal(),order.getState(),
order.getAddress(),order.getName(),order.getTelephone(),order.getUser().getUid());
}
//向orderitem表插入数据
public void addOrderItem(Order order) throws SQLException {
QueryRunner runner = new QueryRunner();
String sql = "insert into orderitem values(?,?,?,?,?)";
Connection conn = DataSourceUtils.getConnection();
List<OrderItem> orderItems = order.getOrderItems();
for(OrderItem item : orderItems){
runner.update(conn,sql,item.getItemid(),item.getCount(),item.getSubtotal(),item.getProduct().getPid(),item.getOrder().getOid());
}
}
4、更新收货人信息
session.setAttribute("order", order);
//页面跳转
response.sendRedirect(request.getContextPath()+"/order_info.jsp");
修改Order_info.jsp
<c:forEach items="${order.orderItems }" var="orderItem">
<tr class="active">
<td width="60" width="40%">
<img src="${pageContext.request.contextPath }/${orderItem.product.pimage}" width="70" height="60">
</td>
<td width="30%"><a target="_blank">${orderItem.product.pname}</a></td>
<td width="20%">¥${orderItem.product.shop_price}</td>
<td width="10%">${orderItem.count}</td>
<td width="15%"><span class="subtotal">¥${orderItem.subtotal }</span></td>
</tr>
</c:forEach>
<div style="text-align: right; margin-right: 120px;">
商品金额: <strong style="color: #ff6600;">¥${order.total }元</strong>
</div>
这里默认地址为登录用户user的信息,友好界面
<!-- method的名字 通过表单提交 -->
<input type="hidden" name="method" value="confirmOrder">
<!-- 传递订单oid -->
<input type="hidden" name="oid" value="${order.oid }">
<div class="form-group">
<label for="username" class="col-sm-1 control-label">地址</label>
<div class="col-sm-5">
<input type="text" class="form-control" id="address" name="address" value="">
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-1 control-label">收货人</label>
<div class="col-sm-5">
<input type="text" class="form-control" id="inputPassword3" name="name"
placeholder="请输收货人" value="${user.name }">
</div>
</div>
<div class="form-group">
<label for="confirmpwd" class="col-sm-1 control-label">电话</label>
<div class="col-sm-5">
<input type="text" class="form-control" id="confirmpwd" name="telephone"
placeholder="请输入联系方式" value="${user.telephone }">
</div>
</div>
//确认订单---更新收获人信息+在线支付
public void confirmOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1、更新收货人信息
Map<String, String[]> properties = request.getParameterMap();
Order order = new Order();
try {
BeanUtils.populate(order, properties);
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
ProductService service = new ProductService();
service.updateOrderAdrr(order);
Product.dao
public void updateOrderAdrr(Order order) throws SQLException {
QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "update orders set address=?,name=?,telephone=? where oid=?";
runner.update(sql, order.getAddress(),order.getName(),order.getTelephone(),order.getOid());
}
5、在线支付