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 public String 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); }
5.页面开发
function condition() { //取出查询条件 var pname = $("#pname").val(); var typeid = $("#typeid").val(); var lprice = $("#lprice").val(); var hprice = $("#hprice").val(); $.ajax({ type:"post", url:"${pageContext.request.contextPath}/prod/condition.action", data:{"pname":pname,"typeid":typeid,"lprice":lprice,"hprice":hprice}, //"pname"是ProductInfoVo的成员变量,pname是上面那个从文本框获取到的 success:function () { //刷新显示数据的容器 $("#table").load("http://localhost:8080/admin/product.jsp #table"); } }); }
测试时页面无法显示,因为多条件查询还没有和分页连接上