how to defint Multiple parameterType?

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

how to defint Multiple parameterType?

maven apache
<select id="selectPerson" parameterType="int" resultType="hashmap">
  SELECT * FROM PERSON WHERE ID = #{id}
</select>

This is the select config in the document, I wonder if the MyBatis
just support one parameterType?
For example, if I want to execute:
select * from user where id=2 name='name'
So what is the parameterType? int or string?
Reply | Threaded
Open this post in threaded view
|

Re: how to defint Multiple parameterType?

Thomas
  On 15-10-2010 17:00, maven apache wrote:
> <select id="selectPerson" parameterType="int" resultType="hashmap">
>    SELECT * FROM PERSON WHERE ID = #{id}
> </select>
>
> This is the select config in the document, I wonder if the MyBatis
> just support one parameterType?
> For example, if I want to execute:
> select * from user where id=2 name='name'
> So what is the parameterType? int or string?
You can wrap it in a java object and pass it with the parameterType:

<select id="selectPerson" parameterType="my.package.MyObject" resultType="hashmap">
   SELECT * FROM PERSON WHERE ID = #{theId} and NAME = #{theName}
</select>

And then create a plain java object with those values
package my.package;

class MyObject {
     Integer theId;
     String theName;
      ...
     setters/getters
}

Reply | Threaded
Open this post in threaded view
|

Re: how to defint Multiple parameterType?

Hunter
In MyBatis 3, you do not have to define a holder object for situations
like this. Instead, use the @Param annotation in your method calls.

Define a method like:
Person selectPerson(@Param("id") int id, @Param("name") String name)

And in the xml file:
<select id="selectPerson" resultType="Person">
   SELECT * FROM PERSON WHERE ID = #{id} and NAME = #{name}
</select>

On Oct 15, 11:15 am, Thomas <[hidden email]> wrote:

>   On 15-10-2010 17:00, maven apache wrote:> <select id="selectPerson" parameterType="int" resultType="hashmap">
> >    SELECT * FROM PERSON WHERE ID = #{id}
> > </select>
>
> > This is the select config in the document, I wonder if the MyBatis
> > just support one parameterType?
> > For example, if I want to execute:
> > select * from user where id=2 name='name'
> > So what is the parameterType? int or string?
>
> You can wrap it in a java object and pass it with the parameterType:
>
> <select id="selectPerson" parameterType="my.package.MyObject" resultType="hashmap">
>    SELECT * FROM PERSON WHERE ID = #{theId} and NAME = #{theName}
> </select>
>
> And then create a plain java object with those values
> package my.package;
>
> class MyObject {
>      Integer theId;
>      String theName;
>       ...
>      setters/getters
>
>
>
> }
Reply | Threaded
Open this post in threaded view
|

Re: how to defint Multiple parameterType?

maven apache
Thanks,sounds too Chaos(maybe flexiable)

2010/10/16 Hunter <[hidden email]>
In MyBatis 3, you do not have to define a holder object for situations
like this. Instead, use the @Param annotation in your method calls.

Define a method like:
Person selectPerson(@Param("id") int id, @Param("name") String name)

And in the xml file:
<select id="selectPerson" resultType="Person">
  SELECT * FROM PERSON WHERE ID = #{id} and NAME = #{name}
</select>

On Oct 15, 11:15 am, Thomas <[hidden email]> wrote:
>   On 15-10-2010 17:00, maven apache wrote:> <select id="selectPerson" parameterType="int" resultType="hashmap">
> >    SELECT * FROM PERSON WHERE ID = #{id}
> > </select>
>
> > This is the select config in the document, I wonder if the MyBatis
> > just support one parameterType?
> > For example, if I want to execute:
> > select * from user where id=2 name='name'
> > So what is the parameterType? int or string?
>
> You can wrap it in a java object and pass it with the parameterType:
>
> <select id="selectPerson" parameterType="my.package.MyObject" resultType="hashmap">
>    SELECT * FROM PERSON WHERE ID = #{theId} and NAME = #{theName}
> </select>
>
> And then create a plain java object with those values
> package my.package;
>
> class MyObject {
>      Integer theId;
>      String theName;
>       ...
>      setters/getters
>
>
>
> }

Reply | Threaded
Open this post in threaded view
|

Re: how to defint Multiple parameterType?

Thomas
In reply to this post by Hunter
Good to learn new stuff :-)

I tried using your suggestions, and it works to some extent.
The variables gets expanded for the prepared statement, using ie. #{name}, but when trying to expand the variable with ${name} (I have various reasons to do so, weird database design is one of them), I get a null pointer exception.
Is this a known 'feature' or just a bug that I should report somewhere?

This illustrates what I'm trying to do. It works when using a holder object, but not with the annotation way.

  <select id="someSql" resultMap="SomeResultMap">
          select * from myTable T where T.${name} = 'stuff' and T.value = #{name} and T.id = #{id}
  </select>

  public SomeResult someSql(@Param("name") String name, @Param("id") Integer id);

(the above is modified to suit the example, but the general purpose is real)

/Thomas



On Sat, Oct 16, 2010 at 2:43 AM, Hunter <[hidden email]> wrote:
In MyBatis 3, you do not have to define a holder object for situations
like this. Instead, use the @Param annotation in your method calls.

Define a method like:
Person selectPerson(@Param("id") int id, @Param("name") String name)

And in the xml file:
<select id="selectPerson" resultType="Person">
  SELECT * FROM PERSON WHERE ID = #{id} and NAME = #{name}
</select>

On Oct 15, 11:15 am, Thomas <[hidden email]> wrote:
>   On 15-10-2010 17:00, maven apache wrote:> <select id="selectPerson" parameterType="int" resultType="hashmap">
> >    SELECT * FROM PERSON WHERE ID = #{id}
> > </select>
>
> > This is the select config in the document, I wonder if the MyBatis
> > just support one parameterType?
> > For example, if I want to execute:
> > select * from user where id=2 name='name'
> > So what is the parameterType? int or string?
>
> You can wrap it in a java object and pass it with the parameterType:
>
> <select id="selectPerson" parameterType="my.package.MyObject" resultType="hashmap">
>    SELECT * FROM PERSON WHERE ID = #{theId} and NAME = #{theName}
> </select>
>
> And then create a plain java object with those values
> package my.package;
>
> class MyObject {
>      Integer theId;
>      String theName;
>       ...
>      setters/getters
>
>
>
> }