英文:
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("search")
public ModelAndView SearchBeers(@RequestParam("searchTerm") String searchTerm) {
return new ModelAndView("/Searchbeers", "beerList", service.Search(searchTerm));
}
service method called is
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;
}
and the SQL statement used is
@NamedQuery(name = "Beers.findByLikeName", query = "SELECT b FROM Beers b WHERE b.name LIKE CONCAT('%',:name,'%')")
the JSP for when displaying the list is
<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>
答案1
得分: 1
你有很多方法可以解决这个问题,但是问题是,当字符串为空时,你的查询是:
SELECT b from Beers b Where b.name LIKE '%%'
因此,当有一个空字符串时,你需要做另一个标记,因为LIKE '%%'
会找到所有的名称。
附注:不推荐返回空值,最好是返回一个空列表。
英文:
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 '%%'
So, you need do antoher flag, when have an empty String, because ... LIKE '%%'
find every name.
PD: Not a good practice return a null, is better a empty list.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论