I am using the Spring Boot and Batch annotation
based approached. I am reading tables data using the JdbcCursorItemReader
and writing it into CSV/XML/other
tables etc based on the need.
As a spring batch best practice just wanted to know the view the way I have created the SQL query inside the JdbcCursorItemReader
method. Is there any way if we can avoid concatenation and follow the best way around this like we do in XML based approached ?
Please let me know if the following way of writing SQL query is the best way ?
Annotation based approached.
@Bean(destroyMethod="")
public JdbcCursorItemReader<Orders> employeesReader(){
JdbcCursorItemReader<Orders> itemReader = new JdbcCursorItemReader<>();
itemReader.setDataSource(dataSource);
itemReader.setSql("SELECT orderNumber, productName, msrp, priceEach "
+ "FROM products p "
+ "INNER JOIN orderdetails o "
+ "ON p.productcode = o.productcode "
+ "AND p.msrp > o.priceEach "
+ "WHERE p.productcode = ? ");
itemReader.setRowMapper(new OrdersRowMapper());
itemReader.setIgnoreWarnings(true);
return itemReader;
}
The same XML can be done using XML based approached without concatenation operators
<bean id="ordersItemReader" class="org.springframework.batch.item.database.JdbcCursorItemReader" scope="step" >
<property name="dataSource" ref="dataSource" />
<property name="sql">
<value>
SELECT orderNumber, productName, msrp, priceEach
FROM products p INNER JOIN orderdetails o ON p.productcode = o.productcode
AND p.msrp > o.priceEach
WHERE p.productcode = '#{stepExecutionContext[productcode]}';
</value>
</property>
<property name="rowMapper">
<bean class="com.XXXX.mapper.OrdersRowMapper" scope="step" />
</property>
</bean>
In my project, we're using annotation based approached and need the guidance on this.