【问题标题】:Grails Spring Batch - pattern for CRUD from record format (how to implement Delete)Grails Spring Batch - 来自记录格式的 CRUD 模式(如何实现删除)
【发布时间】:2015-02-27 19:21:30
【问题描述】:

我正在考虑使用 Grails Spring Batch 插件在 Grails 中使用 Spring Batch。

如果我的输入文件中有许多固定长度的记录引用了一个实体,其中该记录的一部分指示该记录是新项目、应该更新的现有项目还是应该删除的现有项目,集成到 Spring Batch 模式中的最佳模式是什么?

所以如果我可能的记录是这样的:

// create new record of type AA, data is 12345
AAN12345 

// update record of type AA, data is 12345 (assume that the data is the key and I can find the existing AA item using this key)
AAU12345

// delete record of type AA using 12345 as the key
AAD12345

我对 LineMapper 很满意,它从 FlatFileItemReader 获取一行并创建一个新项目并将其传递给写入器进行保存。

LineMapper 可能如下所示:

class AaLineMapper implements LineMapper<AaItem> {

    @Override
    AaItem mapLine(String line, int lineNumber) throws Exception {
        switch (line[0..1]) {
            case 'N':
                AaItem item = new AaItem()
                // set fields here based on line
                return item
                break

            case 'U':
                // possibly this?
                AaItem item = AaItem.findByKey(someValueWithinLine)
                // set fields here based on line
                return item
                break

            case 'D':
                // not sure on this one, deleting and returning null doesn't seem to work
                // I thought the writer should delete the object?
                break
        }
    }
}

但是,为了更新,我是否假设最好的方法是在 LineMapper 中使用 Item.findByKey(12345),然后修改 Item 并在 writer 中调用 save()

如何实现删除?如果我从 LineMapper 返回 null,则应用程序似乎停止了。我认为作者应该删除对象,而不是这个?还是我只使用findByKey(12345),然后通过设置删除标志传递给作者?

抱歉,这是使用框架的第一天。我有兴趣了解最佳实践。

【问题讨论】:

    标签: grails spring-batch


    【解决方案1】:

    你很接近,但并不完全在那里。你真正需要你的线映射器产生的是一个类的实例,它不仅包含要生效的域类的实例,而且还包含一个属性来指示需要采取什么行动(可能是由项目处理器或分类项目作家,取决于您的要求)。

    所以这样的事情可能会起作用:

    class MyActionContainerClass {
      Object target
      String actionType // U, D, N
    }
    

    【讨论】:

    • 非常感谢 - target 是通过 U 和 D 分支中的问题中的某种 findByKey() 方法获得的吗?
    • 是的,这很好。我使用 Object 是因为我不确定您在那里使用的是哪种类,或者它是否是多个类。不过,你的想法是对的。如果您想详细讨论,请访问 chat.freenode.org 上的 IRC #grails。
    • 不客气,欢迎来到 Spring Batch 的精彩世界!
    猜你喜欢
    • 2018-07-24
    • 2015-06-22
    • 1970-01-01
    • 1970-01-01
    • 2020-01-02
    • 1970-01-01
    • 2019-09-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多