当前位置:   article > 正文

SpringBoot-实现搜索功能_springboot实现搜索功能

springboot实现搜索功能
  1. 构造一个对象QuestionQueryDTO
package com.july.community.dto;

import lombok.Data;

@Data
public class QuestionQueryDTO {
    private String search;
    private Integer page;
    private Integer size;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  1. 修改service
QuestionQueryDTO questionQueryDTO = new QuestionQueryDTO();
        questionQueryDTO.setSearch(search);
        Integer totalCount = questionExtMapper.countBySearch(questionQueryDTO);
...
questionQueryDTO.setSize(size);
        questionQueryDTO.setPage(offset);
        List<Question> questions = questionExtMapper.selectBySearchWithRowbounds(questionQueryDTO);//该list存从数据库中查到的question对象
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  1. 在自定义的QuestionExtMapper中编写代码
Integer countBySearch(QuestionQueryDTO questionQueryDTO);

    List<Question> selectBySearchWithRowbounds(QuestionQueryDTO questionQueryDTO);
  • 1
  • 2
  • 3
 <select id="countBySearch" parameterType="com.july.community.dto.QuestionQueryDTO" resultType="java.lang.Integer">
        select count(*) from tbl_question
         <where>
             <if test="search != null">
                 and title regexp #{search}
             </if>
         </where>
    </select>

    <select id="selectBySearchWithRowbounds" parameterType="com.july.community.dto.QuestionQueryDTO" resultMap="BaseResultMap">
        select * from tbl_question
        <where>
            <if test="search != null">
                and title regexp #{search}
            </if>
        </where>
        order by time_modified desc
        limit #{offset},#{size}
    </select>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  1. 修改navigation.html
<form class="navbar-form navbar-left" name="search" action="/" method="get">
                    <div class="form-group">
                        <input type="text" class="form-control" placeholder="搜索问题">
                    </div>
                    <button type="submit" class="btn btn-default">搜索</button>
                </form>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  1. 修改question.html中分页的跳转
    首先需要获取到search的值,在indexController中
model.addAttribute("search",search);
  • 1

分页:index.html

<!--分页标签-->
            <nav aria-label="Page navigation">
                <ul class="pagination">
                    <li th:if="${pagination.showFirstPage}">
                        <a href="@{/(page=1,search=${search})}" aria-label="Previous">
                            <span aria-hidden="true">&laquo;</span>
                        </a>
                    </li>
                    <li  th:if="${pagination.showPrevious}">
                        <a th:href="@{/(page=${pagination.page - 1},search=${search})}" aria-label="Previous">
                            <span aria-hidden="true">&lt;</span>
                        </a>
                    </li>
                    <li th:each="page : ${pagination.pages}" th:class="${pagination.page == page} ? 'active' : ''">
                        <a th:href="@{/(page=${page},search=${search})}" th:text="${page}" ></a>
                    </li>
                    <li  th:if="${pagination.showNext}">
                        <a th:href="@{/(page=${pagination.page + 1},search=${search})}" aria-label="Next">
                            <span aria-hidden="true">&gt;</span>
                        </a>
                    </li>
                    <li th:if="${pagination.showEndPage}">
                        <a th:href="@{/(page=${pagination.totalPage},search=${search})}" aria-label="Next">
                            <span aria-hidden="true">&raquo;</span>
                        </a>
                    </li>
                </ul>
            </nav>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/355603
推荐阅读
相关标签
  

闽ICP备14008679号