mybatis中xml文件的${}和#{}区别

在项目中要实现所有业务批量提交的功能,实现方式,把表名,表主键字段当做参数传递,在xml文件中全部使用的#{},导致解析的时候出现问题。

1 #是将传入的值当做字符串的形式, eg:select id,name,age from student where id
=#{id},当前端把 id 值 1,传入到后台的时候,就相当于 select id,name,age from student
where id =‘1’.
2 s q l e g : s e l e c t i d , n a m e , a g e f r o m s t u d e n t w h e r e i d = 是将传入的数据直接显示生成 sql 语句, eg:select id,name,age from student where id = {id},当前端把 id 值 1,传入到后台的时候,就相当于 select id,name,age from
student where id = 1.
3 使用#可以很大程度上防止 sql 注入。 (语句的拼接)
4 但是如果使用在 order by 中就需要使用 KaTeX parse error: Expected 'EOF', got '#' at position 54: …5 在大多数情况下还是经常使用#̲,但在不同情况下必须使用.
我觉得#与的区别最大在于: #{} 传入值时, sql 解析时,参数是带引号的,而的区别最大
在于: #{} 传入值时, sql 解析时,参数是带引号的,而{}穿入值, sql 解析时,参数是不
带引号的。
#{ }:解析为一个 JDBC 预编译语句(prepared statement)的参数标记符。预编译,重复
利用,效率高

猜你喜欢

转载自blog.csdn.net/weixin_41969587/article/details/88083727
今日推荐