您的位置:首页 > 其它

ibatis 中 $与#的区别

2014-12-10 16:38 148 查看
在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="color: rgb(51, 51, 51); font-family: Arial, Helvetica, simsun, u5b8bu4f53; line-height: 25px;">$ 的作用实际上是字符串拼接, </span><br style="line-height: 25px; color: rgb(51, 51, 51); font-family: Arial, Helvetica, simsun, u5b8bu4f53;" /><span style="color: rgb(51, 51, 51); font-family: Arial, Helvetica, simsun, u5b8bu4f53; line-height: 25px;">select * from $tableName$ </span><br style="line-height: 25px; color: rgb(51, 51, 51); font-family: Arial, Helvetica, simsun, u5b8bu4f53;" /><span style="color: rgb(51, 51, 51); font-family: Arial, Helvetica, simsun, u5b8bu4f53; line-height: 25px;">等效于 </span><br style="line-height: 25px; color: rgb(51, 51, 51); font-family: Arial, Helvetica, simsun, u5b8bu4f53;" /><span style="color: rgb(51, 51, 51); font-family: Arial, Helvetica, simsun, u5b8bu4f53; line-height: 25px;">StringBuffer sb = new StringBuffer(256); </span><br style="line-height: 25px; color: rgb(51, 51, 51); font-family: Arial, Helvetica, simsun, u5b8bu4f53;" /><span style="color: rgb(51, 51, 51); font-family: Arial, Helvetica, simsun, u5b8bu4f53; line-height: 25px;">sb.append("select * from ").append(tableName); </span><br style="line-height: 25px; color: rgb(51, 51, 51); font-family: Arial, Helvetica, simsun, u5b8bu4f53;" /><span style="color: rgb(51, 51, 51); font-family: Arial, Helvetica, simsun, u5b8bu4f53; line-height: 25px;">sb.toString(); </span><br style="line-height: 25px; color: rgb(51, 51, 51); font-family: Arial, Helvetica, simsun, u5b8bu4f53;" /><br style="line-height: 25px; color: rgb(51, 51, 51); font-family: Arial, Helvetica, simsun, u5b8bu4f53;" /><span style="color: rgb(51, 51, 51); font-family: Arial, Helvetica, simsun, u5b8bu4f53; line-height: 25px;">#用于变量替换 </span><br style="line-height: 25px; color: rgb(51, 51, 51); font-family: Arial, Helvetica, simsun, u5b8bu4f53;" /><span style="color: rgb(51, 51, 51); font-family: Arial, Helvetica, simsun, u5b8bu4f53; line-height: 25px;">select * from table where id = #id# </span><br style="line-height: 25px; color: rgb(51, 51, 51); font-family: Arial, Helvetica, simsun, u5b8bu4f53;" /><span style="color: rgb(51, 51, 51); font-family: Arial, Helvetica, simsun, u5b8bu4f53; line-height: 25px;">等效于 </span><br style="line-height: 25px; color: rgb(51, 51, 51); font-family: Arial, Helvetica, simsun, u5b8bu4f53;" /><span style="color: rgb(51, 51, 51); font-family: Arial, Helvetica, simsun, u5b8bu4f53; line-height: 25px;">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;" /><span style="color: rgb(51, 51, 51); font-family: Arial, Helvetica, simsun, u5b8bu4f53; line-height: 25px;">prepareStement.setString(1,'abc'); </span><br style="line-height: 25px; color: rgb(51, 51, 51); font-family: Arial, Helvetica, simsun, u5b8bu4f53;" /><br style="line-height: 25px; color: rgb(51, 51, 51); font-family: Arial, Helvetica, simsun, u5b8bu4f53;" /><span style="color: rgb(51, 51, 51); font-family: Arial, Helvetica, simsun, u5b8bu4f53; line-height: 25px;">------------------------------------------------ </span><br style="line-height: 25px; color: rgb(51, 51, 51); font-family: Arial, Helvetica, simsun, u5b8bu4f53;" /><br style="line-height: 25px; color: rgb(51, 51, 51); font-family: Arial, Helvetica, simsun, u5b8bu4f53;" /><span style="color: rgb(51, 51, 51); font-family: Arial, Helvetica, simsun, u5b8bu4f53; line-height: 25px;">说道这里, 总结一下, 什么时候用$,什么时候 用 # </span><br style="line-height: 25px; color: rgb(51, 51, 51); font-family: Arial, Helvetica, simsun, u5b8bu4f53;" /><br style="line-height: 25px; color: rgb(51, 51, 51); font-family: Arial, Helvetica, simsun, u5b8bu4f53;" /><span style="color: rgb(51, 51, 51); font-family: Arial, Helvetica, simsun, u5b8bu4f53; line-height: 25px;">对于变量部分, 应当使用#, 这样可以有效的防止sql注入, 未来,# 都是用到了prepareStement,这样对效率也有一定的提升 </span><br style="line-height: 25px; color: rgb(51, 51, 51); font-family: Arial, Helvetica, simsun, u5b8bu4f53;" /><br style="line-height: 25px; color: rgb(51, 51, 51); font-family: Arial, Helvetica, simsun, u5b8bu4f53;" /><span style="color: rgb(51, 51, 51); font-family: Arial, Helvetica, simsun, u5b8bu4f53; line-height: 25px;">$只是简单的字符拼接而已,对于非变量部分, 那只能使用$, 实际上, 在很多场合,$也是有很多实际意义的 </span><br style="line-height: 25px; color: rgb(51, 51, 51); font-family: Arial, Helvetica, simsun, u5b8bu4f53;" /><span style="color: rgb(51, 51, 51); font-family: Arial, Helvetica, simsun, u5b8bu4f53; line-height: 25px;">例如 </span><br style="line-height: 25px; color: rgb(51, 51, 51); font-family: Arial, Helvetica, simsun, u5b8bu4f53;" /><span style="color: rgb(51, 51, 51); font-family: Arial, Helvetica, simsun, u5b8bu4f53; line-height: 25px;">select * from $tableName$ 对于不同的表执行统一的查询 </span><br style="line-height: 25px; color: rgb(51, 51, 51); font-family: Arial, Helvetica, simsun, u5b8bu4f53;" /><span style="color: rgb(51, 51, 51); font-family: Arial, Helvetica, simsun, u5b8bu4f53; line-height: 25px;">update $tableName$ set status = #status# 每个实体一张表,改变不用实体的状态 </span><br style="line-height: 25px; color: rgb(51, 51, 51); font-family: Arial, Helvetica, simsun, u5b8bu4f53;" /><span style="color: rgb(51, 51, 51); font-family: Arial, Helvetica, simsun, u5b8bu4f53; line-height: 25px;">特别提醒一下, $只是字符串拼接, 所以要特别小心sql注入问题。</span>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: