• 网上图书商城项目学习笔记-034订单管理


    一、流程分析

    二、代码

    1.view层

    (1)list.jsp

     1   <body>
     2 <p class="pLink">
     3   <a href="<c:url value='/admin/AdminOrderServlet?method=findByStatus&status=1'/>">未付款</a>  | 
     4   <a href="<c:url value='/admin/AdminOrderServlet?method=findByStatus&status=2'/>">已付款</a>  | 
     5   <a href="<c:url value='/admin/AdminOrderServlet?method=findByStatus&status=3'/>">已发货</a>  | 
     6   <a href="<c:url value='/admin/AdminOrderServlet?method=findByStatus&status=4'/>">交易成功</a>  | 
     7   <a href="<c:url value='/admin/AdminOrderServlet?method=findByStatus&status=5'/>">已取消</a>
     8 </p>
     9 <div class="divMain">
    10     <div class="title">
    11         <div style="margin-top:7px;">
    12             <span style="margin-left: 150px;margin-right: 280px;">商品信息</span>
    13             <span style="margin-left: 40px;margin-right: 100px;">金额</span>
    14             <span style="margin-left: 50px;margin-right: 53px;">订单状态</span>
    15             <span style="margin-left: 100px;">操作</span>
    16         </div>
    17     </div>
    18     <br/>
    19     <table align="center" border="0" width="100%" cellpadding="0" cellspacing="0">
    20     
    21     
    22     
    23 <c:forEach items="${pb.beanList}" var="order">    
    24 
    25         <tr class="tt">
    26             <td width="320px">订单号:<a  href="<c:url value='/admin/AdminOrderServlet?method=load&oid=${order.oid }'/>">${order.oid }</a></td>
    27             <td width="200px">下单时间:${order.orderTime }</td>
    28             <td width="178px">&nbsp;</td>
    29             <td width="205px">&nbsp;</td>
    30             <td>&nbsp;</td>
    31         </tr>
    32         <tr style="padding-top: 10px; padding-bottom: 10px;">
    33             <td colspan="2">
    34 <c:forEach items="${order.orderItems }" var="item">
    35     <img border="0" width="70" src="<c:url value='/${item.book.image_b }'/>"/>
    36 </c:forEach>
    37             </td>
    38             <td style="padding-left: 0">
    39                 <span class="price_t">&yen;${order.total }</span>
    40             </td>
    41             <td>
    42 <c:choose>
    43     <c:when test="${order.status eq 1 }">(等待付款)</c:when>
    44     <c:when test="${order.status eq 2 }">(准备发货)</c:when>
    45     <c:when test="${order.status eq 3 }">(等待确认)</c:when>
    46     <c:when test="${order.status eq 4 }">(交易成功)</c:when>
    47     <c:when test="${order.status eq 5 }">(已取消)</c:when>
    48 </c:choose>    
    49             </td>
    50             <td>
    51                 <a href="<c:url value='/admin/AdminOrderServlet?method=load&oid=${order.oid }'/>">查看</a><br/>
    52 <c:if test="${order.status eq 1 }">
    53                 <a href="<c:url value='/admin/AdminOrderServlet?method=load&oid=${order.oid }&btn=cancel'/>">取消</a><br/>                        
    54 </c:if>
    55 <c:if test="${order.status eq 2 }">
    56                 <a href="<c:url value='/admin/AdminOrderServlet?method=load&oid=${order.oid }&btn=deliver'/>">发货</a><br/>
    57 </c:if>        
    58             </td>
    59         </tr>
    60 
    61 </c:forEach>
    62 
    63 
    64 
    65 
    66 
    67     </table>
    68     <br/>
    69     <%@include file="/jsps/pager/pager.jsp" %>
    70 </div>
    71   </body>
    72 </html>

    (2)desc.jsp

     1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
     2 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
     3 <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
     4 
     5 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
     6 <html>
     7   <head>
     8     <title>订单详细</title>
     9     
    10     <meta http-equiv="pragma" content="no-cache">
    11     <meta http-equiv="cache-control" content="no-cache">
    12     <meta http-equiv="expires" content="0">
    13     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    14     <meta http-equiv="description" content="This is my page">
    15     <meta http-equiv="content-type" content="text/html;charset=utf-8">
    16     <!--
    17     <link rel="stylesheet" type="text/css" href="styles.css">
    18     -->
    19     <link rel="stylesheet" type="text/css" href="<c:url value='/adminjsps/admin/css/order/desc.css'/>">
    20   </head>
    21   
    22 <body>
    23     <div class="divOrder">
    24         <span>订单号:${oder.oid }
    25 <c:choose>
    26     <c:when test="${order.status eq 1 }">(等待付款)</c:when>
    27     <c:when test="${order.status eq 2 }">(准备发货)</c:when>
    28     <c:when test="${order.status eq 3 }">(等待确认)</c:when>
    29     <c:when test="${order.status eq 4 }">(交易成功)</c:when>
    30     <c:when test="${order.status eq 5 }">(已取消)</c:when>
    31 </c:choose>    
    32            下单时间:${order.orderTime }
    33         </span>
    34     </div>
    35     <div class="divRow">
    36         <div class="divContent">
    37             <dl>
    38                 <dt>收货人信息</dt>
    39                 <dd>${order.address }</dd>
    40             </dl>
    41         </div>
    42         <div class="divContent">
    43             <dl>
    44                 <dt>商品清单</dt>
    45                 <dd>
    46                     <table cellpadding="0" cellspacing="0">
    47                         <tr>
    48                             <th class="tt">商品名称</th>
    49                             <th class="tt" align="left">单价</th>
    50                             <th class="tt" align="left">数量</th>
    51                             <th class="tt" align="left">小计</th>
    52                         </tr>
    53 
    54 
    55 
    56 <c:forEach items="${order.orderItems }" var="item">
    57 
    58                         <tr style="padding-top: 20px; padding-bottom: 20px;">
    59                             <td class="td" width="400px">
    60                                 <div class="bookname">
    61                                   <img align="middle" width="70" src="<c:url value='/${item.book.image_b }'/>"/>
    62                                   ${item.book.bname }
    63                                 </div>
    64                             </td>
    65                             <td class="td" >
    66                                 <span>&yen;${item.book.currPrice }</span>
    67                             </td>
    68                             <td class="td">
    69                                 <span>${item.quantity }</span>
    70                             </td>
    71                             <td class="td">
    72                                 <span>&yen;${item.subtotal }</span>
    73                             </td>            
    74                         </tr>
    75 
    76 </c:forEach>
    77                             
    78                             
    79                     </table>
    80                 </dd>
    81             </dl>
    82         </div>
    83         <div class="divBtn">
    84             <span class="spanTotal">合  计:</span>
    85             <span class="price_t">&yen;${order.total }</span><br/>
    86 
    87 <c:if test="${order.status eq 2 and btn eq 'deliver' }">
    88     <a id="deliver" href="<c:url value='/admin/AdminOrderServlet?method=deliver&oid=${order.oid }'/>">发  货</a>
    89 </c:if>
    90 <c:if test="${order.status eq 1 and btn eq 'cancel' }">
    91     <a id="cancel" href="<c:url value='/admin/AdminOrderServlet?method=cancel&oid=${order.oid }'/>">取  消</a>
    92 </c:if>
    93         </div>
    94     </div>
    95 </body>
    96 </html>

     

    2.servlet层

    (1)AdminOrderServlet.java 

      1 public class AdminOrderServlet extends BaseServlet {
      2     private OrderService service = new OrderService();
      3     /**
      4      * 获取当前请求的url
      5      * @param req
      6      * @return
      7      */
      8     private String getUrl(HttpServletRequest req) {
      9         String url = req.getRequestURI() + "?" + req.getQueryString();
     10         /*
     11          * 如果url中存在pc参数,截取掉,如果不存在那就不用截取。
     12          */
     13         int index = url.lastIndexOf("&pc=");
     14         if(index != -1) {
     15             url = url.substring(0, index);
     16         }
     17         return url;
     18     }
     19     
     20     /**
     21      * 获取当前页
     22      * @param req
     23      * @return
     24      */
     25     private int getPc(HttpServletRequest req) {
     26         int pc = 1;
     27         String param = req.getParameter("pc");
     28         if(param != null && !param.trim().isEmpty()) {
     29             try {
     30                 pc = Integer.parseInt(param);
     31             } catch (Exception e) {
     32                 e.printStackTrace();
     33             }
     34         }
     35         return pc;
     36     }
     37     
     38     /**
     39      * 查询所有订单
     40      * @param req
     41      * @param resp
     42      * @return
     43      * @throws ServletException
     44      * @throws IOException
     45      */
     46     public String findAll(HttpServletRequest req, HttpServletResponse resp)
     47             throws ServletException, IOException {
     48         int pc = getPc(req);
     49         PageBean<Order> pb = service.findAll(pc);
     50         pb.setUrl(getUrl(req));
     51         req.setAttribute("pb", pb);
     52         return "f:/adminjsps/admin/order/list.jsp";
     53     }
     54     
     55     /**
     56      * 查看订单详细信息
     57      * @param req
     58      * @param resp
     59      * @return
     60      * @throws ServletException
     61      * @throws IOException
     62      */
     63     public String load(HttpServletRequest req, HttpServletResponse resp)
     64             throws ServletException, IOException {
     65         String oid = req.getParameter("oid");
     66         Order order = service.load(oid);
     67         req.setAttribute("order", order);
     68         String btn = req.getParameter("btn");//btn说明了用户点击哪个超链接来访问本方法的
     69         req.setAttribute("btn", btn);
     70         return "/adminjsps/admin/order/desc.jsp";
     71     }
     72     
     73     /**
     74      * 取消订单
     75      * @param req
     76      * @param resp
     77      * @return
     78      * @throws ServletException
     79      * @throws IOException
     80      */
     81     public String cancel(HttpServletRequest req, HttpServletResponse resp)
     82             throws ServletException, IOException {
     83         String oid = req.getParameter("oid");
     84         /*
     85          * 校验订单状态
     86          */
     87         int status = service.findStatus(oid);
     88         if(status != 1) {
     89             req.setAttribute("code", "error");
     90             req.setAttribute("msg", "状态不对,不能取消!");
     91             return "f:/adminjsps/msg.jsp";
     92         }
     93         service.updateStatus(oid, 5);//设置状态为取消!
     94         req.setAttribute("code", "success");
     95         req.setAttribute("msg", "您的订单已取消,您不后悔吗!");
     96         return "f:/adminjsps/msg.jsp";        
     97     }
     98     
     99     /**
    100      * 发货功能
    101      * @param req
    102      * @param resp
    103      * @return
    104      * @throws ServletException
    105      * @throws IOException
    106      */
    107     public String deliver(HttpServletRequest req, HttpServletResponse resp)
    108             throws ServletException, IOException {
    109         String oid = req.getParameter("oid");
    110         /*
    111          * 校验订单状态
    112          */
    113         int status = service.findStatus(oid);
    114         if(status != 2) {
    115             req.setAttribute("code", "error");
    116             req.setAttribute("msg", "状态不对,不能发货!");
    117             return "f:/adminjsps/msg.jsp";
    118         }
    119         service.updateStatus(oid, 3);//设置状态为取消!
    120         req.setAttribute("code", "success");
    121         req.setAttribute("msg", "您的订单已发货,请查看物流,马上确认吧!");
    122         return "f:/adminjsps/msg.jsp";        
    123     }
    124     
    125     /**
    126      * 根据状态查询
    127      * @param req
    128      * @param resp
    129      * @return
    130      * @throws ServletException
    131      * @throws IOException
    132      */
    133     public String findByStatus(HttpServletRequest req, HttpServletResponse resp)
    134             throws ServletException, IOException {
    135         int pc = getPc(req);
    136         String status = req.getParameter("status");
    137         PageBean<Order> pb = service.findByStatus(status, pc);
    138         pb.setUrl(getUrl(req));
    139         req.setAttribute("pb", pb);
    140         return "f:/adminjsps/admin/order/list.jsp";
    141     }
    142     
    143     
    144 }

    3.service层

    (1)OrderService.java

     

      1 public class OrderService {
      2     private OrderDao dao = new OrderDao();
      3 
      4     /**
      5      * 我的订单
      6      * @param uid
      7      * @param pc
      8      * @return
      9      */
     10     public PageBean<Order> myOrders(String uid, int pc) {
     11         try {
     12             JdbcUtils.beginTransaction();
     13             PageBean<Order> pb = dao.findByUser(uid, pc);
     14             JdbcUtils.commitTransaction();
     15             return pb;
     16         } catch (SQLException e) {
     17             try {
     18                 JdbcUtils.rollbackTransaction();
     19             } catch (SQLException e1) {
     20                 e1.printStackTrace();
     21             }
     22             throw new RuntimeException(e);
     23         }
     24     }
     25     
     26     /**
     27      * 生成订单
     28      * @param order
     29      */
     30     public void createOrder(Order order) {
     31         try {
     32             JdbcUtils.beginTransaction();
     33             dao.add(order);
     34             JdbcUtils.commitTransaction();
     35         } catch (SQLException e) {
     36             try {
     37                 JdbcUtils.rollbackTransaction();
     38             } catch (SQLException e1) {
     39                 e1.printStackTrace();
     40             }
     41             e.printStackTrace();
     42         }
     43     }
     44     
     45     /**
     46      * 根据id加载订单
     47      * @param id
     48      * @return
     49      */
     50     public Order load(String id) {
     51         try {
     52             return dao.findById(id);
     53         } catch (SQLException e) {
     54             throw new RuntimeException(e);
     55         }
     56     }
     57     
     58     /**
     59      * 修改订单状态
     60      * @param id
     61      * @param status
     62      */
     63     public void updateStatus(String id, int status) {
     64         try {
     65             dao.updateStatus(id, status);
     66         } catch (SQLException e) {
     67             throw new RuntimeException(e);
     68         }
     69     }
     70 
     71     /**
     72      * 查找订单状态
     73      * @param id
     74      * @return
     75      */
     76     public int findStatus(String id) {
     77         try {
     78             return dao.findStatus(id);
     79         } catch (SQLException e) {
     80             throw new RuntimeException(e);
     81         }
     82     }
     83     
     84     /**
     85      * 查找所有订单
     86      * @param pc
     87      * @return
     88      */
     89     public PageBean<Order> findAll(int pc) {
     90         try {
     91             return dao.findAll(pc);
     92         } catch (SQLException e) {
     93             throw new RuntimeException(e);
     94         }
     95     }
     96     
     97     /**
     98      * 根据状态查询
     99      * @param status
    100      * @param pc
    101      * @return
    102      */
    103     public PageBean<Order> findByStatus(String status, int pc) {
    104         try {
    105             return dao.findByStatus(status, pc);
    106         } catch (SQLException e) {
    107             throw new RuntimeException(e);
    108         }
    109     }
    110     
    111     
    112 }

    4.dao层

    (1)OrderDao.java

      1 public class OrderDao {
      2     private QueryRunner qr = new TxQueryRunner();
      3     /**
      4      * 按用户id查找
      5      * @param uid
      6      * @return
      7      * @throws SQLException 
      8      */
      9     public PageBean<Order> findByUser(String uid, int pc) throws SQLException {
     10         List<Expression> exprList = new ArrayList<Expression>();
     11         exprList.add(new Expression("uid", "=", uid));
     12         return findByCriteria(exprList, pc);
     13     }
     14 
     15     /**
     16      * 按条件查找
     17      * @param exprList
     18      * @return
     19      * @throws SQLException 
     20      */
     21     private PageBean<Order> findByCriteria(List<Expression> exprList, int pc) throws SQLException {
     22         /*
     23          * 1. 得到pageSize
     24          * 2. 得到totalRecords
     25          * 3. 得到beanList
     26          * 4. 创建PageBean,返回
     27          */
     28         // 1. 得到pageSize
     29         int ps = PageConfig.ORDER_PAGE_SIZE;
     30         
     31         // 2. 通过exprList来生成where子句
     32         StringBuilder sb = new StringBuilder(" where 1=1");
     33         List<Object> params = new ArrayList<Object>();
     34         for(Expression expr : exprList) {
     35             sb.append(" and ").append(expr.getName()).append(" ")
     36                 .append(expr.getOperator()).append(" ");
     37             if(!expr.getOperator().equalsIgnoreCase("is null")) {
     38                 sb.append("?");
     39                 params.add(expr.getValue());
     40             }
     41         }
     42         
     43         // 3. 总记录数 
     44         String sql = "select count(*) from t_order" + sb;
     45         Number number = (Number)qr.query(sql, new ScalarHandler(), params.toArray());
     46         int tr = number.intValue();
     47         
     48         // 4. 得到beanList,即当前页记录
     49         sql = "select * from t_order" + sb + " order by ordertime desc limit ?,?";
     50         params.add((pc - 1) * ps);//当前页首行记录的下标
     51         params.add(ps);
     52         
     53         List<Order> beanList = qr.query(sql, new BeanListHandler<Order>(Order.class), params.toArray());
     54         // 虽然已经获取所有的订单,但每个订单中并没有订单条目。
     55         // 遍历每个订单,为其加载它的所有订单条目
     56         for(Order order : beanList) {
     57             loadOrderItem(order);
     58         }
     59         
     60         // 5. 创建PageBean,设置参数
     61         PageBean<Order> pb = new PageBean<Order>();
     62         pb.setBeanList(beanList);
     63         pb.setCurrentPage(pc);
     64         pb.setPageSize(ps);
     65         pb.setTotalRecords(tr);
     66         return pb;
     67     }
     68 
     69     /*
     70      * 为指定的order载它的所有OrderItem
     71      */
     72     private void loadOrderItem(Order order) throws SQLException {
     73         String sql = "select * from t_orderItem where oid=?";//t_orderItem已经包含book表的一些字段
     74         List<Map<String,Object>> mapList = qr.query(sql, new MapListHandler(), order.getOid());
     75         List<OrderItem> orderItems = toOrderItemList(mapList);
     76         order.setOrderItems(orderItems);
     77     }
     78 
     79     /**
     80      * 把多个Map转换成多个OrderItem
     81      * @param mapList
     82      * @return
     83      */
     84     private List<OrderItem> toOrderItemList(List<Map<String, Object>> mapList) {
     85         List<OrderItem> items = new ArrayList<OrderItem>();
     86         OrderItem item = null;
     87         for(Map<String,Object> map : mapList) {
     88             item = toOrderItem(map);
     89             items.add(item);
     90         }
     91         return items;
     92     }
     93 
     94     /*
     95      * 把一个Map转换成一个OrderItem
     96      */
     97     private OrderItem toOrderItem(Map map) {
     98         OrderItem item = CommonUtils.toBean(map, OrderItem.class);
     99         Book book = CommonUtils.toBean(map, Book.class);
    100         item.setBook(book);
    101         return item;
    102     }
    103     
    104     /**
    105      * 生成订单
    106      * @param order
    107      * @throws SQLException
    108      */
    109     public void add(Order order) throws SQLException {
    110         /*
    111          * 1. 插入订单
    112          */
    113         String sql = "insert into t_order values (?,?,?,?,?,?)";
    114         Object [] params = {order.getOid(), order.getOrderTime(), order.getTotal(), order.getStatus(), order.getAddress(), order.getUser().getUid()};
    115         qr.update(sql, params);
    116         
    117         /*
    118          * 2. 循环遍历订单的所有条目,让每个条目生成一个Object[]
    119          * 多个条目就对应Object[][]
    120          * 执行批处理,完成插入订单条目
    121          */
    122         sql = "insert into t_orderItem values (?,?,?,?,?,?,?,?)";
    123         List<OrderItem> items = order.getOrderItems();
    124         int len = items.size();
    125         Object [][] params2 = new Object[len][];
    126         OrderItem item = null;
    127         Book book = null;
    128         for(int i=0 ; i<len ; i++) {
    129             item = items.get(i);
    130             book = item.getBook();
    131             params2[i] = new Object[]{item.getOrderItemId(), item.getQuantity(), item.getSubtotal(),book.getBid(), 
    132                     book.getBname(), book.getCurrPrice(), book.getImage_b(), item.getOrder().getOid()};
    133         }
    134         qr.batch(sql, params2);
    135     }
    136     
    137     /**
    138      * 根据id查找
    139      * @param id
    140      * @return
    141      * @throws SQLException
    142      */
    143     public Order findById(String id) throws SQLException {
    144         String sql = "select * from t_order where oid=?";
    145         Order order = qr.query(sql, new BeanHandler<Order>(Order.class), id);
    146         loadOrderItem(order);
    147         return order;
    148     }
    149     
    150     /**
    151      * 修改订单状态
    152      * @param id
    153      * @param status
    154      * @throws SQLException
    155      */
    156     public void updateStatus(String id, int status) throws SQLException {
    157         String sql = "update t_order set status=? where oid=?";
    158         qr.update(sql, status, id);
    159     }
    160     
    161     /**
    162      * 查找订单状态
    163      * @param id
    164      * @return
    165      * @throws SQLException
    166      */
    167     public int findStatus(String id) throws SQLException {
    168         String sql = "select status from t_order where oid=?";
    169         Number status = (Number) qr.query(sql, new ScalarHandler(), id);
    170         return status.intValue();
    171     }
    172     
    173     /**
    174      * 查询所有订单
    175      * @param pc
    176      * @return
    177      * @throws SQLException
    178      */
    179     public PageBean<Order> findAll(int pc) throws SQLException {
    180         List<Expression> exprList = new ArrayList<Expression>();
    181         return findByCriteria(exprList, pc);
    182     }
    183     
    184     /**
    185      * 根据状态查询
    186      * @param status
    187      * @param pc
    188      * @return
    189      * @throws SQLException
    190      */
    191     public PageBean<Order> findByStatus(String status, int pc) throws SQLException {
    192         List<Expression> exprList = new ArrayList<Expression>();
    193         exprList.add(new Expression("status", "=", status));
    194         return findByCriteria(exprList, pc);
    195     }
    196     
    197 }

    项目源码:

    http://pan.baidu.com/s/1gdVELIr

  • 相关阅读:
    drf-通过drf-extensions扩展来实现缓存
    social_django第三方登录 没有token解决方法
    python-项目日志配置使用
    drf-支付宝支付
    git 相关命令
    django第三方登录与邮箱验证流程
    django项目部署
    数组中的方法
    滚动到页面底部,更新数据
    图片卷边
  • 原文地址:https://www.cnblogs.com/shamgod/p/5181969.html
Copyright © 2020-2023  润新知