The difference between the # and $ symbols in the Mybatis.xml file and the processing of mathematical symbols

Source: Internet
Author: User

1.#{} represents a placeholder symbol that can be implemented by #{} to set values in the placeholder PreparedStatement, automate Java type and JDBC type conversions, and#{} can effectively prevent SQL injection . #{} can receive either a simple type value or a Pojo property value. If ParameterType transmits a single simple type value, #{} can be a value or other name in parentheses.

Using the # passed in parameter is, the SQL statement parsing is added "", such as SELECT * FROM table where name = #{name}, the incoming name is Xiao Li, then the last print is

SELECT * FROM table where name = ' Xiao Li ' is parsed as a string, so the benefit compared to $ is more obvious right, #{} can prevent SQL injection, if you pass in the parameter is single quotes ', then if you use ${}, this way will be error.

Another scenario is if you want to do a dynamic sort, such as an order by column, this time be sure to use ${}, because if you use #{}, then the print will be

SELECT * FROM table order BY ' name ', this is useless.

That is, it is best not to use #.

2.

If you use >,<,>=,<= in Mapper.xml to destroy XML-formatted symbols, you can cause
Mybaits cannot parse the XML file properly.

The workaround is to write the SQL statement in the <! [Cdata[sql statement in]]>
<select id= "test" resulttype= "Java.lang.Integer" >
<! [cdata[
Select 1<2
]]>
</select>

The difference between the # and $ symbols in the Mybatis.xml file and the processing of mathematical symbols

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.