Spring batch 在插入spring批之前删除记录
我做了很多研究,但没有找到解决以下问题的方法。我们正在使用spring批处理来处理用户及其组,并在另一个表中创建关联。 用例是当我们发现用户组已更改时,我们需要删除以前的关联并创建新的关联 我如何使用spring批处理来实现这一点,因为处理器将只返回一个项目Spring batch 在插入spring批之前删除记录,spring-batch,Spring Batch,我做了很多研究,但没有找到解决以下问题的方法。我们正在使用spring批处理来处理用户及其组,并在另一个表中创建关联。 用例是当我们发现用户组已更改时,我们需要删除以前的关联并创建新的关联 我如何使用spring批处理来实现这一点,因为处理器将只返回一个项目 public T process(T source) throws Exception { LOG.info("Processing item: " + source); T target = null; //log
public T process(T source) throws Exception {
LOG.info("Processing item: " + source);
T target = null;
//logic to find old and new group
if (!oldGroup.equals(newGroup)) {
LOG.debug("Group Changed");
//this is where I need to delete the old association and create a new one
}
return target;
}
项目处理器的返回类型可能与输入类型不同,转换数据类型是项目处理器的典型用例。您可以使用的一种技术是: 创建一个包装器类型,其中包含关于编写器应该做什么的所有元数据,如要删除的关联的id、要创建的关联等。 然后,条目编写器可以读取这些元数据并相应地采取行动 因此,在您的情况下,如果T是用户,则项目处理器可以是:
public UserWrapper process(User source) throws Exception {
LOG.info("Processing item: " + source);
UserWrapper target = new UserWrapper(source);
//logic to find old an new group
if (!oldGroup.equals(newGroup)) {
LOG.debug("Group Changed");
target.setAssociationToDelete(xxx);
target.setAssociationToCreate(xxx);
// set any information useful to take action
}
return target;
}
相应的编写器会将UserWrapper类型作为输入项,读取有关要删除/创建的关联的信息,并相应地执行操作
您只需要为UserWrapper找到一个好名字。。你知道,命名很难:-
希望这有帮助。有不同的方法。这是我的建议 读卡器-读取用户记录 Processor-筛选要删除的记录 不需要更新-ifoldGroup.equalsnewGroup{return null} 作家- 删除并重新创建关联 如果在读取时只能选择需要更改的记录,则不需要处理器 有关实施细节,请参阅,如果您仍然需要任何帮助,请告知我们