问题描述
我正在尝试实现jOOQ RecordUnmapper
来调整记录,以后再插入/更新.
I'm trying to implement a jOOQ RecordUnmapper
to adjust a record that I will later insert/update.
我在下面的尝试中,问题在于Record
类无法实例化.如何创建Record
对象?另外,如何在插入/更新中使用解映射器?
My attempt below, problem is that the Record
class cannot be instantiated. How to create the Record
object? Also, how to use the unmapper in insert/update?
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
类,这是您可以用于此目的的类
Record
is an interface, so you can't directly create an instance of the interface, you have to instantiate a class that implements the Record
interface, if you use Jooq code generator, you probably already have a TableRecord
class, this is the class you can use for this purpouse,
然后解映射器应类似于:
then the unmapper should look something like:
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));
这篇关于如何使用jOOQ RecordUnmapper?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!