69.商品專案(多條件查詢)
阿新 • • 發佈:2021-12-30
1.查詢條件的封裝物件
頁面傳送條件到後臺的物件一般命名有vo
package com.bjpowernode.pojo.vo; public class ProductInfoVo { //商品名稱 private String pname; //商品型別 private Integer typeid; //最低價格 private Integer lprice; //最高價格 private Integer hprice; //設定頁碼 private Integer page=1; @Override publicString toString() { return "ProductInfoVo{" + "pname='" + pname + '\'' + ", typeid=" + typeid + ", lprice=" + lprice + ", hprice=" + hprice + ", page=" + page + '}'; } public String getPname() {return pname; } public void setPname(String pname) { this.pname = pname; } public Integer getTypeid() { return typeid; } public void setTypeid(Integer typeid) { this.typeid = typeid; } public Integer getLprice() { return lprice; }public void setLprice(Integer lprice) { this.lprice = lprice; } public Integer getHprice() { return hprice; } public void setHprice(Integer hprice) { this.hprice = hprice; } public Integer getPage() { return page; } public void setPage(Integer page) { this.page = page; } public ProductInfoVo(String pname, Integer typeid, Integer lprice, Integer hprice, Integer page) { this.pname = pname; this.typeid = typeid; this.lprice = lprice; this.hprice = hprice; this.page = page; } public ProductInfoVo() { } }
2.mapper開發
<sql id="Base_Column_List" > p_id, p_name, p_content, p_price, p_image, p_number, type_id, p_date </sql>
<select id="selectCondition" parameterType="com.bjpowernode.pojo.vo.ProductInfoVo" resultMap="BaseResultMap"> select <include refid="Base_Column_List"></include> from product_info <!--拼接條件--> <where> <!--商品名稱不為空,拼接商品名稱模糊查詢--> <if test="pname != null and pname !=''"> and p_name like '%${pname}%' </if> <!--商品型別不為空,拼接商品型別查詢條件--> <if test="typeid != null and typeid != -1"> and type_id =#{typeid} </if> <!--如果最低價格不為空,最高價格為空,則查詢大於最低價格的所有商品--> <if test="(lprice != null and lprice != '') and (hprice == null or hprice == '')"> and p_price >= #{lprice} </if> <!--如果最高價格不為空,最低價格為空,則查詢小於最高價格的所有商品--> <if test="(hprice != null and hprice !='') and (lprice == null or lprice == '')"> and p_price <= #{hprice} </if> <!--如果最高和最低價格都不為空,則查詢介於最高價格和最低價格之間的所有商品--> <if test="(lprice !=null and lprice !='') and (hprice != null and hprice != '')"> and p_price between #{lprice} and #{hprice} </if> </where> order by p_id desc </select>
3.業務邏輯層
@Override public List<ProductInfo> selectCondition(ProductInfoVo vo) { return productInfoMapper.selectCondition(vo); }
4.控制器開發
// 多條件查詢功能實現 @ResponseBody @RequestMapping("/condition") public void condition(ProductInfoVo vo, HttpSession session){ List<ProductInfo> list = productInfoService.selectCondition(vo); session.setAttribute("list",list); }