mybatis中$和#的用法区别
2016-05-09 00:00
363 查看
项目中拼接了一个字符串作为sql not in的条件,但是用#起不到作用,
跟踪日志发现
#的话sql中为?,采用的是preparedStatement的方式
$的话sql中为直接参数连接起来
String Substitution By default, using the #{} syntax will cause MyBatis to generate PreparedStatement properties and set the values safely against the PreparedStatement parameters (e.g. ?).
While this is safer, faster and almost always preferred, sometimes you just want to directly inject a string unmodified into the SQL Statement.
For example, for ORDER BY, you might use something like this: ORDER BY ${columnName} Here MyBatis won't modify or escape the string.
NOTE It's not safe to accept input from a user and supply it to a statement unmodified in this way. This leads to potential SQL Injection attacks and therefore you should either disallow user input in these fields, or always perform your own escapes and checks.
跟踪日志发现
#的话sql中为?,采用的是preparedStatement的方式
$的话sql中为直接参数连接起来
String Substitution By default, using the #{} syntax will cause MyBatis to generate PreparedStatement properties and set the values safely against the PreparedStatement parameters (e.g. ?).
While this is safer, faster and almost always preferred, sometimes you just want to directly inject a string unmodified into the SQL Statement.
For example, for ORDER BY, you might use something like this: ORDER BY ${columnName} Here MyBatis won't modify or escape the string.
NOTE It's not safe to accept input from a user and supply it to a statement unmodified in this way. This leads to potential SQL Injection attacks and therefore you should either disallow user input in these fields, or always perform your own escapes and checks.