序列执行
适用于 ✅ 开源版 ✅ 专业版 ✅ 企业版
大多数数据库都支持某种序列,为您提供唯一值,用于主键和其他枚举。如果您正在使用 jOOQ 的代码生成器,它将为每个序列生成一个序列对象。使用这种序列对象有两种方式
独立调用序列
除了实际构造 select 语句之外,您还可以使用DSLContext 的便捷方法
// Fetch the next value from a sequence BigInteger nextID = create.nextval(S_AUTHOR_ID); // Fetch the current value from a sequence BigInteger currID = create.currval(S_AUTHOR_ID);
在 SQL 中内联序列引用
您可以在 jOOQ SQL 语句中内联序列引用。以下是如何执行此操作的示例
// Reference the sequence in a SELECT statement: Field<BigInteger> s = S_AUTHOR_ID.nextval(); BigInteger nextID = create.select(s).fetchOne(s); // Reference the sequence in an INSERT statement: create.insertInto(AUTHOR, AUTHOR.ID, AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME) .values(S_AUTHOR_ID.nextval(), val("William"), val("Shakespeare")) .execute();
有关在 SQL 语句中内联序列引用的更多信息,请参阅手册中关于序列和序列化的部分。
反馈
您对此页面有任何反馈吗? 我们很乐意倾听!