mybatis中的#{}和${}的区别
#{}是直接取值,取到的值会加上单引号标识它在sql中,接续出来后,是一个值
例:userId=1
select * from user where user_id=#{userId}
解析成: select * from user where user_id=’1’
${}也是取值,但是取出来的值,不会加上单引号,解析出阿来后,将会是sql语句中的一部分。
select * from user where user_id=${userId}
解析成: select * from user where user_id=1
select name,age from user :这是正确的sql语句
column=name
select #{column} from user; => select ‘column’ from user 错误
select ${column} from user;=> select column from user 正确
${}可能就会造成sql注入,用时需谨慎。如果需要用到sql语句拼接的时候,可以考虑用${},一般情况下,都是用#{}