问题描述
如何在jDBI中执行这样的事情?
How can I execute somethings like this in jDBI ?
@SqlQuery("select id from foo where name in <list of names here>")
List<Integer> getIds(@Bind("nameList") List<String> nameList);
表: foo(id int,name varchar)
与myBatis的@SelectProvider类似。
Similar to @SelectProvider from myBatis.
类似的问题已被提出,但不知怎的回答我不清楚。
Similar questions has been asked How do I create a Dynamic Sql Query at runtime using JDBI's Sql Object API?, but somehow answer is not clear to me.
推荐答案
这应该有效:
@SqlQuery("select id from foo where name in (<nameList>)")
List<Integer> getIds(@BindIn("nameList") List<String> nameList);
不要忘记使用以下方法注释包含此方法的类:
Don't forget to annotate class containing this method with:
@UseStringTemplate3StatementLocator
注释(因为引擎盖JDBI使用Apache StringTemplate来做这样的替换)。另请注意,使用此注释时,不能在不进行转义的情况下在SQL查询中使用<字符(因为它是StringTemplate使用的特殊符号)。
annotation (beacuse under the hood JDBI uses Apache StringTemplate to do such substitutions). Also note that with this annotation, you cannot use '<' character in your SQL queries without escaping (beacause it is a special symbol used by StringTemplate).
这篇关于如何在jDBI中进行查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!