1

I have this query in spring boot, in one of the dao class.

QUERY

String sql = select * from language where screen = ? and lang = ? and screenid in (?)

REQUEST OBJECT

{
    "language" : {
         "screen" : "Index",
         "lang": "english",
         "screenid" : ["250","10","44","11"]
    }
}

I fetch each of the values, that came from JSON request and store it into bean.

QUESTION

The screenid has array of string values, How can I pass that in query ?

I tried,

String ids = LangObj.get("screenid").toString();
// this gives the value as ["250","10","44","11"]

when passed this value directly to query it forms as select * from language where screen = 'Index' and lang = 'english' and screenid in (["250","10","44","11"]) and thus not giving any output, I am looking for a query that to be formed like the below,

select * from language where screen = 'Index' and lang = 'english' and screenid in ('250','10','44','11')

Any suggestions ?

1 Answers1

0

Check this documentation please :

https://www.baeldung.com/spring-jdbctemplate-in-list

Example code :

List<Employee> getEmployeesFromIdList(List<Integer> ids) {
    String inSql = String.join(",", Collections.nCopies(ids.size(), "?"));
 
    List<Employee> employees = jdbcTemplate.query(
      String.format("SELECT * FROM EMPLOYEE WHERE id IN (%s)", inSql), 
      ids.toArray(), 
      (rs, rowNum) -> new Employee(rs.getInt("id"), rs.getString("first_name"), 
        rs.getString("last_name")));

    return employees;
}