Reading multiple rows for DB2 using JDBC named templates but without "IN" clause in SQL

351 views Asked by At

I would like to fetch serveral rows of data from a table.

I have a dao, sql and some parameters.

    public static final String SELECT = ""
    + "SELECT NAME, SURNAME, AGE, LEVEL"
    + "FROM MYTABLE "
    + "WHERE "

    public static final String CONDITION = ""
    + "SURNAME = :" + PARAM_SURNAME
    + "AND LEVEL = :" + PARAM_LEVEL;

For retrieval of a signle object I would do something like that:

         public MyObject getMyThing(
                final String surname,
                final Integer level) {
            final MapSqlParameterSource parameters = new MapSqlParameterSource()
                    .addValue(PARAM_SURNAME, surname)
                    .addValue(PARAM_LEVEL, level);
            final String query = SELECT + CONDITION;
            return myNamedTemplate.query(query, parameters, new MyObjectRowMapper());
    }

And it is be just fine. However I would also like to use one SQL to retrieve several objects from databse. I know about the "IN" clause but it is not good enough as its usage will affect performance in the bad way. What I would want is something that results in a following query:

    SELECT STUFF FROM TABLE WHERE
    //and the part I am interested in:
    (SURNAME = :PARAM_SURNAME
    AND LEVEL = :PARAM_LEVEL)
    OR
    (SURNAME = :PARAM_SURNAME
    AND LEVEL = :PARAM_LEVEL)
    OR
    (SURNAME = :PARAM_SURNAME
    AND LEVEL = :PARAM_LEVEL)
    //and so on...

So the question: How can I achieve this using JDBC Named Template in Spring? I cannot figure out how to do parameter mapping. Desired method in DAO could be something like that:

       public List<MyObject> getMyThings(
                final List<String> surnames,
                final List<Integer> levels) {
            final MapSqlParameterSource parameters = // magic
            final String query = // more magic
            return myNamedTemplate.query(query, parameters, new MyObjectRowMapper());
    }
0

There are 0 answers