(How) can I use “LIKE” in SQL queries with MyBatis safely and DB-agnostic?

后端 未结 9 1157
面向向阳花
面向向阳花 2021-02-04 03:33

In MyBatis, you mark the places where parameters should be inserted into your SQL like so:

SELECT * FROM Person WHERE id = #{id}

9条回答
  •  感动是毒
    2021-02-04 03:49

    Typically this is done by adding the % to the parameter itself before passing it in, in whatever language you're using outside of SQL. However note that either way you might still need to do an escaping step if your search term may have _ or % in it. See eg this question for background.)

    To fix the concatenation problem in general, put MySQL into ANSI sql_mode and you get proper support for the || operator, as well as correct handling of double quotes for schema names rather than string literals.

    (If you can't do that you'd have to build a function to build the statement out of either || or CONCAT(), abstracting away the difference.)

提交回复
热议问题