且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

jOOQ插入查询并返回生成的密钥

更新时间:2023-09-17 23:15:40

您使用的语法用于插入多个记录.这将插入4条记录,每条记录只有一个字段.

The syntax you're using is for inserting multiple records. This is going to insert 4 records, each with one field.

.values(node.getParentid())
.values(node.getName())
.values(node.getRem())
.values(node.getUipos())

但是您声明了4个字段,所以这行不通:

But you declared 4 fields, so that's not going to work:

create.insertInto(Tblcategory.TBLCATEGORY, 
  Tblcategory.PARENT_ID, Tblcategory.NAME, Tblcategory.REM, Tblcategory.UIPOS)

您可能想做的是这样:

Result<TblcategoryRecord> result = create
  .insertInto(Tblcategory.TBLCATEGORY, 
    Tblcategory.PARENT_ID, Tblcategory.NAME, Tblcategory.REM, Tblcategory.UIPOS)
  .values(node.getParentid(), node.getName(), node.getRem(), node.getUipos())
  .returning(Tblcategory.CATEGORY_ID)
  .fetch();

或者:

Result<TblcategoryRecord> result = create
  .insertInto(Tblcategory.TBLCATEGORY) 
  .set(Tblcategory.PARENT_ID, node.getParentid())
  .set(Tblcategory.NAME, node.getName())
  .set(Tblcategory.REM, node.getRem())
  .set(Tblcategory.UIPOS, node.getUipos())
  .returning(Tblcategory.CATEGORY_ID)
  .fetch();

也许,通过使用

TblcategoryRecord result =
  // [...]
  .fetchOne();

有关更多详细信息,请考虑手册:

For more details, consider the manual:

http://www.jooq .org/doc/2.6/manual/sql-building/sql-statements/insert-statement/

或用于创建返回值的INSERT语句的Javadoc:

Or the Javadoc for creating INSERT statements that return values:

http://www.jooq.org/javadoc/latest/org/jooq/InsertReturningStep.html