Spring Data: can it find by two values of the same

2020-07-07 11:40发布

问题:

I am doing a Spring web app and I use Spring Data.

I am able to use Spring Data to find objects by a single value of a field. For example:

some_object_repository.findByFirstName("John") 

Is there any way I can provide two first names (e.g., "John", "David") similar to the following in concept:

some_object_repository.findByFirstName({"John", "David"})

without me writing a custom implementation?

Regards and thanks!

回答1:

You can do this with In at the end

findByAgeIn(Collection ages) … where x.age in ?1

http://docs.spring.io/spring-data/jpa/docs/1.6.0.RELEASE/reference/html/jpa.repositories.html#jpa.query-methods

Section 2.3.2 Query creation

In your case it will be

findByFirstNameIn(Collection names)