Quantcast

@Select referencing select defined in mapper XML file

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

@Select referencing select defined in mapper XML file

MaximDim
Hi there,

I have a select defined in mapper.xml file:

=============================================================================
<mapper namespace="foo">

  <select id="selectWithRoles" parameterType="String"
resultMap="personWithRoles">
    select *
    from
    PERSON P
    left outer join PERSON_ROLE R on P.ID = R.PERSON_ID
    where P.ID = #{id}
  </select>

  <resultMap id="personWithRoles" type="Person">
    <id property="id" column="ID" />
    <collection property="roles" ofType="Role">
      <id property="personId" column="PERSON_ID"/>
      <result property="name" column="NAME"/>
    </collection>
  </resultMap>

</mapper>
=============================================================================

and I want to expose this select via DAO Interface through
annotation(s) without copying select statements into DAO. Following
works fine:

@Select("select * from PERSON P left outer join PERSON_ROLE R on P.ID
= R.PERSON_ID where P.ID = #{id}")
@ResultMap("personWithRoles")
public Person loadByIdWithRoles(String id);

But I don't like copying SQL into annotation (could be pretty long)
want something like this:

@Select("selectWithRoles")
public Person loadByIdWithRoles(String id);

where "selectWithRoles" is id of select defined in XML. Is it
possible?
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: @Select referencing select defined in mapper XML file

Jeff Butler
There is no annotation for this, but you don't need it.  As long as
the namespace in the XML file matches the fully qualifies name of your
interface AND the method name matches the id in XML, then MyBatis will
magically make it work with no annotations.

Jeff Butler


On Sat, Mar 24, 2012 at 7:33 PM, MaximDim <[hidden email]> wrote:

> Hi there,
>
> I have a select defined in mapper.xml file:
>
> =============================================================================
> <mapper namespace="foo">
>
>  <select id="selectWithRoles" parameterType="String"
> resultMap="personWithRoles">
>    select *
>    from
>    PERSON P
>    left outer join PERSON_ROLE R on P.ID = R.PERSON_ID
>    where P.ID = #{id}
>  </select>
>
>  <resultMap id="personWithRoles" type="Person">
>    <id property="id" column="ID" />
>    <collection property="roles" ofType="Role">
>      <id property="personId" column="PERSON_ID"/>
>      <result property="name" column="NAME"/>
>    </collection>
>  </resultMap>
>
> </mapper>
> =============================================================================
>
> and I want to expose this select via DAO Interface through
> annotation(s) without copying select statements into DAO. Following
> works fine:
>
> @Select("select * from PERSON P left outer join PERSON_ROLE R on P.ID
> = R.PERSON_ID where P.ID = #{id}")
> @ResultMap("personWithRoles")
> public Person loadByIdWithRoles(String id);
>
> But I don't like copying SQL into annotation (could be pretty long)
> want something like this:
>
> @Select("selectWithRoles")
> public Person loadByIdWithRoles(String id);
>
> where "selectWithRoles" is id of select defined in XML. Is it
> possible?
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: @Select referencing select defined in mapper XML file

MaximDim
Aha. Pretty cool. Thanks, I'll give it a try.

On Mar 25, 5:42 pm, Jeff Butler <[hidden email]> wrote:

> There is no annotation for this, but you don't need it.  As long as
> the namespace in the XML file matches the fully qualifies name of your
> interface AND the method name matches the id in XML, then MyBatis will
> magically make it work with no annotations.
>
> Jeff Butler
>
>
>
>
>
>
>
> On Sat, Mar 24, 2012 at 7:33 PM, MaximDim <[hidden email]> wrote:
> > Hi there,
>
> > I have a select defined in mapper.xml file:
>
> > =========================================================================== ==
> > <mapper namespace="foo">
>
> >  <select id="selectWithRoles" parameterType="String"
> > resultMap="personWithRoles">
> >    select *
> >    from
> >    PERSON P
> >    left outer join PERSON_ROLE R on P.ID = R.PERSON_ID
> >    where P.ID = #{id}
> >  </select>
>
> >  <resultMap id="personWithRoles" type="Person">
> >    <id property="id" column="ID" />
> >    <collection property="roles" ofType="Role">
> >      <id property="personId" column="PERSON_ID"/>
> >      <result property="name" column="NAME"/>
> >    </collection>
> >  </resultMap>
>
> > </mapper>
> > =========================================================================== ==
>
> > and I want to expose this select via DAO Interface through
> > annotation(s) without copying select statements into DAO. Following
> > works fine:
>
> > @Select("select * from PERSON P left outer join PERSON_ROLE R on P.ID
> > = R.PERSON_ID where P.ID = #{id}")
> > @ResultMap("personWithRoles")
> > public Person loadByIdWithRoles(String id);
>
> > But I don't like copying SQL into annotation (could be pretty long)
> > want something like this:
>
> > @Select("selectWithRoles")
> > public Person loadByIdWithRoles(String id);
>
> > where "selectWithRoles" is id of select defined in XML. Is it
> > possible?
Loading...