-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using Spring Batch JdbcCursorItemReader with NamedParameters [BATCH-2521] #1081
Comments
Mahmoud Ben Hassine commented Hi Wellington Baltazar de Souza
There are no attached files to this ticket. That said, after reading the SO thread you linked here, there is no need for a new feature in Spring Batch to support named parameters with the @Bean
public JdbcCursorItemReader<Person> itemReader() {
String sql = "select * from person where id = :id and name = :name";
Map<String, Object> namedParameters = new HashMap<String, Object>() {{
put("id", 1);
put("name", "foo");
}};
return new JdbcCursorItemReaderBuilder<Person>()
.name("personItemReader")
.dataSource(dataSource())
.rowMapper(rowMapper())
.sql(NamedParameterUtils.substituteNamedParameters(sql, new MapSqlParameterSource(namedParameters)))
.preparedStatementSetter(new ListPreparedStatementSetter(Arrays.asList(NamedParameterUtils.buildValueArray(sql, namedParameters))))
.build();
} The @ Wellington Baltazar de Souza What do you think? |
The solution proposed in the linked SO thread suggests to
This is a bit heavy compared to the following two lines used to transform named parameters to jdbc placeholders: String preparedSql = NamedParameterUtils.substituteNamedParameters(sql, new MapSqlParameterSource(namedParameters));
PreparedStatementSetter preparedStatementSetter = new ArgumentPreparedStatementSetter(NamedParameterUtils.buildValueArray(sql, namedParameters)); So as mentioned in my previous comment, I see no need to introduce a new concept to support this feature (trying to avoid API bloat) thanks to the A complete example can be found in this repo (which also contains an example of how to use a list as a named parameter). |
@bean
} Problem is that its not taking map values of "21 , 20" its failing. Can you please help me on this. |
@logie2882 you are putting multiple values as a String here: - map.put("name", "21,22");
+ map.put("name", Arrays.asList("21,22".split(",")));
Sure, splitting values as shown above should help (I tested this on the same example). If you need further support, please use SO (https://github.com/spring-projects/spring-batch#getting-help). Thank you. |
Tried the above said solution , When i pass a single string its working fine. When passing multiple string value i am getting exception java.sql.SqlException:Invalid column index. The reason for failing is only two IN params but the argumentPreparedStatementSetter is creating 3 param values the query formed at runtime is I am using spring core jar 4.1.1 RELEASE. |
As mentioned previously, if you need further support, please use SO.
|
Wellington Baltazar de Souza opened BATCH-2521 and commented
Find attached the source files adding this feature.
Affects: 3.0.7
Reference URL: http://stackoverflow.com/questions/23222353/using-spring-batch-jdbccursoritemreader-with-namedparameters/38644424#38644424
1 votes, 1 watchers
The text was updated successfully, but these errors were encountered: