`

Mybatis 中在传参时,$ 和# 的区别

    博客分类:
  • j2ee
 
阅读更多

原博客地址:http://blog.csdn.net/shaozengwei/article/details/17446247

 

MyBatis中使用parameterType向SQL语句传参,parameterType后的类型可以是基本类型int,String,HashMap和java自定义类型。

在SQL中引用这些参数的时候,可以使用两种方式#{parameterName}或者${parameterName},

   首先,我们说一下这两种引用参数时的区别,使用#{parameterName}引用参数的时候,Mybatis会把这个参数认为是一个字符串,例如传入参数是“Smith”,那么在SQL(Select * from emp where name = #{employeeName})使用的时候就会转换为Select * from emp where name = 'Smith';同时在SQL(Select * from emp where name = ${employeeName})使用的时候就会转换为Select * from emp where name = Smith。

   再次,从安全性上考虑,能使用#尽量使用#来传参,因为这样可以有效防止SQL注入的问题。

 

最后总结一下必须使用$引用参数的情况,我能想到的目前只有一种,那就是参数的int型的时候,必须使用$引用。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics