ibatis # $区别

1、#可以进行预编译,进行类型匹配,#变量名#  会转化为 jdbc 的 ?
   $不进行数据类型匹配,$变量名$就直接把 $name$替换为 name的内容

   例如:
     select * from tablename where id = #id# ,其中如果字段id为字符型,那么#id#表示的就是'id'类型,如果id为整型,那么#id#就是id类型
     会转化为jdbc的 select * from tablename where id=?,把?参数设置为id的值

     select * from tablename where id = $id$ ,如果字段id为整型,Sql语句就不会出错,但是如果字段id为字符型,
     那么Sql语句应该写成 select * from table where id = '$id$'
     如果id为 "' ' or 1 = 1", 那么这样就有可能导致sql注入,所以ibatis用#比$好,不会造成sql注入

2、ibatis中的参数传入的值参数比较多,最好用bean方式传入,
   也就是通过set/get取值的方式给sql map注入参数,不要用hashmap结构传入,
   每次用hashmap传入会占用比较多的内容。如果参数少,用hashmap也比较方便简单。
   但是对传入参数的判断,用bean方式比较容易检测发现,配置也能够统一配置。

3、#方式能够很大程度防止sql注入.
4、$方式无法方式sql注入.
5、$方式一般用于传入数据库对象.例如传入表名.
6、一般能用#的就别用$.

在做in,like 操作时候要特别注意

mysql: select * from user where user_name like concat('%',#name#,'%')
oracle: select * from user where user_name like '%'||#name#||'%'
sql server:select * from user where user_name like '%'+#name#+'%'

  

猜你喜欢

转载自exceptioneye.iteye.com/blog/1054265
今日推荐