[英]jdbi version 3, stringtemplate when to escape <, > characters?
我将 jdbi3 与 StringTemplate 4 模板引擎一起使用,我有这个测试查询:
@SqlQuery("select * from test "
+ "where field1 = 5"
+ "<if(cond1)> or field2 \\<= :value1<endif>"
+ "<if(cond2)> or field2 >= :value2<endif>"
+ "<if(cond3)> or field2 in (<values>)<endif>")
@RegisterBeanMapper(Test.class)
@UseStringTemplateEngine
public List<Test> selectTest(
@Define("cond1") boolean cond1, @Bind("value1") int value2,
@Define("cond2") boolean cond2, @Bind("value2") int value3,
@Define("cond3") boolean cond3,
@BindList(value="values", onEmpty=BindList.EmptyHandling.NULL_STRING ) List<Integer> values);
当我必须在查询中使用\
字符<
或>
转义时使用 StringTemplate 引擎? 测试我发现我必须像以前一样在查询中转义<=
。
在使用@BindList
的 IN 子句中,我必须使用<values>
但在这种情况下,我希望像\\<values>
一样转义它,否则它将被 StringTemplate 用作属性,但如果我这样做,查询不会工作。
关于>=
escaping 它在查询中似乎相同。
让我们考虑一下:
3.27.1
Jdbi 版本作为当前 Jdbi 版本。4.3.1
StringTemplate 版本作为当前 StringTemplate 版本,因为当前 Jdbi 版本使用它。 请参阅: jdbi/pom.xml at v3.27.1 · jdbi/jdbi 。请注意有关要转义哪些字符的警告:
由于 StringTemplate 默认使用
<
字符来标记 ST 表达式,您可能需要转义一些 SQL:String datePredSql = "<if(datePredicate)> <dateColumn> \\<:dateFilter <endif>"
@BindList
变量名请参阅BindListTest.ifValueGivenWithNullValueOptionThenResultIsTruthy()
测试方法: jdbi/BindListTest.java v3.27.1 · jdbi/jdbi 。
请注意,该测试涵盖了一个非常相似的注释方法:
@SqlQuery("select name from something <if(name)> where name in (<name>) <endif>")
@UseStringTemplateEngine
List<String> getForValue(@Nonnull @BindList(value = "name", onEmpty = NULL_VALUE) List<String> name);
请注意, @BindList
变量名称未转义:
in (<name>)
请注意要转义的字符:
模板是一系列文本和表达式元素,可以选择穿插注释。 在最粗略的层面上,基本要素是:
text <expr> <! comment !>
使用反斜杠字符转义分隔符:
\<
或\>
。— stringtemplate4/templates.md 在 4.3.1 · antlr/stringtemplate4 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.