码迷,mamicode.com
首页 > 其他好文 > 详细

Mybatis中#和$的区别

时间:2017-09-16 01:15:40      阅读:270      评论:0      收藏:0      [点我收藏+]

标签:问题   sel   数据   对象   tis   过程   使用   nbsp   color   

01.$ 不安全 底层实现是Statement对象

select * from student where id=${id}

如果我们id传入的是11 编译之后

select * from student where id=11

 

# 安全 底层实现是PreparedStatement对象

select * from student where id=#{id}

如果我们id传入的是11 编译之后

select * from student where id=?

 

MyBatis启用了预编译功能,在SQL执行前,会先将上面的SQL发送给数据库进行编译;

执行时,如果传入参数为#{}格式的,将传入参数替换编译好的sql中的占位符“?”;

如果传入参数格式为${},则直接使用编译好的SQL就可以了。

因为SQL注入只能对编译过程起作用,所以使用#{}传入参数的方式可以很好地避免了SQL注入的问题。

 

02.在sql语句需要排序的时候

order by ${id}

只有在需要排序的时候 使用$ 

其他时候能用#绝对不用$

 

Mybatis中#和$的区别

标签:问题   sel   数据   对象   tis   过程   使用   nbsp   color   

原文地址:http://www.cnblogs.com/areyouready/p/7529241.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!