【发布时间】:2022-04-02 05:04:22
【问题描述】:
jDBI 查询是否可以有可选(空)参数?我试图让可选参数在数据库查询中工作。我正在使用 dropwizard。
@SqlQuery("SELECT * \n" +
"FROM posts \n" +
"WHERE (:authorId IS NULL OR :authorId = author_id)")
public List<Post> findAll(@Bind("authorId") Optional<Long> authorId);
通过 authorId 时查询有效,但当它为 NULL 时给我这个错误:
org.postgresql.util.PSQLException: ERROR: could not determine data type of parameter $1
这是我调用的资源路径:
@GET
public ArrayList<Post> getPosts(@QueryParam("authorId") Long authorId)
{
return (ArrayList<Post>)postDao.findAll(Optional.fromNullable(authorId));
}
根据我的阅读,这是可以做到的,所以我猜我遗漏了一些东西或有一个明显的错误。任何帮助将不胜感激!
仅供参考 - 我也尝试过没有 guava Optional(由 dropwizard 支持) - 只是将 authorId 作为 Long 发送为空。只要它不为空,这也有效。
【问题讨论】:
-
你应该检查jdbi community
-
谢谢@zloster,我也把这个贴在那里了。
标签: java dropwizard jdbi