简体   繁体   English

Mybatis Spring Batch:如何在Tasklet中使用where子句从多张表中删除?

[英]Mybatis Spring Batch : how to delete from mutliple tables with where clause in Tasklet?

I want to do multiple deletes from various tables like this : 我想从这样的各种表中进行多次删除:

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;

but with spring batch. 但春季批。

so far I tried : 到目前为止,我尝试了:

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;
}

it is compiling but i got a bad SQL grammar exception, i think it is because JdbcTemplates does not manage transactionnal query. 它正在编译,但是我遇到了一个错误的SQL语法异常,我认为这是因为JdbcTemplates不管理transactionnal查询。

Any hint ? 有什么提示吗?

PS : for my readers and writers I use Mybatis, is there a way he can handle the multiple delete ? PS:对于使用Mybatis的读者和作家,他有办法处理多重删除吗? I tried but I was unable to use mybatis sql statement in the Tasklet 我尝试过,但是无法在Tasklet中使用mybatis sql语句

Thanks to your help, i managed to make it work. 多亏了您的帮助,我才得以使它成功。

at first, i did it with separate templates like this : 起初,我是用这样的单独模板完成的:

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);

it kinda worked (compiling but the deletions were not commited). 它有点工作(编译,但未提交删除)。 But i was convinced i could do it in one go. 但是我坚信我可以一口气做到这一点。

I created a service and a dao pointing to my mybatis mapper : 我创建了一个服务和一个指向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
    }

i called it in my tasklet and it worked ! 我在Tasklet中称它为工作对象! now i can make all my deletes in one go ! 现在我可以一口气删除所有内容!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM