Question

Does Spring have any features which allow for a batch select? I basically have n number of selects to execute depending on the number of records in a list. At the moment because the size of the list is always changing i have to dynamically build the SQL which will be executed. The end product looks something like this

select * from record_details t WHERE id IN ((?),(?),(?))

However the code to generate this SQL on the fly is messy and I'm wondering if there is a nicer approach for this type of problem?

Was it helpful?

Solution

The NamedParameterJdbcTemplate (and according support class) does have that support.

public void someRepoMethod(List ids) {
    String query = "select * from record_details where id in (:ids)";
    getNamedParameterJdbcTemplate().query(query, Collections.singletonMap("ids", ids), new YourRowMapper());
}

OTHER TIPS

If you don't want to generate SQL yourself you have to use some existing framework. From what i know myBatis, is more lightweight than hibernate so it can suit you more, but there may be other more suitable.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top