Spring Boot Mybatis动态查询

GermánSánchez牧师

您好,这是mybatis的新手,这是我第一次尝试在Spring Boot中使用注释。

我的代码是这样的:

@Select("<script>"
            + "SELECT t.something, s.somewhat, "
            + "FROM t.table1 t "
            + "LEFT JOIN table2 s ON t.id = s.id "
            + "WHERE s.delete_date IS NULL "
            + "<if test=\"isnew\"> "
            + "AND t.insert_date = t.update_date "
            + "AND (t.score >= s.min_score AND t.score <= s.max_score) "
            + "</if>"
            + "union "
            + "ANOTHER SIMILAR QUERY WITH ANOTHER <IF> + "</script>")
List<Map<String, Object>> methodName(@Param("isnew") Boolean isNew);

这是错误。

Caused by: java.lang.IllegalArgumentException: org.apache.ibatis.builder.BuilderException: Could not find value method on SQL annotation. Cause: org.apache.ibatis.builder.BuilderException: Error creating document instance. Cause: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 822;

我几乎确定语法错误是非常可预测的,但我找不到它。以下是我尝试过的一些示例,但没有一个起作用:

  • “ <if test =“ isnew = true”>”
  • “ <if test =“ isnew == true”>”
  • “ <if test =“ isnew!= false”>”
  • “ <if test =” isnew“>”
  • “ <if test =#{isnew}”>“
  • “ <if#{isnew} = true>”

提前致谢

GermánSánchez牧师

对于那些可能有相同问题的人,这是解决方案:

您必须转义字符<,因为mybatis将其视为未打开的标签,&lt可以工作:

            + "SELECT t.something, s.somewhat, "
            + "FROM t.table1 t "
            + "LEFT JOIN table2 s ON t.id = s.id "
            + "WHERE s.delete_date IS NULL "
            + "<if test=\"isnew\"> "
            + "AND t.insert_date = t.update_date "
            + "AND (t.score >= s.min_score AND t.score lt;= s.max_score) "
            + "</if>"
            + "union "
            + "ANOTHER SIMILAR QUERY WITH ANOTHER <IF> + "</script>")
List<Map<String, Object>> methodName(@Param("isnew") Boolean isNew);```

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章