案例2-查询订单的详情
需求:
只有未付款的订单,点击"付款",跳转到订单的详情页面
步骤分析:
1.在订单列表,点击付款,
/store/order?method=getById&oid=?
2.在orderservlet中编写getById()
接受oid
调用service 返回Order
将order放入域中,请求转发 order_info.jsp
3.orderDao中 通过一个订单号,查询订单详情
select * from orderitem oi,product p where oi.pid = p.pid and oi.oid = ?
用mapListhandler封装结果集,然后使用BeanUtils封装成指定的bean对象 ,添加到order的items中即可
/store/WebContent/jsp/order_list.jsp
<c:if test="${o.state==0 }">
<a href="${pageContext.request.contextPath }/order?method=getById&oid=${o.oid}">付款</a>
</c:if>
<c:if test="${o.state==1 }">
已付款
</c:if>
<c:if test="${o.state==2 }">
<a href="#">确认收货</a>
</c:if>
<c:if test="${o.state==3 }">
已完成
</c:if>
com.louis.web.servlet.OrderServlet
public String getById(HttpServletRequest request, HttpServletResponse response) throws Exception {
//1.获取oid
String oid = request.getParameter("oid");
//2.调用service 通过oid 返回值:order
OrderService os=(OrderService) BeanFactory.getBean("OrderService");
Order order=os.getById(oid);
//3.将order放入request域中
request.setAttribute("bean", order);
return "/jsp/order_info.jsp";
}
com.louis.service.impl.OrderServiceImpl
/**
* 查看订单详情
*/
@Override
public Order getById(String oid) throws Exception {
OrderDao od=(OrderDao) BeanFactory.getBean("OrderDao");
return od.getById(oid);
}
com.louis.dao.impl.OrderDaoImpl
/**
* 通过oid 查询订单详情
*/
@Override
public Order getById(String oid) throws Exception {
QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
String sql="select * from orders where oid = ?";
Order order = qr.query(sql, new BeanHandler<>(Order.class), oid);
//封装orderitems
sql="select * from orderitem oi,product p where oi.pid = p.pid and oi.oid = ?";
List<Map<String, Object>> query = qr.query(sql, new MapListHandler(), oid);
for (Map<String, Object> map : query) {
//封装product
Product product = new Product();
BeanUtils.populate(product, map);
//封装orderitem
OrderItem oi = new OrderItem();
BeanUtils.populate(oi, map);
oi.setProduct(product);
//将orderitem假如中order的items中
order.getItems().add(oi);
}
return order;
}