简体   繁体   中英

JPQL SQL make a query base on a list of arguments

I have a class Person as followed

public class Person{
   String name;
   //----------
}

I would like to query the list of persons where name are like "name1" and name like "name2", and name like "name3" .... so, i would like to have a funtion like this

public void queryPersonsWhithNameLike(List<String>  names){
   //here is my query
   String queryStr = "select p from Person p where p.name LIKE :names.get(0) AND p.name LIKE :names.get(1) AND p.name LIKE :names.ger(2) AND......";

}

Please how can i write a such query using sql and jpql?

public void queryPersonsWhithNameLike(List names){ Iterate over the List and construct a query:

String queryStr = "select p from Person p";
int count=0;
for(String str:names) {
   if(count==0) queryStr+="WHERE p.name LIKE :name"+count;
   else queryStr+=" AND p.name LIKE :name"+count;
   count++;
}
... Initialize query
for(String str:names) {
... Set all the :name+count as parameters
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM