按条件查询功能

一、创建FindProductByManyConditionServlet

/findProductByManyCondition

public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//1.获取表单数据
		String id = request.getParameter("id"); // 商品id
		String name = request.getParameter("name"); // 商品名称
		String category = request.getParameter("category"); // 商品类别
		String minprice = request.getParameter("minprice"); // 最小价格
		String maxprice = request.getParameter("maxprice"); // 最大价格
		// 2.创建ProductService对象
		ProductService service = new ProductService();
		// 3.调用service层用于条件查询的方法
		List<Product> ps = service.findProductByManyCondition(id, name,
				category, minprice, maxprice);
		// 4.将条件查询的结果放进request域中
		request.setAttribute("ps", ps);
		// 5.请求重定向到商品管理首页list.jsp页面
		request.getRequestDispatcher("/admin/products/list.jsp").forward(
				request, response);
	}

二、ProductService添加方法

// 多条件查询                                                                 
public List<Product> findProductByManyCondition(String id, String name,  
		String category, String minprice, String maxprice) {             
	List<Product> ps = null;                                             
	try {                                                                
		ps = dao.findProductByManyCondition(id, name, category, minprice,
				maxprice);                                               
	} catch (SQLException e) {                                           
		e.printStackTrace();                                             
	}                                                                    
	return ps;                                                           
}                                                                        

三、ProcductDao添加方法

// 多条件查询                                                                      
public List<Product> findProductByManyCondition(String id, String name,       
		String category, String minprice, String maxprice)                    
		throws SQLException {                                                 
	List<Object> list = new ArrayList<Object>();                              
	String sql = "select * from products where 1=1 ";                         
                                                                              
	QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());    
                                                                              
	if (id != null && id.trim().length() > 0) {                               
		sql += " and id=?";                                                   
		list.add(id);                                                         
	}                                                                         
                                                                              
	if (name != null && name.trim().length() > 0) {                           
		sql += " and name=?";                                                 
		list.add(name);                                                       
	}                                                                         
	if (category != null && category.trim().length() > 0) {                   
		sql += " and category=?";                                             
		list.add(category);                                                   
	}                                                                         
	if (minprice != null && maxprice != null                                  
			&& minprice.trim().length() > 0 && maxprice.trim().length() > 0) {
		sql += " and price between ? and ?";                                  
		list.add(minprice);                                                   
		list.add(maxprice);                                                   
	}                                                                         
                                                                              
	Object[] params = list.toArray();                                         
                                                                              
	return runner.query(sql, new BeanListHandler<Product>(Product.class),     
			params);                                                          
}                                                                             
上一篇:慢慢说设计模式:建造者模式


下一篇:linux的统计实现