How do I use multiple COMPLEX parameters in a mapper?

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

How do I use multiple COMPLEX parameters in a mapper?

Alvaro Munoz Perez
So far I have seen many examples on how to use multiple parameters in a mapper, but all them are for simple objects (as in https://github.com/mybatis/mybatis-3/wiki/FAQ ).  But what about if we want to pass multiple complex objects?   Is that possible? How would you use them? 

Example: 
Supposing that we have an entity User, with:  name, address, passport....  And an entity Book with title, autor, editor....  

import org.apache.ibatis.annotations.Param;
public interface UserMapper {
   User selectUser(@Param("user") User user, @Param("book") Book book);
}
Would it be possible to do the following? Or it is not, since the parameterType is map, and myBatis does not know where to search our entities?
<select id=”selectUser” resultType=”User”>
  select id, username, hashedPassword
  from some_table
  where username = #{User.name}
  and title = #{Book.title}
</select>

--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

RE: How do I use multiple COMPLEX parameters in a mapper?

Poitras Christian

Hi,

 

Your exemple should work. Use lower case like #{user.name} since your @Param value is lower case.

 

MyBatis treats maps like beans where key are properties. The only difference is that there is no validation for non-exiting keys while giving an invalid property name will throw an execption.

 

Christian

 

De : [hidden email] [mailto:[hidden email]] De la part de Alvaro Munoz Perez
Envoyé : January-21-15 2:42 AM
À : [hidden email]
Objet : How do I use multiple COMPLEX parameters in a mapper?

 

So far I have seen many examples on how to use multiple parameters in a mapper, but all them are for simple objects (as in https://github.com/mybatis/mybatis-3/wiki/FAQ ).  But what about if we want to pass multiple complex objects?   Is that possible? How would you use them? 

 

Example: 

Supposing that we have an entity User, with:  name, address, passport....  And an entity Book with title, autor, editor....  

 

import org.apache.ibatis.annotations.Param;
public interface UserMapper {
   User selectUser(@Param("user") User user, @Param("book") Book book);
}
Would it be possible to do the following? Or it is not, since the parameterType is map, and myBatis does not know where to search our entities?
<select id=”selectUser” resultType=”User”>
  select id, username, hashedPassword
  from some_table
  where username = #{User.name}
  and title = #{Book.title}
</select>

--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: How do I use multiple COMPLEX parameters in a mapper?

Alvaro Munoz Perez
In reply to this post by Alvaro Munoz Perez
Thank you Christian.

I got a stupid question then: 

Why if you pass more than one complex parameter you don't need to specify the ParameterType, but if you only use one complex parameter you need to specify its package location using ParameterType ?  

I know that in the case of multiple, MyBatis will automatically do a map.. But how does it know where to pick the entities from if you haven't specified it anywhere as you do when you only pass one? 


On Wednesday, January 21, 2015 at 8:42:09 AM UTC+1, Alvaro Munoz Perez wrote:
So far I have seen many examples on how to use multiple parameters in a mapper, but all them are for simple objects (as in <a href="https://github.com/mybatis/mybatis-3/wiki/FAQ" target="_blank" rel="nofollow" onmousedown="this.href='https://www.google.com/url?q\75https%3A%2F%2Fgithub.com%2Fmybatis%2Fmybatis-3%2Fwiki%2FFAQ\46sa\75D\46sntz\0751\46usg\75AFQjCNHaOYIQTxpdYFb5q5V8gi75i_Hh4g';return true;" onclick="this.href='https://www.google.com/url?q\75https%3A%2F%2Fgithub.com%2Fmybatis%2Fmybatis-3%2Fwiki%2FFAQ\46sa\75D\46sntz\0751\46usg\75AFQjCNHaOYIQTxpdYFb5q5V8gi75i_Hh4g';return true;">https://github.com/mybatis/mybatis-3/wiki/FAQ ).  But what about if we want to pass multiple complex objects?   Is that possible? How would you use them? 

Example: 
Supposing that we have an entity User, with:  name, address, passport....  And an entity Book with title, autor, editor....  

import org.apache.ibatis.annotations.Param;
public interface UserMapper {
   User selectUser(@Param("user") User user, @Param("book") Book book);
}
Would it be possible to do the following? Or it is not, since the parameterType is map, and myBatis does not know where to search our entities?
<select id=”selectUser” resultType=”User”>
  select id, username, hashedPassword
  from some_table
  where username = #{User.name}
  and title = #{Book.title}
</select>

--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: How do I use multiple COMPLEX parameters in a mapper?

Ray Sprinkle

You don't. Parameter type is no longer ever required.

On Jan 22, 2015 6:12 AM, "Alvaro Munoz Perez" <[hidden email]> wrote:
Thank you Christian.

I got a stupid question then: 

Why if you pass more than one complex parameter you don't need to specify the ParameterType, but if you only use one complex parameter you need to specify its package location using ParameterType ?  

I know that in the case of multiple, MyBatis will automatically do a map.. But how does it know where to pick the entities from if you haven't specified it anywhere as you do when you only pass one? 


On Wednesday, January 21, 2015 at 8:42:09 AM UTC+1, Alvaro Munoz Perez wrote:
So far I have seen many examples on how to use multiple parameters in a mapper, but all them are for simple objects (as in https://github.com/mybatis/mybatis-3/wiki/FAQ ).  But what about if we want to pass multiple complex objects?   Is that possible? How would you use them? 

Example: 
Supposing that we have an entity User, with:  name, address, passport....  And an entity Book with title, autor, editor....  

import org.apache.ibatis.annotations.Param;
public interface UserMapper {
   User selectUser(@Param("user") User user, @Param("book") Book book);
}
Would it be possible to do the following? Or it is not, since the parameterType is map, and myBatis does not know where to search our entities?
<select id=”selectUser” resultType=”User”>
  select id, username, hashedPassword
  from some_table
  where username = #{User.name}
  and title = #{Book.title}
</select>

--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.