[]ibatis and dollar sign Difference (#, $)

Source: Internet
Author: User

 How to use parameters in SQL statement table names in MyBatis

INSERT INTO Prefix_${table_name} (a, B, c) VALUES (#{a}, #{b}, #{c})

${} indicates direct use literal (literal value)

#{} indicates that this is a parameter

If table_name is "ABC"

Then ${table_name} is ABC

#{table_name} is "ABC"

Java code
  1. 1. #可以进行预编译, type match, #变量名 # will be converted to the type of JDBC
  2. $ does not perform data type matching, $ variable name $ directly replaces $name $ with the contents of name
  3. For example:
  4. SELECT * FROM tablename where id = #id #, assuming the value of ID is 12, where if the database field ID is a character type, then #id# represents ' 12 ', if the ID is integer, then #id# is
  5. SELECT * FROM TableName where id= will be converted to JDBC? Put parameter is set to the value of ID
  6. SELECT * FROM tablename WHERE id = $id $, if the field ID is integer, the SQL statement does not go wrong, but if the field ID is a character type,
  7. Then the SQL statement should be written as a select * from table where id = ' $id $ '
  8. 3. #方式能够很大程度防止sql注入.
  9. The 4.$ method does not prevent SQL injection.
  10. The 5.$ method is typically used to pass in database objects. For example, incoming table names.
  11. 6. So ibatis use # better than $, generally can use the # do not use $.
  12. In addition, use # #可以指定参数对应数据库的类型
  13. Such as:
  14. SELECT * FROM tablename WHERE id = #id: number#
  15. Pay special attention when doing in,like operation
  16. Summarize the following:
  17. The $ number used in the specific Pojo class is also the non-basic type of the value, while the # number is used in the specific base type of the value
  18. <sql id="Update_by_example_where_clause" >
  19. <where>
  20. <foreach collection="Example.oredcriteria" item="Criteria" separator="or" >
  21. <if test="Criteria.valid" >
  22. <trim prefix="(" Prefixoverrides="and" suffix=")" >
  23. <foreach collection="Criteria.criteria" item="criterion" >
  24. <choose>
  25. <when test="Criterion.novalue" >
  26. and ${criterion.condition}
  27. </when>
  28. <when test="Criterion.singlevalue" >
  29. and ${criterion.condition} #{criterion.value}
  30. </when>
  31. <when test="Criterion.betweenvalue" >
  32. and ${criterion.condition} #{criterion.value} and #{criterion.secondvalue}
  33. </when>
  34. <when test="Criterion.listvalue" >
  35. and ${criterion.condition}
  36. <foreach close=")" collection="Criterion.value" item="ListItem" open= "(" separator="," >
  37. #{listitem}
  38. </foreach>
  39. </when>
  40. </choose>
  41. </foreach>
  42. </trim>
  43. </if>
  44. </foreach>
  45. </where>
  46. </sql>

[]ibatis symbol differs from dollar sign (#, $)

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.