How to implement search function in Java?

Keywords: xml Mybatis


First of all, we need to make it clear that the search box sends a Get request based on keywords, and sends a Get request to the current page

//The sample code request path is the current page path "/ product" 
<! -- search box get requests to search based on the keywords of the product name -- >
<form action="/product" class="form-inline pull-left" >
   < input type = "text" name = "product name" placeholder = "product name" class = "form control" value = "${param. Product name}" >
   <button class="btn btn-primary"><i class="fa fa-search"></i></button>

When we want to realize the multi criteria search function, we can encapsulate the search criteria into a Map set, and then search according to the Map set


Controller layer code:

    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<>();

        PageInfo<Kaola> pageInfo = kaolaService.findByPageNo(pageNo,searchParam);
        return "product/list";

Business layer code:

public PageInfo<Kaola> findByPageNo(Integer pageNo, Map<String, Object> searchParam) {
        List<Kaola> kaolaList = kaolaMapper.findBySearchParamWithType(searchParam);
        return new PageInfo<>(kaolaList);

mapper.xml in MyBatis:

<select id="findBySearchParamWithType" resultType="com.kaishengit.entity.Kaola">
            kaola.*, AS '',
            kaola_type.type_name AS 'kaolaType.typeName',
            parent_id AS 'kaolaType.parentId'
        INNER JOIN kaola_type ON kaola.type_id =
            <if test="productName != null and productName != ''">
                kaola.product_name LIKE concat('%',#{productName},'%')
            <if test="place != null and place != ''">
                and = #{place}
            <if test="typeId != null and typeId != ''">
                and kaola.type_id = #{typeId}
            <if test="minPrice !=null and minPrice != ''">
                <![CDATA[ and kaola.price >= #{minPrice} ]]>
            <if test="maxPrice !=null and maxPrice != ''">
                <![CDATA[ and kaola.price <= #{maxPrice} ]]>

In this way, the multi criteria search function can be realized from the front end to the back end. We will also encounter such a situation that when entering search criteria, the display list will constantly refresh automatically. Here, it is applied to Ajax related content. During the input process, AJAX requests will be sent out continuously, and then the page will be refreshed.

<input type="text" name="productName" placeholder="Commodity name" class="form-control" value="${param.productName}">

value="${param.productName}" is to obtain the value from the parameters of the request url, and realize the function of refreshing the page to display keywords after inputting keyword search, as shown in the figure above:


When inputting Chinese keywords for search, you can use encodeuriccomponent to solve the problem of displaying Chinese characters in url path:

    totalPages: ${pageInfo.pages},
    visiblePages: 10,
    first:'home page',
    prev:'previous page',
    next:'next page',
    + "&typeId=${param.typeId}&minPrice=${param.minPrice}&maxPrice=${param.maxPrice}&p={{number}}"
click to enlarge

search result

Posted by suthen_cowgirl on Sun, 03 May 2020 09:39:08 -0700