springmvc 多条件参数查询

jsp视图部分

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page import="java.security.MessageDigest"%>
<c:set var="ctx" value="${pageContext.request.contextPath}" />     //获取项目根目录地址
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>    //声明调用

//根据店铺名称或者所在机构查询   jquery部分
$('#btnSearch',navTab.getCurrentPanel()).click(function() {         //navTab.getCurrentPanel()   表示取当前web下的ID
var psize = $("#nowPageSize",navTab.getCurrentPanel()).val();
var Searchinstid = $("#Searchinstid",navTab.getCurrentPanel()).val();               //获取查询的值
var Searchname = $("#Searchname",navTab.getCurrentPanel()).val();
if(Searchname == ""){
Searchname = "all";
}
Searchname = encodeURIComponent(encodeURIComponent(Searchname));        //编码   避免中文乱码问题
navTab.openTab("beauty", "${ctx}/beauty/Search/"+Searchname+"/"+Searchinstid, { title:"美容店管理", fresh:true, data:{page:1,rows:psize} });
});             //url传递参数

//查询字段部分

<div class="pageHeader">
<div class="searchBar">
<ul class="searchContent">
<li style="240px">
<label style="40px">名称:</label>
<input type="text" name="Searchname" id="Searchname" placeholder="请输入美容店名称" value="${Searchshopname }" /> //value="${Searchshopname }"接收后台的回写查询数据
</li>
<li>
<label style="60px">所在机构:</label>
<select style="height:22px" name="Searchinstid" id="Searchinstid" >
<c:forEach items="${insts }" var="shop">
<option value="${shop.id }" <c:if test="${shop.id==Searchinstitutionid}">selected </c:if>>${shop.institutionname }</option>//判断当前选项并保留
</c:forEach>
</select>
</li>
<li style="margin-left: -100px;"><button type="button" style="margin-right: 5px;" target="ajaxTodo" id="btnSearch" name="btnSearch">查询</button></li>
</ul>
</div>
</div>

-------------------------------------------------------------------------------------------------------------------------------------------------------

Controller控制器部分

//分页查询
@RequestMapping(value="/Search/{Searchname}/{Searchinstid}", method = RequestMethod.GET)   // get方式传递接收地址参数
public String Search(
@RequestParam(required = false, defaultValue = "1") int page,
@RequestParam(required = false, defaultValue = "20") int rows,
@PathVariable("Searchname") String shopname,   //获取传递的参数值
@PathVariable("Searchinstid") Integer institutionid,
HttpServletRequest request, Model model){
try {
shopname = URLDecoder.decode(shopname, "UTF-8");              //解码过程  避免中文乱码
} catch (Exception ex) {
}
if (shopname.equals("all")) {                 // 为空用all做中间转换
shopname = "";
}
PageHelper.startPage(page, rows);
List<BtyShop> list = btyshopService.SearchSearch(shopname,institutionid);       //调用Service传参查询
request.setAttribute("pageInfo", new PageInfo<BtyShop>(list));
List<Inst> insts = instService.getAll();
request.setAttribute("insts", insts);
request.setAttribute("shoplist", list);
// 回写查询数据
model.addAttribute("Searchshopname", shopname);
model.addAttribute("Searchinstitutionid", institutionid);
return "basic/Beautyshop";
}

---------------------------------------------------------------------------------------------------------------------------------------------------

Service部分  

public List<BtyShop> SearchSearch(String shopname, Integer institutionid) {            //传递参数查询
Map<String,Object> param = new HashMap();                                  
param.put("shopname", shopname);
param.put("institutionid", institutionid);          //往集合中添加键值对
List<BtyShop> BtyShop=btyShopMapper.SearchSearch(param);      //调用Mappper的方法查询,由于Mapper只能传一个参数,将多个参数转换为Map集合
return BtyShop;
}

-------------------------------------------------------------------------------------------------------------------------------------------------------

Mapper.java部分

List<BtyShop> SearchSearch(Map<String, Object> param);  //只有一个方法跳转执行sql语句

-------------------------------------------------------------------------------------------------------------------------------------------------------

Mapper.xml部分              //配置sql语句

<select id="SearchSearch" resultMap="BaseResultMap" parameterType="java.util.Map" >  //类型为map
select
<include refid="Base_Column_List" />
,(select b.institutionname from t_inst b where
s.institutionId=b.id)institutionname
from t_bty_shop s where status=1
<if test="institutionid!=null and institutionid!=''">         //判空即为不执行
<![CDATA[ AND institutionId =#{institutionid} ]]>
</if>
<if test="shopname!=null and shopname!=''">
<![CDATA[ AND shopName like CONCAT(CONCAT('%', #{shopname}),'%') ]]>        //springmvc变量模糊查询写法
</if>
</select>

当能力支撑不了野心时,就该静下心来学习!
原文地址:https://www.cnblogs.com/1234cjq/p/5743734.html