I am confused about using $ vs #
. I didn't found any guides for this. I used them asname = #{name}
, name like '%${word}%'
, order by name ${orderAs}
,where name = #{word}
Sometimes , these are work fine but at the sometimes , parameters aren't included or gave me error like
org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'name'.......
So, I'd like to know when to use $
or #
?
General Usage Rules In America, Canada, Australia and New Zealand, the general rule is that double quotes are used to denote direct speech. Single quotes are used to enclose a quote within a quote, a quote within a headline, or a title within a quote.
Single quotation marks are also known as 'quote marks', 'quotes', 'speech marks' or 'inverted commas'. Use them to: show direct speech and the quoted work of other writers. enclose the title of certain works.
As a general rule, British usage has in the past usually preferred single quotes for ordinary use, but double quotes are now increasingly common; American usage has always preferred double quotes.
Quotation marks are for when you want to use someone else's words in your writing. Let's say you want to write about something you heard your friend say. You could do it like this: John said, “I really hate when it's hot outside.”
Following the myBatis
guidelines #{}
is used in your sql statements.
If you take a look any of MyBatis Reference in the Section Mapper XML Files it says explicity:
Notice the parameter notation:
#{id}
Otherwise ${}
is for
1- Configuration properties.
For example:
<properties resource="org/mybatis/example/config.properties"> <property name="username" value="dev_user"/> <property name="password" value="F2Fa3!33TYyg"/> </properties>
Then the properties can be used like next:
<dataSource type="POOLED"> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </dataSource>
2- String Substitution ${}
(Parameters section):
By default, using the #{} syntax will cause MyBatis to generate PreparedStatement properties and set the values safely against the PreparedStatement parameters (e.g. ?). While this is safer, faster and almost always preferred, sometimes you just want to directly inject a string unmodified into the SQL Statement. For example, for ORDER BY, you might use something like this:
ORDER BY ${columnName}
Here MyBatis won't modify or escape the string.
NOTE It's not safe to accept input from a user and supply it to a statement unmodified in this way. This leads to potential SQL Injection attacks and therefore you should either disallow user input in these fields, or always perform your own escapes and checks.
So definitively in name like '%${word}%' or
order by name ${orderAs}` you need to use String substitution not a prepared statement.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With