mybaties中xml文件下动态传参#{}与${}区别?

#{} 为占位符,传参后默认会用引号将参数引起来
${} 单纯替代,即传过来的参数是什么就填什么,不会有引号
例如:
table的参数值为acmg_pecust_valueInfo

select * from #{table}; >> select * from 'acmg_pecust_valueInfo';
select * from ${table}; >> select * from acmg_pecust_valueInfo;
优缺点:

使用#{}可以预防sql攻击,而${}却不能

例如:
当custNo的参数值为001;drop acmg_pecust_valueInfo;时;
使用${}时如下:

select * from acmg_pecust_valueInfo t where t.cust_no=${custNo};
>> select * from acmg_pecust_valueInfo t where t.cust_no=001;drop acmg_pecust_valueInfo;

上述情况就是执行sql同时也删除了表
用#{}的话就不会发生此情况

select * from acmg_pecust_valueInfo t where t.cust_no=#{custNo};
>> select * from acmg_pecust_valueInfo t where t.cust_no="001;drop acmg_pecust_valueInfo;"

你可能感兴趣的:(后端代码类)