Mybaitis#和?的区别
简单理解就是使用${}相当于字符串替换,Mybaitis会直接把传入参数拼入sql语句,这样做就会有sql注入问题的存在。
例如,一个查询语句为
select * from table where name=${name or 1=1},这样被解析成sql时就会变为
select * from table where name=name or 1=1这样就是查询表内所有数据,发生了sql注入问题
使用#{}时,Mybatis会调用预编译方法进行处理,相当于对传入数据加一个双引号,这样
select * from table where name=#{name or 1=1},这样被解析成sql时就会变为
select * from table where name="name or 1=1"
有效防止sql注入问题,所以能用#时尽量不要用$