使用jdbi执行创建或更新 [英] performing create-or-update with jdbi
问题描述
对于一个小型新项目,我决定尝试 JDBI (通常我使用hibernate / jpa)。
For a small new project, I decided to give JDBI a try (normally I work with hibernate/jpa).
我喜欢使用@ SqlUpdate / @ SqlQuery创建基于注释的轻量级dao。
I like the lightweight, annotation based dao creation using @SqlUpdate/@SqlQuery.
但是:有我无法确定是否要创建实体或更新现有实体的情况。
我会放置一个select语句,并根据其返回值使用insert或update语句。
But: There are situations where I can't be sure if I want to create an entity or update an existing one. I would place a "select" statement and depending on its return value use the insert or update statement.
问题:这是以某种方式支持的接口 - 仅仅是jdbi中的dao?或者我是否必须自己编写createOrUpdate方法(使自动生成的dao或多或少过时)?
Question: is this somehow supported by the "interface-only" dao in jdbi? Or do I have to write a "createOrUpdate" method myself (making the auto generated dao more or less obsolete)?
感谢任何提示。
推荐答案
感谢@zloster我现在构建了一个基于抽象类而不是接口的解决方案。按要求工作。
Thanks to @zloster I now built a solution based on an abstract class instead of an interface. Works as required.
@SqlUpdate("insert ...")
public abstract void insert(...);
@SqlUpdate("update...")
public abstract void update();
public X createOrUpdate(final X x) {
if (!exists(x)) {
insert(x);
} else {
update(x);
}
return find(...);
}
这篇关于使用jdbi执行创建或更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!