7-22模糊查询

做好农易商城网站的商品查询

1.html

<li>
			<form action="selectGoods" th:object="${goodsForm}" method="post">
			<input type="text" name="commodityName"/>
			<input type="submit" value="检索"/>
			</form></li>

2. 在controller中

  
    @RequestMapping(value = "selectGoods", method = RequestMethod.POST)
    public String  selectGoods(Model model,HttpSession session,GoodsForm goodsForm,Device device){
    	log.info("检索相关商品");
    	List<GoodsForm> list=goodsService.searchGoodsListrelative(goodsForm);
    	model.addAttribute("list",list);
    	UVO uvo = (UVO)session.getAttribute("UVO");
    	if (uvo == null) {
    		uvo = new UVO();
    		session.setAttribute("UVO", uvo);
    	}
    	CartForm cartForm = new CartForm();
    	cartForm.setGuestId(uvo.getGuestId());
    	model.addAttribute("cartList", cartService.searchCartList(cartForm));
    	if(device.isNormal()) {
    		return "shop/index";
    	} else {
    		return "mobile/index";
    	}
    }

3。在GoodService中

	public List<GoodsForm> searchGoodsListrelative(GoodsForm frm) {
		List<GoodsForm> result = queryDao.executeForObjectList("Goods.selectGoodsListrelative", frm);
		return result;
	}

4。Sql语句
  查询条件 commodity.commodity_name LIKE '%$commodityName$%'

<select id="selectGoodsListrelative"
		parameterClass="cn.agriculture.web.form.GoodsForm"
		resultClass="cn.agriculture.web.form.GoodsForm">
		SELECT commodity.commodity_id as commodityId,
			commodity.type as type,
			supplier.supplier_name as supplierName,
			brand.brand_name as brandName,
			commodity.commodity_name as commodityName,
			commodity.weight as weight,
			commodity.is_gift as isGift,
			commodity.specifications as specifications,
			commodity.unit as unit,
			commodity.benchmark_price as benchmarkPrice,
			commodity.guide_price as guidePrice,
			commodity.retail_price as retailPrice,
			commodity.competition_level as competitionLevel,
			commodity.note as note,
			commodity.update_time as updateTime,
			commodity.update_user as updateUser,
			commodity.picture_id as pictureId,
			stock.stock as stock
		FROM commodity, supplier, brand, stock
		WHERE commodity.commodity_id = stock.commodity_id
		AND commodity.supplier_id = supplier.supplier_id
		AND commodity.brand_id = brand.brand_id
		AND commodity.commodity_name LIKE '%$commodityName$%'


你可能感兴趣的:(7-22模糊查询)