For a simple SQL statement, for example:
Select * from user where id = # ID #
Ibatis will compile the SQL statement
Select * from user where id =?
The passed value is then filled, similar to the JDBC preparestatment format.
The preceding SQL statement is a simple method. Adding like statements that require fuzzy queries is troublesome and may cause SQL injection.
If you want to query users with "sa" in their usernames, you may write SQL statements in this way.
Select * from user where name like '% SA %'
But it cannot be written in the XML of ibatis.
Select * from user where name like # % SA % # Or like % # SA # % is definitely not acceptable, and an error is reported.
There is a simple way to write
Select * from user where name like '% $ name $ %'
$ Will embed the original parameters into the SQL statement without pre-compilation, which may cause SQL injection attacks. In fact, ibatis simply put, every # is used as a parameter and precompiled in setobject mode. The $ method directly replaces the string. Therefore, $ is not safe. It puts user input directly into SQL.
Conclusion: We recommend that you do not use like for queries. 1 is because of low efficiency, 2 is because it is quite troublesome to use in ibatis, and the readability is poor. If you really want to use it, do not use $, instead, use the # Like patchwork form:
Select * from user where name like '%' | # name # | '% ';