|
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? |
|
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? |
|
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? |
| Powered by Nabble | Edit this page |
