# And $ symbols in ibatis2.3 (conversion)

Source: Internet
Author: User

在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$‘$ 的作用实际上是字符串拼接, 
select * from $tableName$ 
等效于 
StringBuffer sb = new StringBuffer(256); 
sb.append("select * from ").append(tableName); 
sb.toString(); 

#用于变量替换 
select * from table where id = #id# 
等效于 
prepareStement = stmt.createPrepareStement("select * from table where id = ?") 
prepareStement.setString(1,‘abc‘); 

------------------------------------------------ 

说道这里, 总结一下, 什么时候用$,什么时候 用 # 

对于变量部分, 应当使用#, 这样可以有效的防止sql注入, 未来,# 都是用到了prepareStement,这样对效率也有一定的提升 

$只是简单的字符拼接而已,对于非变量部分, 那只能使用$, 实际上, 在很多场合,$也是有很多实际意义的 
例如 
select * from $tableName$ 对于不同的表执行统一的查询 
update $tableName$ set status = #status# 每个实体一张表,改变不用实体的状态 
特别提醒一下, $只是字符串拼接, 所以要特别小心sql注入问题。

# And $ symbols in ibatis2.3 (conversion)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.