OrderDao
1 public PageBean<Order> findByUser(String uid, int pc) throws SQLException { 2 List<Expression> exprList = new ArrayList<Expression>(); 3 exprList.add(new Expression("uid", "=", uid)); 4 return findByCriteria(exprList, pc); 5 } 6 7 private PageBean<Order> findByCriteria(List<Expression> exprList, int pc) throws SQLException { 8 /* 9 * 1. 得到ps 10 * 2. 得到tr 11 * 3. 得到beanList 12 * 4. 创建PageBean,返回 13 */ 14 /* 15 * 1. 得到ps 16 */ 17 int ps = PageConstants.ORDER_PAGE_SIZE;//每页记录数 18 /* 19 * 2. 通过exprList来生成where子句 20 */ 21 StringBuilder whereSql = new StringBuilder(" where 1=1"); 22 List<Object> params = new ArrayList<Object>();//SQL中有问号,它是对应问号的值 23 for(Expression expr : exprList) { 24 /* 25 * 添加一个条件上, 26 * 1) 以and开头 27 * 2) 条件的名称 28 * 3) 条件的运算符,可以是=、!=、>、< ... is null,is null没有值 29 * 4) 如果条件不是is null,再追加问号,然后再向params中添加一与问号对应的值 30 */ 31 whereSql.append(" and ").append(expr.getName()) 32 .append(" ").append(expr.getOperator()).append(" "); 33 // where 1=1 and bid = ? 34 if(!expr.getOperator().equals("is null")) { 35 whereSql.append("?"); 36 params.add(expr.getValue()); 37 } 38 } 39 40 /* 41 * 3. 总记录数 42 */ 43 String sql = "select count(*) from t_order" + whereSql; 44 Number number = (Number)qr.query(sql, new ScalarHandler(), params.toArray()); 45 int tr = number.intValue();//得到了总记录数 46 /* 47 * 4. 得到beanList,即当前页记录 48 */ 49 sql = "select * from t_order" + whereSql + " 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), 54 params.toArray()); 55 // 虽然已经获取所有的订单,但每个订单中并没有订单条目。 56 // 遍历每个订单,为其加载它的所有订单条目 57 for(Order order : beanList) { 58 loadOrderItem(order); 59 } 60 61 /* 62 * 5. 创建PageBean,设置参数 63 */ 64 PageBean<Order> pb = new PageBean<Order>(); 65 /* 66 * 其中PageBean没有url,这个任务由Servlet完成 67 */ 68 pb.setBeanList(beanList); 69 pb.setPc(pc); 70 pb.setPs(ps); 71 pb.setTr(tr); 72 73 return pb; 74 } 75 76 /* 77 * 为指定的order载它的所有OrderItem 78 */ 79 private void loadOrderItem(Order order) throws SQLException { 80 /* 81 * 1. 给sql语句select * from t_orderitem where oid=? 82 * 2. 执行之,得到List<OrderItem> 83 * 3. 设置给Order对象 84 */ 85 String sql = "select * from t_orderitem where oid=?"; 86 List<Map<String,Object>> mapList = qr.query(sql, new MapListHandler(), order.getOid()); 87 List<OrderItem> orderItemList = toOrderItemList(mapList); 88 89 order.setOrderItemList(orderItemList); 90 } 91 92 /** 93 * 把多个Map转换成多个OrderItem 94 * @param mapList 95 * @return 96 */ 97 private List<OrderItem> toOrderItemList(List<Map<String, Object>> mapList) { 98 List<OrderItem> orderItemList = new ArrayList<OrderItem>(); 99 for(Map<String,Object> map : mapList) { 100 OrderItem orderItem = toOrderItem(map); 101 orderItemList.add(orderItem); 102 } 103 return orderItemList; 104 } 105 106 /* 107 * 把一个Map转换成一个OrderItem 108 */ 109 private OrderItem toOrderItem(Map<String, Object> map) { 110 OrderItem orderItem = CommonUtils.toBean(map, OrderItem.class); 111 Book book = CommonUtils.toBean(map, Book.class); 112 orderItem.setBook(book); 113 return orderItem; 114 }
OrderService
1 public PageBean<Order> myOrders(String uid,int pc) 2 { 3 try{ 4 JdbcUtils.beginTransaction(); 5 PageBean<Order> pb=orderDao.findByUser(uid, pc); 6 JdbcUtils.commitTransaction(); 7 return pb; 8 9 }catch(SQLException e) 10 { 11 try { 12 JdbcUtils.rollbackTransaction(); 13 } catch (SQLException e1) { 14 throw new RuntimeException(e); 15 } 16 17 } 18 return null; 19 20 }
OrderServlet
1 /** 2 * 获取当前页码 3 * @param req 4 * @return 5 */ 6 private int getPc(HttpServletRequest req) { 7 int pc = 1; 8 String param = req.getParameter("pc"); 9 if(param != null && !param.trim().isEmpty()) { 10 try { 11 pc = Integer.parseInt(param); 12 } catch(RuntimeException e) {} 13 } 14 return pc; 15 } 16 17 /** 18 * 截取url,页面中的分页导航中需要使用它做为超链接的目标! 19 * @param req 20 * @return 21 */ 22 /* 23 * http://localhost:8080/goods/BookServlet?methed=findByCategory&cid=xxx&pc=3 24 * /goods/BookServlet + methed=findByCategory&cid=xxx&pc=3 25 */ 26 private String getUrl(HttpServletRequest req) { 27 String url = req.getRequestURI() + "?" + req.getQueryString(); 28 /* 29 * 如果url中存在pc参数,截取掉,如果不存在那就不用截取。 30 */ 31 int index = url.lastIndexOf("&pc="); 32 if(index != -1) { 33 url = url.substring(0, index); 34 } 35 return url; 36 } 37 38 /** 39 * 我的订单 40 * @param req 41 * @param resp 42 * @return 43 * @throws ServletException 44 * @throws IOException 45 */ 46 public String myOrders(HttpServletRequest req, HttpServletResponse resp) 47 throws ServletException, IOException { 48 /* 49 * 1. 得到pc:如果页面传递,使用页面的,如果没传,pc=1 50 */ 51 int pc = getPc(req); 52 /* 53 * 2. 得到url:... 54 */ 55 String url = getUrl(req); 56 /* 57 * 3. 从当前session中获取User 58 */ 59 User user = (User)req.getSession().getAttribute("sessionUser"); 60 61 /* 62 * 4. 使用pc和cid调用service#findByCategory得到PageBean 63 */ 64 PageBean<Order> pb = orderService.myOrders(user.getUid(), pc); 65 /* 66 * 5. 给PageBean设置url,保存PageBean,转发到/jsps/book/list.jsp 67 */ 68 pb.setUrl(url); 69 req.setAttribute("pb", pb); 70 return "f:/jsps/order/list.jsp"; 71 }