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. 修改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. 在自定义的QuestionExtMapper中编写代码
Integer countBySearch(QuestionQueryDTO questionQueryDTO);

    List<Question> selectBySearchWithRowbounds(QuestionQueryDTO questionQueryDTO);
 <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. 修改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. 修改question.html中分页的跳转
    首先需要获取到search的值,在indexController中
model.addAttribute("search",search);

分页: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">«span>
                        a>
                    li>
                    <li  th:if="${pagination.showPrevious}">
                        <a th:href="@{/(page=${pagination.page - 1},search=${search})}" aria-label="Previous">
                            <span aria-hidden="true"><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">>span>
                        a>
                    li>
                    <li th:if="${pagination.showEndPage}">
                        <a th:href="@{/(page=${pagination.totalPage},search=${search})}" aria-label="Next">
                            <span aria-hidden="true">»span>
                        a>
                    li>
                ul>
            nav>

你可能感兴趣的:(SpringBoot)