Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use $ vs #?

Tags:

I am confused about using $ vs #. I didn't found any guides for this. I used them as
name = #{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 # ?

like image 692
Cataclysm Avatar asked Oct 10 '16 08:10

Cataclysm


People also ask

What is the difference between single and double quotation marks?

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.

What are single quotation marks used for?

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.

Should I use single or double quotes?

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.

When should you use quotation marks?

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.”


1 Answers

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}%' ororder by name ${orderAs}` you need to use String substitution not a prepared statement.

like image 199
Pau Avatar answered Sep 23 '22 12:09

Pau