To @Param or not to @Param? That is the question.

45 views
Skip to first unread message

icfantv

unread,
Apr 18, 2013, 12:40:44 PM4/18/13
to mybati...@googlegroups.com
I'm looking at the mybatis and mybatis-spring docs and I don't specifically
see a more indepth example of this.

If I have a mapper interface method of "void foo(String bar);" my XML looks
like this:

<select id="foo" parameterType="String" resultType="String">
select bat from baz where my_column = ${bar}
</select>

If I change it to "void foo(@Param("bar") String bar);" I think the ${bar}
would change to #{bar}, correct?

So, is it possible to even have "void foo(String bar, Integer bat);" because
then, what would your parameterType be? You'd *have* to use the @Param so
as to be able to reference them individually (via the #{} notation) and then
you wouldn't specify the parameterType attribute, right?

The docs indicate that #{} causes the use of prepared statements and ${}
does direct string injection (e.g., for use in ORDER BY clauses) but I'm
find it it tough to wrap my head around both these syntaxes when using
mapper interfaces. What if my mapper method takes in a bean, could my sql
do something like:

select * from foo where foo_col = #{some_val} order by ${some_col}

If so, what does my mapper method look like? Does it need @Param? If so,
would the syntax have to be be #{bean.someVal} and ${someCol}?

Thanks.




--
View this message in context: http://mybatis-user.963551.n3.nabble.com/To-Param-or-not-to-Param-That-is-the-question-tp4026853.html
Sent from the mybatis-user mailing list archive at Nabble.com.

Frank Martínez

unread,
Apr 18, 2013, 1:11:10 PM4/18/13
to mybati...@googlegroups.com
Hi

- If your mapper method siganture has more tan one argument, you should use @Param to give a name to each one.
- If your mapper method siganture has just one argument, @Param is not required and:
  - if the argument has a TypeHandler you can refer to it as ${value} or #{value} depending of your intention
  - if the argument doesn't have a TypeHandler, you can refer to it as ${_parameter} or to its attributes directly as #{attrX}

Cheers,

Frank.




--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mybatis-user...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.





--
Frank D. Martínez M.

Eduardo Macarron

unread,
Apr 18, 2013, 1:15:11 PM4/18/13
to mybatis-user
Note that if it has a typehandler you can refer to it with any string
you want like #{id}, #{value}, #{blahblah}... but... in the case of
dymamic params you can _only_ use ${value}

2013/4/18 Frank Martínez <mnes...@gmail.com>:

icfantv

unread,
Apr 18, 2013, 1:15:55 PM4/18/13
to mybati...@googlegroups.com
Ok, that makes sense.

What about if I have multiple mapper parameters and they are beans? I
reference it via param_name.method_name (sans "get" or "is") correct?



--
View this message in context: http://mybatis-user.963551.n3.nabble.com/To-Param-or-not-to-Param-That-is-the-question-tp4026853p4026856.html

icfantv

unread,
Apr 18, 2013, 1:18:43 PM4/18/13
to mybati...@googlegroups.com
@eduardo, when you say type handler, are you referring to the parameterType
and resultType or are you referring to the TypeHandler interface?

Also, what do you mean by "dynamic params?"



--
View this message in context: http://mybatis-user.963551.n3.nabble.com/To-Param-or-not-to-Param-That-is-the-question-tp4026853p4026857.html

Eduardo Macarron

unread,
Apr 18, 2013, 1:40:47 PM4/18/13
to mybatis-user
if you have multple parameters that are beans then yes, you access
them that way.

Regarding the "dynamic params". Suppose this statement

select * from ${table} where value=#{value}

The ${table} is a dynamic param, it can hold any value and mybatis
will replace the ${table} with that value.

the #{value} is different. MyBatis will change it by a ?. And the
value will not be appended but passed to the statement using a
typehandler. The typehandler depends on the type of #{value}.


2013/4/18 icfantv <adam.n...@gmail.com>:

icfantv

unread,
Apr 18, 2013, 2:19:28 PM4/18/13
to mybati...@googlegroups.com
Reply all
Reply to author
Forward
Message has been deleted
0 new messages