我需要使用命令行参数来更新sql查询。
我正在生成spring batch,它将读取sql查询,并且spring batch作业将读取查询,并将数据更新为JSON文件。
在batch.xml中的sql查询:
<resultMap id="fndEmpSeniorityRsltMap" >
<id column="EmpId" property="empID" />
<result column="EmpName" property="empName" />
</resultMap>
<select id="fndEmp" parameterType="java.lang.String" resultMap="fndEmpRsltMap">
select EmpId, EmpName from Emp
</select>
cmd行参数:
private Set<String> argumentValue;
--cmdLineArgument=<name>
itemreadwirtejob
@Bean
public ItemReader<Emp> btchItmRdr(SqlSessionFactory sqlSessionFactory) {
final MyBatisCursorItemReader<Emp> reader = new MyBatisCursorItemReader<>();
reader.setSqlSessionFactory(sqlSessionFactory);
reader.setParameterValues(cnfgRdr.getParamMap());
reader.setQueryId("fndEmp");
return reader;
}
@Bean
public ItemWriter<Emp> itmWrtrInCsv() throws DataAcesExcp {
try {
final FlatFileItemWriter<Emp> csvWriter = new FlatFileItemWriter<>();
csvWriter.setShouldDeleteIfExists(true);
csvWriter.setResource(new FileSystemResource(cnfgRdr.getOutLocCsv()));
csvWriter.setHeaderCallback(headerWriter -> headerWriter.append("EmpId").append(",")
.append("EMPName").append(",");
csvWriter.setLineAggregator(item -> new StringBuilder().append(item.getEmpID())
.append(",").append(item.getEmpName()).append(",")toString());
return csvWriter;
} catch (DataAccessException excp) {
throw new DataAcesExcp(excp);
}
}
@Bean
public ItemWriter<Emp> itmRdrInJson() throws DataAcesExcp {
try {
final FlatFileItemWriter<Emp> jsonWriter = new FlatFileItemWriter<>();
jsonWriter.setShouldDeleteIfExists(true);
jsonWriter.setResource(new FileSystemResource(cnfgRdr.getOutLocTxt()));
jsonWriter.setLineAggregator(new JsonLineAggregator<Emp>());
jsonWriter.setFooterCallback(new JsonFlatFileFooterCallback());
return jsonWriter;
} catch (DataAccessException excp) {
throw new DataAcesExcp(excp);
}
}
@Bean
public Step rdItmAndWrtInCsv() throws DataAcesExcp {
return stepBuilderFactory.get("rdItmAndWrtInCsv").<Emp, Emp> chunk(cnfgRdr.getChunkSize())
.reader(btchItmRdr(sqlSessionFactory)).faultTolerant().retryLimit(cnfgRdr.getRetryLimit())
.retry(DeadlockLoserDataAccessException.class).writer(itmWrtrInCsv()).build();
}
@Bean
public Step rdItmAndWrtInJson() throws DataAcesExcp {
return stepBuilderFactory.get("rdItmAndWrtInJson").<Emp, Emp> chunk(cnfgRdr.getChunkSize())
.reader(btchItmRdr(sqlSessionFactory)).faultTolerant().retryLimit(cnfgRdr.getRetryLimit())
.retry(DeadlockLoserDataAccessException.class).writer(itmRdrInJson()).build();
}
@Bean
public Job rdItmAndWrtJob(BtchJobCmtnNotfLstnr lstnr,JobBuilderFactory jobBuilderFactory) throws DataAcesExcp {
if(argumentValue.get(0).equalsIgnoreCase("read"))
{
return jobBuilderFactory.get("rdItmAndWrtJob").incrementer(new RunIdIncrementer())
.listener(lstnr).start(rdItmAndWrtInCsv()).next(rdItmAndWrtInJson()).build();
}
}
在上面的代码中,如果我将read作为命令行参数传递,它将读取sql查询并使用数据更新csv和josn文件。
我现在需要执行以下操作:
Top-默认为前500条记录,
Emp-适用于特定员工。
如果我将Top作为cmd行arg传递,则春季批将需要更新查询并显示表中的前500条记录。
与emp相同,如果我将empid = 500作为cmd行args传递,则查询必须显示empid = 500的记录。
请告诉我是否有解决方案。预先感谢