How to use multiple LIKE '%keyword%' in Sping JPA on same column?

江枫思渺然 提交于 2021-01-28 03:59:00

问题


I am able to search for a keyword in a column using

List<Application> findByProposalContainingIgnoreCase(String keyword);

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

For example:

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

UPDATE:

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));
}

回答1:


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);

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.

UPDATE

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



来源:https://stackoverflow.com/questions/52497673/how-to-use-multiple-like-keyword-in-sping-jpa-on-same-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!