Java实现搜索功能代码详解
⾸先,我们要清楚搜索框中根据关键字进⾏条件搜索发送的是Get请求,并且是向当前页⾯发送Get请求
//⽰例代码请求路径为当前页⾯路径 "/product"
<!-- 搜索框 get请求根据商品名称的关键字进⾏搜索-->
<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>
当我们要实现多条件搜索功能时,可以将搜索条件封装为⼀个Map集合,再根据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";
}
业务层代码:
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中的l:
<select id="findBySearchParamWithType" resultType="ity.Kaola">
SELECT
kaola.*, kaola_type.id AS 'kaolaType.id',
pe_name AS 'peName',
parent_id AS 'kaolaType.parentId'
FROM
kaola
INNER JOIN kaola_type pe_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 != ''">
pe_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>
这样,就可以从前端到后端实现多条件搜索功能了。我们还会遇到这样⼀种情况,在输⼊搜索条件时,显⽰列表会不断⾃动刷
springboot其实就是spring
新,这⾥其实⽤到了Ajax的相关内容,在输⼊的过程中,会不断发出Ajax请求,然后刷新页⾯。
<input type="text" name="productName" placeholder="商品名称" class="form-control" value="${param.productName}">
value="${param.productName}"是从请求url的参数中获取值,实现在输⼊关键字搜索后刷新页⾯显⽰关键字这⼀功能,直接上图:在输⼊中⽂关键字进⾏搜索时,可以使⽤encodeURIComponent解决url路径显⽰中⽂乱码问题:
//分页
$('#pagination-demo').twbsPagination({
totalPages: ${pageInfo.pages},
visiblePages: 10,
first:'⾸页',
last:'末页',
prev:'上⼀页',
next:'下⼀页',
href:"?productName="+encodeURIComponent('${param.productName}')+"&place="+encodeURIComp
onent('${param.place}')
+ "&typeId=${peId}&minPrice=${param.minPrice}&maxPrice=${param.maxPrice}&p={{number}}"
});
点击查看⼤图
搜索结果
总结
以上所述是⼩编给⼤家介绍的Java实现搜索功能代码详解,希望对⼤家有所帮助,如果⼤家有任何疑问请给我留⾔,⼩编会及时回复⼤家的。在此也⾮常感谢⼤家对⽹站的⽀持!

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。