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

通过将值存入map
@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);
	}

你可能感兴趣的:(java,sql,map)