【问题标题】:How to use jOOQ RecordUnmapper?如何使用 jOOQ RecordUnmapper?
【发布时间】:2019-07-14 20:12:57
【问题描述】:

我正在尝试实现一个 jOOQ RecordUnmapper 来调整我稍后将插入/更新的记录。

我在下面的尝试,问题是无法实例化Record 类。如何创建Record 对象?另外,如何在insert/update中使用unmapper?

public class TableUnmapper implements RecordUnmapper<Table, Record> {

    @Override
    public Record unmap(Table t) throws MappingException {
        Record r = new Record();  // <-- this line does not compile
        r.from(t);
        r.set(TABLES.TITLE_FONT_FAMILY, t.getTitleFont().getFontFamily());
        return r;
    }

}

【问题讨论】:

    标签: java sql jooq


    【解决方案1】:

    Record是一个接口,所以不能直接创建接口的实例,必须实例化一个实现Record接口的类,如果你使用Jooq代码生成器,你可能已经有了@ 987654324@类,这是你可以用于这个目的的类,

    那么解映射器应该看起来像:

    public class TableUnmapper implements RecordUnmapper<Table, TableRecord> {
    
        @Override
        public TableRecord unmap(Table t) throws MappingException {
    
            TableRecord r = new TableRecord(t.getSomeAttribute());
    
            r.setAttribute(t.getSomeOtherAttribute());
    
            return r;
        }
    
    }
    

    使用解映射器:

    DSLContext create;
    Table table = new Table(/* Whatever Arguments */);
    TableUnmapper unmapper = new TableRecordUnmapper();
    
    // Insert
    create.insertInto(TABLES).set(unmapper.unmap(table));
    
    // update
    create.executeUpdate(unmapper.unmap(table));
    

    【讨论】:

    • 另外,如果没有可用的特定(生成)记录类型,可以使用DSLContext.newRecord()创建记录
    猜你喜欢
    • 2022-01-08
    • 2013-12-05
    • 2018-07-11
    • 2013-10-24
    • 2018-04-21
    • 2012-08-05
    • 2020-11-02
    • 1970-01-01
    • 2017-05-23
    相关资源
    最近更新 更多