1 案例分析
1 訂單分析1

2 訂單分析2

3 訂單的業務操作

2 代碼實現
1 cart.jsp代碼修改
<a href="${pageContext.request.contextPath }/product?method=submitOrder">
<input type="button" width="100" value="提交訂單" name="submit" border="0" style="background: url('./images/register.gif') no-repeat scroll 0 0 rgba(0, 0, 0, 0);
height:35px;width:100px;color:white;">
</a>
2 ProductServlet代碼
// 8 提交訂單 submitOrder
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 = (Cart) session.getAttribute("cart");
double total = 0.0;
if(cart!=null){
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 該訂單中有多少訂單項
//從購物車中獲取所有的cartItem
Map<String, CartItem> cartItems = cart.getCartItems();
//遍歷所有的購物車項,加入到訂單項orderItem中
for(Map.Entry<String, CartItem> entry:cartItems.entrySet()){
//取出里面的每一個購物項
CartItem cartItem = entry.getValue();
// 創建新的訂單項
OrderItem orderItem = new OrderItem();
// 1)private String itemid; //訂單項
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);
session.setAttribute("order", order);
//頁面跳轉
response.sendRedirect(request.getContextPath()+"/order_info.jsp");
}
3 ProductService代碼
//訂單提交 將訂單的數據和訂單項的數據存儲到數據庫中
public void submitOrder(Order order) {
ProductDao dao = new ProductDao();
try {
// 1 、開啟事務
C3P0Utils.startTransaction();
//2、調用dao存儲order表數據的方法
boolean isaddOrdersSuccess = false;
int rowAddOrders = dao.addOrders(order);
if(rowAddOrders>0){
isaddOrdersSuccess = true;
}
//3、調用dao存儲orderItem表數據的方法
boolean isaddOrderItemSuccess = false;
int rowAddOrderItem = dao.addOrderItems(order);
if(rowAddOrderItem>0){
isaddOrderItemSuccess = true;
}
//只要有一項操作不成功就回滾。
if(!isaddOrdersSuccess&&isaddOrderItemSuccess){
C3P0Utils.rollback();
}
} catch (SQLException e) {
try {
C3P0Utils.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
}
e.printStackTrace();
}finally {
//無論是否成功都提交並且釋放資源
try {
C3P0Utils.commitAndRelease();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
4 ProductDao代碼
// 向orders表中插入數據
public int addOrders(Order order) throws SQLException {
QueryRunner qr = new QueryRunner();
String sql ="insert into orders values (?,?,?,?,?,?,?,?)";
Connection conn = C3P0Utils.getConnection();
int rowAddOrders = qr.update(conn,sql, order.getOid(),order.getOrdertime(),order.getTotal(),order.getState(),
order.getAddress(),order.getName(),order.getTelephone(),order.getUser().getUid());
return rowAddOrders;
}
// 向orderitem表插入數據
public int addOrderItems(Order order) throws SQLException {
QueryRunner qr = new QueryRunner();
String sql ="insert into orderItem values (?,?,?,?,?)";
Connection conn = C3P0Utils.getConnection();
List<OrderItem> orderItems = order.getOrderItems();
int rowAddOrderItem = 0;
for (OrderItem item : orderItems) {
rowAddOrderItem += qr.update(conn, sql,item.getItemid(),item.getCount(),
item.getSubtotal(),item.getProduct().getPid(),
item.getOrder().getOid());
}
return rowAddOrderItem;
}
5 order_info.jsp修改代碼
<table class="table table-bordered">
<tbody>
<tr class="warning">
<th colspan="5">訂單編號:${order.oid }</th>
</tr>
<tr class="warning">
<th>圖片</th>
<th>商品</th>
<th>價格</th>
<th>數量</th>
<th>小計</th>
</tr>
<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>
</tbody>
</table>
3 domain部分
1 orderItem
package www.test.domain;
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;//該訂單項屬於哪個訂單
public String getItemid() {
return itemid;
}
public void setItemid(String itemid) {
this.itemid = itemid;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public double getSubtotal() {
return subtotal;
}
public void setSubtotal(double subtotal) {
this.subtotal = subtotal;
}
public Product getProduct() {
return product;
}
public void setProduct(Product product) {
this.product = product;
}
public Order getOrder() {
return order;
}
public void setOrder(Order order) {
this.order = order;
}
}
2 order
package www.test.domain;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
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 String getOid() {
return oid;
}
public void setOid(String oid) {
this.oid = oid;
}
public Date getOrdertime() {
return ordertime;
}
public void setOrdertime(Date ordertime) {
this.ordertime = ordertime;
}
public double getTotal() {
return total;
}
public void setTotal(double total) {
this.total = total;
}
public int getState() {
return state;
}
public void setState(int state) {
this.state = state;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public List<OrderItem> getOrderItems() {
return orderItems;
}
public void setOrderItems(List<OrderItem> orderItems) {
this.orderItems = orderItems;
}
}