按条件查询订单

按条件查询订单,第1张

按条件查询订单 创建Servlet

单击“查询”,提交表单,发送“/findOrderByManyCondition”,由FindOrderByManyConditionServlet处理

public class FindOrderByManyConditionServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
        //获取订单编号和收件人名称
		String id = request.getParameter("id");
		String receiverName = request.getParameter("receiverName");
        //创建Service层对象
		OrderService service = new OrderService();
		//调用Service层OrderService类的findOrderByManyCondition()方法查询数据
		List orders = service.findOrderByManyCondition(id, receiverName);
        //将查询结果添加到request作用域中
		request.setAttribute("orders", orders);
        //请求转发到list.jsp页面,并将request请求和response响应也转发到该页面中
		request.getRequestDispatcher("/admin/orders/list.jsp").forward(request,
				response);
	}
}
编写Service层方法
// 多条件查询订单信息                                                                 
public List findOrderByManyCondition(String id, String receiverName) {
	List orders = null;                                               
	try {                                                                    
		orders = odao.findOrderByManyCondition(id, receiverName);            
	} catch (SQLException e) {                                               
		e.printStackTrace();                                                 
	}                                                                        
	return orders;                                                           
}                                                                            
编写DAO层方法
public List findOrderByManyCondition(String id, String receiverName)
		throws SQLException {
	//1.创建集合对象
	List objs = new ArrayList();
	//2.定义查询sql
	String sql = "select orders.*,user.* from orders,user where user.id=orders.user_id ";
	//3.根据参数拼接sql语句
	if (id != null && id.trim().length() > 0) {
		sql += " and orders.id=?";
		objs.add(id);
	}
	if (receiverName != null && receiverName.trim().length() > 0) {
		sql += " and receiverName=?";
		objs.add(receiverName);
	}
	sql += " order by orders.user_id";
	//4.创建QueryRunner对象
	QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
	//5.返回QueryRunner对象query方法的执行结果
	return runner.query(sql, new ResultSetHandler>() {
		public List handle(ResultSet rs) throws SQLException {
			List orders = new ArrayList();
           //循环遍历出订单和用户信息
			while (rs.next()) {
				Order order = new Order();
				order.setId(rs.getString("orders.id"));
				order.setMoney(rs.getDouble("orders.money"));
				order.setOrdertime(rs.getDate("orders.ordertime"));
				order.setPaystate(rs.getInt("orders.paystate"));
				order.setReceiverAddress(rs
						.getString("orders.receiverAddress"));
				order.setReceiverName(rs.getString("orders.receiverName"));
				order.setReceiverPhone(rs.getString("orders.receiverPhone"));
				orders.add(order);
				User user = new User();
				user.setId(rs.getInt("user.id"));
				user.setEmail(rs.getString("user.email"));
				user.setGender(rs.getString("user.gender"));
				user.setActiveCode(rs.getString("user.activecode"));
				user.setIntroduce(rs.getString("user.introduce"));
				user.setPassword(rs.getString("user.password"));
				user.setRegistTime(rs.getDate("user.registtime"));
				user.setRole(rs.getString("user.role"));
				user.setState(rs.getInt("user.state"));
				user.setTelephone(rs.getString("user.telephone"));
				user.setUsername(rs.getString("user.username"));
				order.setUser(user);

			}

			return orders;
		}
	}, objs.toArray());
}					
										


					

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5683416.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)