如何在同一列的 Sping JPA 中使用多个 LIKE '%keyword%'? [英] How to use multiple LIKE '%keyword%' in Sping JPA on same column?

查看:32
本文介绍了如何在同一列的 Sping JPA 中使用多个 LIKE '%keyword%'?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用

List<Application> findByProposalContainingIgnoreCase(String keyword);

如何使用关键字列表实现相同的效果?

How can I achieve the same using a list of keywords?

例如:

List<Application> findByProposalContainingIgnoreCase(List<String> keywords);

更新:

如果不能这样做,以下方法是否有效:

If it is not possible to do so, is the below way effective:

@Autowired
private ApplicationService applicationService;

List<Application> applications = new ArrayList<>();

for (String keyword : keywords) {
    applications.addAll(applicationService.findByProposalContainingIgnoreCase(keyword));
}

推荐答案

这是不可能的,因为普通 sql 也做不到.使用like"一词意味着搜索包含所提供关键字的所有可能性.在您的情况下,您必须将多个包含语句串在一起,例如

It's not possible as normal sql cannot do it either. The use of the word 'like' implies searching for all the possibilities that contain the keyword provided. In your case you'd have to string multiple contains statements together like

List<Application> findByProposalContainingIgnoreCaseOrProposalContainingIgnoreCase(String keyword1, String keyword2);

您最好为列表添加in关键字

You might be better of doing the inkeyword for your list

List<Application> findByProposalIn(Set<String> proposals);

问题出现了,但现在您必须将提案的大写和小写都添加到集合中,因为它可能不区分大小写,并且它将寻找不属于单词的完全匹配项.它并不理想,但我认为它适用于您正在尝试做的事情.

The problem arises though that now you would have to add both uppercase and lowercase of the proposal to the set as it might not be case sensitive, and it will look for the exact matches not part of the word. Its not ideal but I think it'll work for what you're trying to do.

更新

您通过添加 for 循环来回答就足以满足您的需要.

You're answer by adding the for loop would suffice for what you need.

这篇关于如何在同一列的 Sping JPA 中使用多个 LIKE '%keyword%'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆