ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

按条件查询功能

2021-12-18 10:58:49  阅读:113  来源: 互联网

标签:category 功能 String list request 查询 条件 id name


一、创建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);                                                          
}                                                                             

标签:category,功能,String,list,request,查询,条件,id,name
来源: https://blog.csdn.net/daqi1983/article/details/122009611

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有