利用map格式参数传值,接收拼接sql语句_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > 利用map格式参数传值,接收拼接sql语句

利用map格式参数传值,接收拼接sql语句

 2014/8/21 1:24:01  759499199  程序员俱乐部  我要评论(0)
  • 摘要:通过将值存入map@RequestMapping(value="/list",method=RequestMethod.POST)@ResponseBodypublicMap<String,Object>list(HttpServletRequestrequest)throwsThrowable{StringpageNo=request.getParameter("pager.pageNo");StringpageSize=request.getParameter("pager
  • 标签:Map 利用 SQL SQL语句
通过将值存入map
class="java">
@RequestMapping(value="/list", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> list(HttpServletRequest request) throws Throwable {
		
		String pageNo = request.getParameter("pager.pageNo");
		String pageSize = request.getParameter("pager.pageSize");
		Map<String, Object> map = new HashMap<String, Object>();
		String product_name = request.getParameter("product_name");
		if(product_name!=null && !"null".equals(product_name) && !"".equals(product_name)){
			map.put("product_name like", "%" + product_name + "%");	
		}
		String basic_seller_name = request.getParameter("basic_seller_name");
		if(basic_seller_name!=null && !"null".equals(basic_seller_name) && !"".equals(basic_seller_name)){
			map.put("basic_seller_name like", "%" + basic_seller_name + "%");
		}
		List<Map<String, Object>> list = productService.findViewByMapPage(map, Integer.parseInt(pageNo), Integer.parseInt(pageSize));
		int total = productService.findViewByMapCount(map);
		// 页面需要显示的列名
		String[] column = {
				"product_id",
				"product_name",
				"product_specifications",
				"product_short_name",
				"product_unit_name",
				"basic_seller_name",
				"product_sort_name",
				"product_brand_name",
				"product_image",
				"product_origin",
				"product_manufacturer",
				"product_pinyin",
				"product_sell_stop",
				"product_buyer_stop",
				"product_minus_stock",
				"product_barcode_number",
				"product_update",
		};
		return Public.listTojson(list, column, pageNo, pageSize, total);
	}

sql语句拼接利用迭代器将map循环
public List<Map<String, Object>> findViewByMapPage(Map<String, Object> map,
			int pageNo, int pageSize) {
		StringBuffer sql = new StringBuffer("select * from " + TableName.product_view);
		Iterator it = map.entrySet().iterator();
		Object object[] = new Object[map.size()];
		int i = 0;
		if (map.size() > 0) {
			while (it.hasNext()) {
				Map.Entry entry = (Map.Entry) it.next();
				String key = entry.getKey().toString();
				Object value = entry.getValue();
				if (i==0) {
					sql.append(" where " + key + " ?");
					object[i] = value;
				} else if (i==1){
					sql.append(" and " + key + " ?");
					object[i] = value;
				}	
				i++;
			}
		}
		sql.append(" order by product_id desc");
		return baseDao.queryForList(sql.toString(), object, this.findViewByMapCount(map), pageNo, pageSize, ProductServiceImpl.class);
	}

或者sql
public List<Map<String, Object>> findViewByMapPage(Map<String, Object> map,
			int pageNo, int pageSize) {
		StringBuffer sql = new StringBuffer("select * from " + TableName.product_view);
		Iterator it = map.entrySet().iterator();
		Object[] object = new Object[map.size()];
		int i = 0;
		if (map.size() > 0) {
			while (it.hasNext()) {
				Map.Entry entry = (Map.Entry) it.next();
				String key = entry.getKey().toString();
				Object value = entry.getValue();
				sql.append(" where " + key + " ?");
				object[i] = value;
				i++;
			}
		}
		sql.append(" order by product_id desc");
		return baseDao.queryForList(sql.toString(), object, this.findViewByMapCount(map), pageNo, pageSize, ProductServiceImpl.class);
	}
发表评论
用户名: 匿名