当前位置: 代码迷 >> java >> 如何使用jOOQ RecordUnmapper?
  详细解决方案

如何使用jOOQ RecordUnmapper?

热度:125   发布时间:2023-07-16 17:54:10.0

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

我在下面的尝试中遇到的问题是,无法实例化Record类。 如何创建Record对象? 另外,如何在插入/更新中使用解映射器?

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

}

Record是一个接口,因此您不能直接创建该接口的实例,您必须实例化一个实现Record接口的类,如果您使用Jooq代码生成器,则可能已经有一个TableRecord类,这就是您所需要的类可以用于这个目的

那么解映射器应类似于:

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