Java實現(xiàn)搜索功能代碼詳解
首先,我們要清楚搜索框中根據(jù)關(guān)鍵字進行條件搜索發(fā)送的是Get請求,并且是向當(dāng)前頁面發(fā)送Get請求
//示例代碼 請求路徑為當(dāng)前頁面路徑 "/product" <!-- 搜索框 get請求 根據(jù)商品名稱的關(guān)鍵字進行搜索--> <form action="/product" class="form-inline pull-left" > <input type="text" name="productName" placeholder="商品名稱" class="form-control" value="${param.productName}"> <button class="btn btn-primary"><i class="fa fa-search"></i></button> </form>
當(dāng)我們要實現(xiàn)多條件搜索功能時,可以將搜索條件封裝為一個Map集合,再根據(jù)Map集合進行搜索
Controller層代碼:
@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"; }
業(yè)務(wù)層代碼:
public PageInfo<Kaola> findByPageNo(Integer pageNo, Map<String, Object> searchParam) { PageHelper.startPage(pageNo,10); List<Kaola> kaolaList = kaolaMapper.findBySearchParamWithType(searchParam); return new PageInfo<>(kaolaList); }
MyBatis中的mapper.xml:
<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>
這樣,就可以從前端到后端實現(xiàn)多條件搜索功能了。我們還會遇到這樣一種情況,在輸入搜索條件時,顯示列表會不斷自動刷新,這里其實用到了Ajax的相關(guān)內(nèi)容,在輸入的過程中,會不斷發(fā)出Ajax請求,然后刷新頁面。
<input type="text" name="productName" placeholder="商品名稱" class="form-control" value="${param.productName}">
是從請求url的參數(shù)中獲取值,實現(xiàn)在輸入關(guān)鍵字搜索后刷新頁面顯示關(guān)鍵字這一功能,直接上圖:
value="${param.productName}"
在輸入中文關(guān)鍵字進行搜索時,可以使用encodeURIComponent解決url路徑顯示中文亂碼問題:
//分頁 $('#pagination-demo').twbsPagination({ totalPages: ${pageInfo.pages}, visiblePages: 10, first:'首頁', last:'末頁', prev:'上一頁', next:'下一頁', href:"?productName="+encodeURIComponent('${param.productName}')+"&place="+encodeURIComponent('${param.place}') + "&typeId=${param.typeId}&minPrice=${param.minPrice}&maxPrice=${param.maxPrice}&p={{number}}" });
點擊查看大圖
搜索結(jié)果
總結(jié)
以上所述是小編給大家介紹的Java實現(xiàn)搜索功能代碼詳解,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
Spring Boot Admin 動態(tài)修改日志級別的方法步驟
這篇文章主要介紹了Spring Boot Admin 動態(tài)修改日志級別的方法步驟,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08springcloud nacos的賦值均衡和動態(tài)刷新
nacos是一個分布式的配置中心和注冊發(fā)現(xiàn)中心,這篇文章主要介紹了springcloud nacos的賦值均衡和動態(tài)刷新,需要的朋友可以參考下2024-05-05Java基于外觀模式實現(xiàn)美食天下食譜功能實例詳解
這篇文章主要介紹了Java基于外觀模式實現(xiàn)美食天下食譜功能,較為詳細的講述了外觀模式的概念、原理并結(jié)合實例形似詳細分析了Java基于外觀模式實現(xiàn)美食天下食譜功能的具體操作步驟與相關(guān)注意事項,需要的朋友可以參考下2018-05-05Java?C++題解leetcode769最多能完成排序的塊
這篇文章主要為大家介紹了Java?C++題解leetcode769最多能完成排序的塊示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-10-10