基於SSH框架專案使用模糊查詢的搜尋功能開發

Tiesto_W發表於2019-04-11

1、jsp端建立搜尋框

<form role="form" action="/websearch/search" method="post"
      id="lucenesearchFormId"
      class="navbar-form navbar-left hidden-xs hidden-sm" role="search">
    <div class="form-group">
        <input type="text" name="keyword" id="keyword"
               class="form-control input-sm" placeholder="查詢公開維修案例"> <input
            type="hidden" id="pageNumId" name="pagenum">
    </div>
    <button type="submit" class="btn btn-default btn-sm">檢索</button>
</form>

2、controller端程式碼

@RequestMapping(value = "/search", method = RequestMethod.POST)
    public String Search(String keyword,Model model) {
        System.out.println(keyword);
        List<Document> documentList = documentService.getDocumentByLikeTitle(keyword);
//        Document document=documentService.getDocumentById(doc_ID);
        model.addAttribute("document", documentList);

        return "/comment/search";
    }

3、service層建立getDocumentByLikeTitle()方法

List<Document> getDocumentByLikeTitle(String keyword);

4、dao事務管理層制定hql資料庫查詢操作,這裡使用模糊查詢方式(有待提升查詢效率)

public List<Document> getDocumentByLikeTitle(String keyword){
    String hql="from Document d where d.title like ? or d.doc_Abstract like ?";
    Query query=currentSession().createQuery(hql);
    query.setString(0,"%"+keyword+"%");
    query.setString(1,"%"+keyword+"%");
    return (List<Document>) query.list();
}

5、前端顯示查詢結果

<c:forEach items="${document}" var="d">
    <tr>
        <td>${d.doc_ID}</td>
        <td>${d.title}</td>
        <td>${d.doc_Abstract}</td>
        <td>
            <c:if test="${d.doc_CarType=='00'}">未選擇車型</c:if>
            <c:if test="${d.doc_CarType=='0'}">HL10000</c:if>
            <c:if test="${d.doc_CarType=='1'}">HL23000</c:if>
            <c:if test="${d.doc_CarType=='2'}">HL32110</c:if>
        </td>
        <td>
            <a href="<c:url value="/know/edit" />?doc_ID=${d.doc_ID}">顯示</a>
            <a href="<c:url value="/know/editknow" />?doc_ID=${d.doc_ID}">編輯</a>
            <a href="<c:url value="/know/deleteDocumentById" />?doc_ID=${d.doc_ID}" οnclick="return confirm('你確定要刪除嗎?')">刪除</a>

        </td>
    </tr>
</c:forEach>

 

相關文章