Spring搜索功能在未输入任何搜索内容时返回完整列表。

huangapple go评论77阅读模式
英文:

Spring search function returning full list when nothing is searched

问题

在我的Spring应用程序中,我已经添加了一个搜索功能,用户可以在数据库中搜索姓名。它能够正常工作,除了当我搜索名称或类似名称时,但是当我不输入任何内容进行搜索时,它应该返回空,但实际上返回了完整的姓名列表。

我在逻辑中设置了如果列表为空则返回null,那么我的逻辑中有什么问题?

从测试和使用"findbyname" SQL查询来看,我认为问题在于我的SQL语句。

控制器方法如下:

@RequestMapping("search")
public ModelAndView SearchBeers(@RequestParam("searchTerm") String searchTerm) {
    return new ModelAndView("/Searchbeers", "beerList", service.Search(searchTerm));
}

服务方法如下:

public List<Beers> Search(String searchTerm) {
    EntityManager em = DBUtil.getEMF().createEntityManager();
    List<Beers> list = null;

    try {
        list = em.createNamedQuery("Beers.findByLikeName", Beers.class)
                .setParameter("name", searchTerm)
                .getResultList();
        if (list == null || list.isEmpty()) {
            list = null;
        }
    } finally {
        em.close();
    }
    return list;
}

使用的SQL语句如下:

@NamedQuery(name = "Beers.findByLikeName", query = "SELECT b FROM Beers b WHERE b.name LIKE CONCAT('%', :name, '%')")

用于显示列表的JSP如下:

<c:forEach items="${beerList}" var="beer">
    <tr>
        <td>${beer.id}</td>
        <td>${beer.name}</td>
        <td>${beer.abv}</td>
        <td>
            <form action="/Assignment3/beer/viewBeer">
                <input type="hidden" name="id" value="${beer.id}"/>
                <input type="submit" value="<spring:message code="label.viewDetails"/>"/>
            </form>
        </td>
    </tr>
</c:forEach>
英文:

In my spring application, I have added a search function where the user can search the database for a name, it works except when I'm searching for names or similar names, but for when the I enter nothing and search, it should return nothing but returns the full list of names.

I set the list to return null if it is null, what is the flaw in my logic?

from testing and using the findbyname SQL I think it is a flaw in my SQL state

the controller method used is

@RequestMapping(&quot;search&quot;)
public ModelAndView SearchBeers(@RequestParam(&quot;searchTerm&quot;) String searchTerm) {

    return new ModelAndView(&quot;/Searchbeers&quot;, &quot;beerList&quot;, service.Search(searchTerm));

}

service method called is

 public List&lt;Beers&gt; Search(String searchTerm) {
    EntityManager em = DBUtil.getEMF().createEntityManager();
    List&lt;Beers&gt; list = null;

    try {
        list = em.createNamedQuery(&quot;Beers.findByLikeName&quot;, Beers.class)
                .setParameter(&quot;name&quot;, searchTerm)
                .getResultList();
        if (list == null || list.isEmpty()) {
            list = null;
        }

    } finally {
        em.close();
    }
    return list;
}

and the SQL statement used is

@NamedQuery(name = &quot;Beers.findByLikeName&quot;, query = &quot;SELECT b FROM Beers b WHERE b.name LIKE CONCAT(&#39;%&#39;,:name,&#39;%&#39;)&quot;)

the JSP for when displaying the list is

   &lt;c:forEach items=&quot;${beerList}&quot; var=&quot;beer&quot;&gt; 
            &lt;tr&gt;
                &lt;td&gt;${beer.id}&lt;/td&gt;
                &lt;td&gt;${beer.name}&lt;/td&gt;
                &lt;td&gt;${beer.abv}&lt;/td&gt;
         
                
                &lt;td&gt;
                    &lt;form action=&quot;/Assignment3/beer/viewBeer&quot;&gt;
                        &lt;input type=&quot;hidden&quot; name=&quot;id&quot; value=&quot;${beer.id}&quot;/&gt;
                        &lt;input type=&quot;submit&quot; value=&quot;&lt;spring:message code=&quot;label.viewDetails&quot;/&gt;&quot;/&gt;
                    &lt;/form&gt;
                &lt;/td&gt;
                
            &lt;/tr&gt;
        &lt;/c:forEach&gt;

答案1

得分: 1

你有很多方法可以解决这个问题,但是问题是,当字符串为空时,你的查询是:

SELECT b from Beers b Where b.name LIKE &#39;%%&#39;

因此,当有一个空字符串时,你需要做另一个标记,因为LIKE &#39;%%&#39;会找到所有的名称。

附注:不推荐返回空值,最好是返回一个空列表。

英文:

You have many ways to solve that, but the problem is, when the String is empty your query is:

SELECT b from Beers b Where b.name LIKE &#39;%%&#39;

So, you need do antoher flag, when have an empty String, because ... LIKE &#39;%%&#39; find every name.

PD: Not a good practice return a null, is better a empty list.

huangapple
  • 本文由 发表于 2020年3月16日 22:59:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/60708294.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定