spring启动mybatis动态查询

fruv7luv  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(383)

你好,我是mybatis的新手,这是我第一次尝试在SpringBoot中使用注解。
我的代码是这样的:

@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!=错误“>”
“”
“<if test={isnew}>”
“<if{isnew}=true>”
提前谢谢

e0uiprwp

e0uiprwp1#

对于那些可能有同样问题的人,这是解决办法:
必须转义字符<,因为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);```

相关问题