First of all, we need to be clear that the search box is a Get request based on the keyword search, and the Get request is sent to the current page.
//The request path for the sample code is the current page path "/product" <!-- Search box get request to search based on the keyword of the product name--><form action="/product" > <input type="text" name="productName" placeholder="product name" value="${param.productName}"> <button><i></i></button></form>When we want to implement the multi-condition search function, we can encapsulate the search conditions into a Map collection and search based on the Map collection
Controller layer code:
@GetMapping("/product") public String list(@RequestParam(required = false,defaultValue = "1",name = "p")Integer pageNo, @RequestParam(required = false,defaultValue = "")String productName, @RequestParam(required = false,defaultValue = "")String place, @RequestParam(required = false,defaultValue = "")Integer typeId, @RequestParam(required = false,defaultValue = "")BigDecimal minPrice, @RequestParam(required = false,defaultValue = "")BigDecimal maxPrice, Model model) { Map<String,Object> searchParam = new HashMap<>(); searchParam.put("productName",productName); searchParam.put("place",place); searchParam.put("typeId",typeId); searchParam.put("minPrice",minPrice); searchParam.put("maxPrice",maxPrice); PageInfo<Kaola> pageInfo = kaolaService.findByPageNo(pageNo,searchParam); model.addAttribute("pageInfo",pageInfo); return "product/list"; }Business layer code:
public PageInfo<Kaola> findByPageNo(Integer pageNo, Map<String, Object> searchParam) { PageHelper.startPage(pageNo,10); List<Kaola> kaolaList = kaolaMapper.findBySearchParamWithType(searchParam); return new PageInfo<>(kaolaList);}mappper.xml in MyBatis:
<select id="findBySearchParamWithType" resultType="com.kaishengit.entity.Kaola"> SELECT kaola.*, kaola_type.id AS 'kaolaType.id', kaola_type.type_name AS 'kaolaType.typeName', parent_id AS 'kaolaType.parentId' FROM kaola INNER JOIN kaola_type ON kaola.type_id = kaola_type.id <where> <if test="productName != null and productName != ''"> kaola.product_name LIKE concat('%',#{productName},'%') </if> <if test="place != null and place != ''"> and kaola.place = #{place} </if> <if test="typeId != null and typeId != ''"> and kaola.type_id = #{typeId} </if> <if test="minPrice != null and minPrice != ''"> <![CDATA[ and kaola.price >= #{minPrice} ]]> </if> <if test="maxPrice !=null and maxPrice != ''"> <![CDATA[ and kaola.price <= #{maxPrice} ]]> </if> </where> ORDER BY kaola.id DESC</select>In this way, you can implement multi-condition search function from the front-end to the back-end. We will also encounter a situation where when entering search conditions, the display list will be automatically refreshed. This actually uses Ajax related content. During the input process, Ajax requests will be continuously issued and the page will be refreshed.
<input type="text" name="productName" placeholder="商品名称" value="${param.productName}"> is to get the value from the parameters of the request url, and realize the function of refreshing the page to display keywords after entering keyword search. The picture is directly above:
value="${param.productName}"
When entering Chinese keywords for searching, you can use encodeURIComponent to solve the problem of displaying Chinese garbled code in the url path:
//Pagination$('#pagination-demo').twbsPagination({ totalPages: ${pageInfo.pages}, visiblePages: 10, first:'Home', last:'Last:'Last page', prev:'Previous page', next:'Next page', href:"?productName="+encodeURIComponent('${param.productName}')+"&place="+encodeURIComponent('${param.place}') + "&typeId=${param.typeId}&minPrice=${param.minPrice}&maxPrice=${param.maxPrice}&p={{number}}"});Click to view the larger image
Search results
Summarize
The above is a detailed explanation of the Java implementation search function code introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support to Wulin.com website!