$和#之间的区别
#相当于对数据加上双引号,$相当于直接显示数据
1.# 对传入的参数视为字符串,也就是它会编译
select * from user where name = #{name}
比如:
传一个csdn,那么完整的sql语句是:select * from user where name = 'csdn'
2.$ 将不会将传入的值进行预编译:
select * from user where name = #{name}
比如:
传一个csdn,那么完整的sql语句是:select * from user where name = csdn
3.相较于$来说,#的优势就在于它能够很大程度的防止sql注入,而$则不行
比如登录操作:
前台传来的数据:用户名:“wang”,密码是“1 or 1 = 1”
3.1 #形式:
select * from user where username=#{name} and password = #{pwd}
完整的sql语句:
select * from user where username='wang' and password = '1 or 1 = 1' 防止了sql注入
3.2 $形式
select * from user where username=${name} and password = ${pwd}
select * from user where username=wang and password = 1 or 1 = 1 sql注入
4.myBatis排序时使用order by动态参数时需要注意,用$而不是#。