mybatis 的${} 与#{}

mybatis的占位符有两种${}与#{}两者的区别

区别

通过#{}传入的参数,mybatis会自动为其加上引号
通过${}传入的参数,mybatis不会为其加上引号
例如: value=11

 select * from user where  id=#{}
输出为 select * from user where id = '11'
select * from user where id = ${}
输出为 select * from user where id =11 

安全

${}会有sql注入的安全隐患,如 value = ‘1 or 1=1 ’
那么 sql 输出为 select * from user where id = 1 or 1=1 这个就等于没有了限制条件

用途

#{}多用于条件参数的传入
${}用于表明,数据库对象名, group by 排序

  value1='group by' value2='time'
  select * from user  ${value1} ${value2}
  输出 select * from user group by time 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章