Java实现搜索功能代码详解

所属分类: 软件编程 / java 阅读数: 14
收藏 0 赞 0 分享

首先,我们要清楚搜索框中根据关键字进行条件搜索发送的是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中的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>

这样,就可以从前端到后端实现多条件搜索功能了。我们还会遇到这样一种情况,在输入搜索条件时,显示列表会不断自动刷新,这里其实用到了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="+encodeURIComponent('${param.place}')
  + "&typeId=${param.typeId}&minPrice=${param.minPrice}&maxPrice=${param.maxPrice}&p={{number}}"
});

点击查看大图

搜索结果

以上所述是小编给大家介绍的Java实现搜索功能代码详解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

更多精彩内容其他人还在看

Java数据类型的规则

这篇文章主要介绍了Java数据类型的规则的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Spring整合TimerTask实现定时任务调度

这篇文章主要介绍了Spring整合TimerTask实现定时任务调度的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

详解SpringMVC使用MultipartFile实现文件的上传

本篇文章主要介绍了SpringMVC使用MultipartFile实现文件的上传,本地的文件上传到资源服务器上,比较好的办法就是通过ftp上传。这里是结合SpringMVC+ftp的形式上传的,有兴趣的可以了解一下。
收藏 0 赞 0 分享

SpringMVC上传文件的三种实现方式

本篇文章主要介绍了SpringMVC上传文件的三种实现方式,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

微信公众帐号开发-自定义菜单的创建及菜单事件响应的实例

本篇文章主要介绍了微信公众帐号开发-自定义菜单的创建及菜单事件响应的实例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
收藏 0 赞 0 分享

浅析Java中的继承与组合

本文将介绍组合和继承的概念及区别,并从多方面分析在写代码时如何进行选择。文中通过示例代码介绍的很详细,有需要的朋友可以参考借鉴,下面来一起看看吧。
收藏 0 赞 0 分享

利用反射获取Java类中的静态变量名及变量值的简单实例

下面小编就为大家带来一篇利用反射获取Java类中的静态变量名及变量值的简单实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

java启动线程的3种方式对比分析

这篇文章主要为大家对比分析了java启动线程的3种方式,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

SpringMVC上传和解析Excel方法

这篇文章主要介绍了SpringMVC上传和解析Excel方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

JAVA中String类与StringBuffer类的区别

这篇文章主要为大家详细介绍了JAVA中String类与StringBuffer类的区别,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多