查询我的订单
- 需求和技术分析
- 步骤分析
- 代码实现
需求和技术分析
**需求:**点击页面上的“我的订单”,分页展示我所有的订单(将所属的订单项也要查询出来)
技术分析:
- 多表查询
- 内连接
- 外连接
- 子查询
步骤分析
- 修改head.jsp的连接,我的订单:${path}/order/findAll
- 在OrderServlet中编写findAll方法
- 获取用户(session)
- 获取当前页
- 固定pageSize
- 调用service根据用户查询所有订单,返回值:PageBean
- 将返回值对象绑定到request域中转发到order_list.jsp
- 在service中编写findAll方法,返回一个PageBean对象
调用dao查询list和totalCount,将user.uid传递过去 - 在OrderDao中查询所有的订单
查出所有订单:select * from orders where uid=? limit ?,?
遍历所有的订单,根据订单id查询订单项表和商品表
select * from orderitem oi,product p where oi.pid=p.pid and oi.oid=?
用MapListHandler封装结果集,然后使用BeanUtil封装成指定的bean对象,添加到order的items中即可。
代码实现
① 修改页面上“我的订单”跳转地址
<li><a href="${path}/user/order/page?currPage=1">我的订单</a></li>
② 完成OrderServlet
/**
* 分页查询订单
* @param request
* @param response
* @throws Exception
*/
private void page(HttpServletRequest request, HttpServletResponse response) throws Exception {
//1、获取当前页
Integer currPage=Integer.parseInt(request.getParameter("currPage"));
Integer pageSize=3;
//2、获取用户
User user=(User) request.getSession().getAttribute("user");
if(user==null) {
request.setAttribute("msg", "你还没有登录,请先登录!");
request.getRequestDispatcher("/jsp/msg.jsp").forward(request, response);
}
//3、调用service 分页查询 参数:currPage pageSize user 返回值PageBean
PageBean<Order> page=orderService.page(currPage,pageSize,user);
//4、将pageBean放入request中
request.setAttribute("page", page);
request.getRequestDispatcher("/jsp/order_list.jsp").forward(request, response);
}
③ 完成OrderDaoImpl
/**
* 分页查询我的订单
*/
@Override
public List<Order> page(Integer currPage, Integer pageSize, String uid) throws Exception {
String sql="select * from orders where uid=? order by ordertime desc limit ?,?";
List<Order> list=qr.query(sql, new BeanListHandler<>(Order.class),uid,(currPage-1)*pageSize,pageSize);
//遍历订单集合,封装每个订单的订单项列表
sql="select * from orderitem oi,product p where oi.pid=p.pid and oid=?";
for(Order order:list) {
//当前订单包含的所有内容
List<Map<String , Object>> mList=qr.query(sql, new MapListHandler(),order.getOid());
for(Map<String, Object> map: mList) {
//封装product
Product p=new Product();
BeanUtils.populate(p, map);
//封装orderItem
OrderItem oi=new OrderItem();
BeanUtils.populate(oi, map);
oi.setProduct(p);
//封装到orderitemList
order.getItems().add(oi);
}
}
return list;
}
/**
* 查询我的订单的总数
*/
@Override
public int getTotalCount(String uid) throws Exception {
String sql="select count(*) from orders where uid=?";
return ((Long)qr.query(sql, new ScalarHandler(),uid)).intValue();
}
注意:由于MapListHandler不能对数据库查询别名进行封装,所以这里可以给Product添加三个set方法。
public void setMarket_price(Double market_price) {
this.marketPrice=market_price;
}
public void setShop_price(Double shop_price) {
this.shopPrice=shop_price;
}
public void setIs_hot(Integer is_hot) {
this.isHot=is_hot;
}
④ 更改页面展示数据
<table class="table table-bordered">
<!-- 遍历我的订单 -->
<c:forEach items="${page.list }" var="order">
<tbody>
<tr class="success">
<th colspan="5">
订单编号:${order.oid } 订单金额:${order.total }
<c:if test="${order.state==0 }">
<!-- 未付款 去付款 -->
<a href="#">付款</a>
</c:if>
<c:if test="${order.state==1 }">
已付款
</c:if>
<c:if test="${order.state==2 }">
<a href="#">确认收货</a>
</c:if>
<c:if test="${order.state==3 }">
已完成
</c:if>
</th>
</tr>
<tr class="warning">
<th>图片</th>
<th>商品</th>
<th>价格</th>
<th>数量</th>
<th>小计</th>
</tr>
<!-- 遍历订单项 -->
<c:forEach items="${order.items }" var="oi">
<tr class="active">
<td width="60" width="40%">
<input type="hidden" name="id" value="22">
<img src="${path }/${oi.product.pimage }" width="70" height="60">
</td>
<td width="30%">
<a target="_blank"> ${oi.product.pname }</a>
</td>
<td width="20%">
¥${oi.product.shopPrice }
</td>
<td width="10%">
${oi.count }
</td>
<td width="15%">
<span class="subtotal">¥${oi.subtotal }</span>
</td>
</tr>
</c:forEach>
</tbody>
</c:forEach>
</table>
分页:
把之前的分页拿过来,更改一下跳转路径:
<!--分页 -->
<div style="width:380px;margin:0 auto;margin-top:50px;">
<ul class="pagination" style="text-align:center; margin-top:10px;">
<!-- 判断当前页是否是首页(有无上一页) -->
<c:if test="${page.currPage==1 }">
<!-- 不可用disabled -->
<li class="disabled"><a href="javascript:void(0)" aria-label="Previous">
<span aria-hidden="true">«</span></a>
</li>
</c:if>
<c:if test="${page.currPage!=1 }">
<!-- 可用 -->
<li><a href="${path }/order/page?currPage=${page.currPage-1}" aria-label="Previous">
<span aria-hidden="true">«</span></a>
</li>
</c:if>
<!-- 展示所有的页码 -->
<c:forEach begin="${page.currPage-4>0?page.currPage-4:1 }" end="${page.currPage+3>page.totalPage?page.totalPage:page.currPage+3 }" var="n">
<!-- 判断是否是当前页 -->
<c:if test="${page.currPage==n }">
<li class="active"><a href="javascript:void(0)">${n }</a></li>
</c:if>
<c:if test="${page.currPage!=n }">
<li ><a href="${path }/order/page?currPage=${n}">${n }</a></li>
</c:if>
</c:forEach>
<!-- 判断是否是尾页(有无下一页) -->
<c:if test="${page.currPage==page.totalPage }">
<!-- 不可用 -->
<li class="disabled">
<a href="javascript:void(0)" aria-label="Next">
<span aria-hidden="true">»</span>
</a>
</li>
</c:if>
<!-- 不是尾页 -->
<c:if test="${page.currPage!=page.totalPage }">
<li>
<a href="${path }/order/page?currPage=${page.currPage+1}" aria-label="Next">
<span aria-hidden="true">»</span>
</a>
</li>
</c:if>
</ul>
</div>