简体   繁体   English

jdbi @sqlquery:无法确定参数$ 1的数据类型

[英]jdbi @sqlquery: could not determine data type of parameter $1

I am trying to add optional/nullable parameters to my sql query. 我正在尝试向我的sql查询添加可选/可为空的参数。

@SqlQuery("select * from temp where param1 = :param1")
List<Temp> findValues(@Bind("param1") String param1)

Problem: param can be null. 问题:参数可以为空。 So I tried to add null check in the sql query. 因此,我尝试在sql查询中添加null检查。

SqlQuery("select * from temp where ( (:param1 is not null) and (param1 = :param1"))
This throws the following error: org.skife.jdbi.v2.exceptions.UnableToExecuteStatementException: org.postgresql.util.PSQLException: ERROR: could not determine data type of parameter $1 这将引发以下错误:org.skife.jdbi.v2.exceptions.UnableToExecuteStatementException:org.postgresql.util.PSQLException:错误:无法确定参数$ 1的数据类型

In my problem I have to add multiple params all of which are nullable. 在我的问题中,我必须添加多个参数,所有这些参数都是可为空的。 (optional filters) (可选过滤器)

NOTE coalesce(:param1, param1) = param1 : doesn't work quite right. 注意coalesce(:param1, param1) = param1 :不太正确。 If param1 is sent, it gives back results where value is either param1 value, or null. 如果发送了param1,它将返回结果,其中value要么为param1值,要么为null。

How can I make this null check work? 如何使此空检查工作? Or is there a way that I can generate the sql query and pass it as a string to @SqlQuery? 还是有一种方法可以生成sql查询并将其作为字符串传递给@SqlQuery?

I have looked everywhere but can't seem to find a solution. 我到处都看过,但似乎找不到解决方法。 Any help would be appreciated. 任何帮助,将不胜感激。

If you want to use it as optional filters, you could use @Define. 如果要将其用作可选过滤器,则可以使用@Define。 Using @Define, you define attributes for the template engine to use(jdbi v2 uses ST v3 ). 使用@Define,可以定义要使用的模板引擎的属性(jdbi v2使用ST v3 )。 Then, your example would look like something like this: 然后,您的示例将如下所示:

@SqlQuery("SELECT * FROM temp WHERE 1 = 1 <if(PARAM1)> AND param1 = :param1 <endif>)
List<Temp> findValues(@Define("PARAM1") @Bind("param1") String param1)

So, in this way, if value for param1 is null, query will look like: 因此,以这种方式,如果param1的值为null,查询将类似于:

SELECT * FROM temp WHERE 1 = 1

otherwise: 除此以外:

SELECT * FROM temp WHERE 1 = 1 AND param1 = :param1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM