我在XML文件中编写了以下查询并希望从java代码传递created_date的动态值,我正在创建spring批处理程序。那怎么办呢?
<property name="dataSource" ref="dataSource" />
<property name="sql"
value="SELECT `txn`.`id`,
`txn`.`sender_id`,
`txn`.`beneficiary_id`,
`txn`.`amount`,
`txn`.`pay_mode_master_id`,
`txn`.`status`,
`txn`.`reference_number`,
`txn`.`created_by`,
`txn`.`created_date`,
`txn`.`updated_by`,
`txn`.`updated_date`,
`txn`.`source_currency_master_id`,
`txn`.`dest_currency_master_id`,
`txn`.`fees`,
`txn`.`exchange_rate`,
`txn`.`total_amount`,
`txn`.`recipient_gets`,
`txn`.`from_country`,
`txn`.`to_country`,
`txn`.`other_purpose`,
`txn`.`transaction_purpose_id`,
`txn`.`sender_account_id`,
`txn`.`transaction_number`,
`txn`.`source_region`,
`txn`.`error`,
`txn`.`corridor_id`,
`txn`.`promo_code`,
`txn`.`receiver_id`,
`txn`.`error_code`
FROM `remittance`.`transaction_master` txn where txn.created_date >= '2017-02-09 00:00:00' AND txn.created_date <='2017-02-09 23:59:59';" />
<property name="rowMapper">
<bean class="com.websystique.springbatch.ExamResultRowMapper" />
</property>
</bean>
由于您在xml
文件中编写查询,因此我认为您可以解析并获取原始查询字符串。之后,您可以通过以下方式简单地从java代码传递动态值。
码:
List<Date> parameters = new ArrayList<>();
/* Here add all dates to the parameter ArrayList */
String query = "SELECT `txn`.`id`,
`txn`.`sender_id`,
`txn`.`beneficiary_id`,
`txn`.`amount`,
`txn`.`pay_mode_master_id`,
`txn`.`status`,
`txn`.`reference_number`,
`txn`.`created_by`,
`txn`.`created_date`,
`txn`.`updated_by`,
`txn`.`updated_date`,
`txn`.`source_currency_master_id`,
`txn`.`dest_currency_master_id`,
`txn`.`fees`,
`txn`.`exchange_rate`,
`txn`.`total_amount`,
`txn`.`recipient_gets`,
`txn`.`from_country`,
`txn`.`to_country`,
`txn`.`other_purpose`,
`txn`.`transaction_purpose_id`,
`txn`.`sender_account_id`,
`txn`.`transaction_number`,
`txn`.`source_region`,
`txn`.`error`,
`txn`.`corridor_id`,
`txn`.`promo_code`,
`txn`.`receiver_id`,
`txn`.`error_code`
FROM `remittance`.`transaction_master` txn where txn.created_date >= :created_date AND txn.created_date <=:created_date";
/* call addDynamicDateToQuery using the class object in which your
addDynamicDateToQuery method is present */
query = addDynamicDateToQuery(query ,"created_date",parameters);
public static String addDynamicDateToQuery(String query, String token,
Collection data) {
if(data.isEmpty() || !query.contains(":")){
query = null;
}else{
StringBuffer clause = new StringBuffer("");
for(Object value : data){
if(value instanceof Date){
clause.append(value + ",");
}
}
String clauseStr = clause.toString();
clauseStr = clauseStr.substring(0,clauseStr.lastIndexOf(","));
query = query.replace(":" + token, clauseStr);
}
return query;
}
/* Now You can call this query using JdbcTemplate object */
我得到了解决方案,以下是我需要在java文件中进行的代码更改,我将调用该工作。
JobParameters param = new JobParametersBuilder().addString("createdDate", "'%2017-02-24%'").toJobParameters();
JobExecution execution = jobLauncher.run(job, param);
&在xml文件中... FROM`remittance``transaction_master` txn其中txn.created_date与#{jobParameters ['createdDate']};“/>
并且您必须将scope =“step”放入bean标记中。否则会出错。
你可以把多个参数放在:
JobParameters param = new JobParametersBuilder().addString("targetPath", targetPath).addString("sourceFile", sourceFile).toJobParameters();