Mybatis Spring 批处理:如何在Tasklet 中使用where 子句从多个表中删除?
Mybatis Spring Batch : how to delete from mutliple tables with where clause in Tasklet?
我想像这样从不同的表中进行多次删除:
DELETE FROM TABLE1 WHERE T1_ID = :id AND T1_CREATION_DATE IS NULL;
DELETE FROM TABLE2 WHERE T2_ID = :id AND T2_CREATION_DATE IS NULL;
COMMIT;
但有 spring 个批次。
到目前为止我试过了:
String query = "DELETE FROM TABLE1 WHERE T1_ID = :id AND T1_CREATION_DATE IS NULL;"+
"DELETE FROM TABLE2 WHERE T2_ID = :id AND T2_CREATION_DATE IS NULL;"+
"COMMIT;";
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
jobContext = stepExecution.getJobExecution().getExecutionContext();
Employee employee = (Employee) jobContext.get("employee");
Map<String, Object> parameters = new HashMap<String, Object>();
parameters.put("id", employee.getId());
new NamedParameterJdbcTemplate(dataSource).update(query,parameters);
return RepeatStatus.FINISHED;
}
它正在编译,但我遇到了一个错误的 SQL 语法异常,我认为这是因为 JdbcTemplates 不管理事务查询。
有什么提示吗?
PS : 对于我的读者和作者,我使用Mybatis,他有没有办法处理多次删除?我试过了,但我无法在 Tasklet
中使用 mybatis sql 语句
多亏了你的帮助,我才成功。
起初,我是用这样的单独模板做的:
protected String expDelete = "DELETE FROM TABLE1 WHERE ID = :id AND CREATION_DATE IS NULL";
protected String dipDelete = "DELETE FROM TABLE2 WHERE ID = :id AND CREATION_DATE IS NULL";
Map<String, Object> parameters = new HashMap<String, Object>();
parameters.put("id", employeeInTreatment.getId());
new NamedParameterJdbcTemplate(this.dataSource).update(expDelete,parameters);
new NamedParameterJdbcTemplate(this.dataSource).update(dipDelete,parameters);
它有点工作(编译但未提交删除)。但我坚信我可以一次完成。
我创建了一个服务和一个指向我的 mybatis 映射器的 dao :
employeeBatchDao.deleteEmployeeImportedData(employeeId);
<delete id="deleteEmployeeImportedData" parameterType="Integer">
{call
declare
begin
DELETE FROM TABLE1 WHERE ID = :id AND CREATION_DATE IS NULL;
DELETE FROM TABLE2 WHERE ID = :id AND CREATION_DATE IS NULL;
end
}
我在我的 tasklet 中调用了它并且成功了!现在我可以一次删除所有内容了!
我想像这样从不同的表中进行多次删除:
DELETE FROM TABLE1 WHERE T1_ID = :id AND T1_CREATION_DATE IS NULL;
DELETE FROM TABLE2 WHERE T2_ID = :id AND T2_CREATION_DATE IS NULL;
COMMIT;
但有 spring 个批次。
到目前为止我试过了:
String query = "DELETE FROM TABLE1 WHERE T1_ID = :id AND T1_CREATION_DATE IS NULL;"+
"DELETE FROM TABLE2 WHERE T2_ID = :id AND T2_CREATION_DATE IS NULL;"+
"COMMIT;";
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
jobContext = stepExecution.getJobExecution().getExecutionContext();
Employee employee = (Employee) jobContext.get("employee");
Map<String, Object> parameters = new HashMap<String, Object>();
parameters.put("id", employee.getId());
new NamedParameterJdbcTemplate(dataSource).update(query,parameters);
return RepeatStatus.FINISHED;
}
它正在编译,但我遇到了一个错误的 SQL 语法异常,我认为这是因为 JdbcTemplates 不管理事务查询。
有什么提示吗?
PS : 对于我的读者和作者,我使用Mybatis,他有没有办法处理多次删除?我试过了,但我无法在 Tasklet
中使用 mybatis sql 语句多亏了你的帮助,我才成功。
起初,我是用这样的单独模板做的:
protected String expDelete = "DELETE FROM TABLE1 WHERE ID = :id AND CREATION_DATE IS NULL";
protected String dipDelete = "DELETE FROM TABLE2 WHERE ID = :id AND CREATION_DATE IS NULL";
Map<String, Object> parameters = new HashMap<String, Object>();
parameters.put("id", employeeInTreatment.getId());
new NamedParameterJdbcTemplate(this.dataSource).update(expDelete,parameters);
new NamedParameterJdbcTemplate(this.dataSource).update(dipDelete,parameters);
它有点工作(编译但未提交删除)。但我坚信我可以一次完成。
我创建了一个服务和一个指向我的 mybatis 映射器的 dao :
employeeBatchDao.deleteEmployeeImportedData(employeeId);
<delete id="deleteEmployeeImportedData" parameterType="Integer">
{call
declare
begin
DELETE FROM TABLE1 WHERE ID = :id AND CREATION_DATE IS NULL;
DELETE FROM TABLE2 WHERE ID = :id AND CREATION_DATE IS NULL;
end
}
我在我的 tasklet 中调用了它并且成功了!现在我可以一次删除所有内容了!