简体   繁体   中英

Spring Boot Mybatis Dynamic query

Hello im pretty new to mybatis and is the first time im trying to use with annotations in spring boot.

My code is something like this :

@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);

This is the error.

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;

Im almost sure there is an error of syntax very predictable but I cant find it. Here are some examples of what I tried, none of them works:

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

Thanks in advance

For those who may have the same problem this is the solution:

You have to escape the character < , because mybatis takes it as an unopened tag, &lt will work :

            + "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);```

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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