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">&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>

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值