ibatis 中 $与#的区别

转载来自:http://blog.csdn.net/kiss_vicente/article/details/7602900
在sql配置中比如in(#rewr#) 与in ($rewr$)

在Ibatis中我们使用SqlMap进行Sql查询时需要引用参数,在参数引用中遇到的符号#和$之间的区分为,#可以进行与编译,进行类型匹配,而$不进行数据类型匹配,例如: 

select * from table where id = #id# ,其中如果字段id为字符型,那么#id#表示的就是'id'类型,如果id为整型,那么#id#就是id类型。 

select * from table where id = $id$ ,如果字段id为整型,Sql语句就不会出错,但是如果字段id为字符型,那么Sql语句应该写成 select * from table where id = '$id$'


<span style="font-family:Arial,Helvetica,simsun,u5b8bu4f53;font-size:14px;color:#333333;line-height:25px; text-align:left">$ 的作用实际上是字符串拼接, </span><br style="line-height:25px; color:rgb(51,51,51); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px; text-align:left" /><span style="font-family:Arial,Helvetica,simsun,u5b8bu4f53;font-size:14px;color:#333333;line-height:25px; text-align:left">select * from $tableName$ </span><br style="line-height:25px; color:rgb(51,51,51); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px; text-align:left" /><span style="font-family:Arial,Helvetica,simsun,u5b8bu4f53;font-size:14px;color:#333333;line-height:25px; text-align:left">等效于 </span><br style="line-height:25px; color:rgb(51,51,51); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px; text-align:left" /><span style="font-family:Arial,Helvetica,simsun,u5b8bu4f53;font-size:14px;color:#333333;line-height:25px; text-align:left">StringBuffer sb = new StringBuffer(256); </span><br style="line-height:25px; color:rgb(51,51,51); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px; text-align:left" /><span style="font-family:Arial,Helvetica,simsun,u5b8bu4f53;font-size:14px;color:#333333;line-height:25px; text-align:left">sb.append("select * from ").append(tableName); </span><br style="line-height:25px; color:rgb(51,51,51); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px; text-align:left" /><span style="font-family:Arial,Helvetica,simsun,u5b8bu4f53;font-size:14px;color:#333333;line-height:25px; text-align:left">sb.toString(); </span><br style="line-height:25px; color:rgb(51,51,51); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px; text-align:left" /><br style="line-height:25px; color:rgb(51,51,51); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px; text-align:left" /><span style="font-family:Arial,Helvetica,simsun,u5b8bu4f53;font-size:14px;color:#333333;line-height:25px; text-align:left">#用于变量替换 </span><br style="line-height:25px; color:rgb(51,51,51); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px; text-align:left" /><span style="font-family:Arial,Helvetica,simsun,u5b8bu4f53;font-size:14px;color:#333333;line-height:25px; text-align:left">select * from table where id = #id# </span><br style="line-height:25px; color:rgb(51,51,51); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px; text-align:left" /><span style="font-family:Arial,Helvetica,simsun,u5b8bu4f53;font-size:14px;color:#333333;line-height:25px; text-align:left">等效于 </span><br style="line-height:25px; color:rgb(51,51,51); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px; text-align:left" /><span style="font-family:Arial,Helvetica,simsun,u5b8bu4f53;font-size:14px;color:#333333;line-height:25px; text-align:left">prepareStement = stmt.createPrepareStement("select * from table where id = ?") </span><br style="line-height:25px; color:rgb(51,51,51); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px; text-align:left" /><span style="font-family:Arial,Helvetica,simsun,u5b8bu4f53;font-size:14px;color:#333333;line-height:25px; text-align:left">prepareStement.setString(1,'abc'); </span><br style="line-height:25px; color:rgb(51,51,51); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px; text-align:left" /><br style="line-height:25px; color:rgb(51,51,51); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px; text-align:left" /><span style="font-family:Arial,Helvetica,simsun,u5b8bu4f53;font-size:14px;color:#333333;line-height:25px; text-align:left">------------------------------------------------ </span><br style="line-height:25px; color:rgb(51,51,51); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px; text-align:left" /><br style="line-height:25px; color:rgb(51,51,51); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px; text-align:left" /><span style="font-family:Arial,Helvetica,simsun,u5b8bu4f53;font-size:14px;color:#333333;line-height:25px; text-align:left">说道这里, 总结一下, 什么时候用$,什么时候 用 # </span><br style="line-height:25px; color:rgb(51,51,51); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px; text-align:left" /><br style="line-height:25px; color:rgb(51,51,51); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px; text-align:left" /><span style="font-family:Arial,Helvetica,simsun,u5b8bu4f53;font-size:14px;color:#333333;line-height:25px; text-align:left">对于变量部分, 应当使用#, 这样可以有效的防止sql注入, 未来,# 都是用到了prepareStement,这样对效率也有一定的提升 </span><br style="line-height:25px; color:rgb(51,51,51); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px; text-align:left" /><br style="line-height:25px; color:rgb(51,51,51); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px; text-align:left" /><span style="font-family:Arial,Helvetica,simsun,u5b8bu4f53;font-size:14px;color:#333333;line-height:25px; text-align:left">$只是简单的字符拼接而已,对于非变量部分, 那只能使用$, 实际上, 在很多场合,$也是有很多实际意义的 </span><br style="line-height:25px; color:rgb(51,51,51); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px; text-align:left" /><span style="font-family:Arial,Helvetica,simsun,u5b8bu4f53;font-size:14px;color:#333333;line-height:25px; text-align:left">例如 </span><br style="line-height:25px; color:rgb(51,51,51); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px; text-align:left" /><span style="font-family:Arial,Helvetica,simsun,u5b8bu4f53;font-size:14px;color:#333333;line-height:25px; text-align:left">select * from $tableName$ 对于不同的表执行统一的查询 </span><br style="line-height:25px; color:rgb(51,51,51); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px; text-align:left" /><span style="font-family:Arial,Helvetica,simsun,u5b8bu4f53;font-size:14px;color:#333333;line-height:25px; text-align:left">update $tableName$ set status = #status# 每个实体一张表,改变不用实体的状态 </span><br style="line-height:25px; color:rgb(51,51,51); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px; text-align:left" /><span style="font-family:Arial,Helvetica,simsun,u5b8bu4f53;font-size:14px;color:#333333;line-height:25px; text-align:left">特别提醒一下, $只是字符串拼接, 所以要特别小心sql注入问题。</span>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值