2012-03-24 4 views
2

Ich habe eine select in mapper.xml-Datei definiert:Mybatis @Select Referenzierung wählen in Mapper XML-Datei definiert

<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> 

und ich möchte diese Auswahl über DAO-Schnittstelle durch Anmerkung belichten (n) ohne das Kopieren select-Anweisungen in DAO. Im Anschluss funktioniert:

@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); 

Aber Ich mag nicht SQL in Annotation Kopieren (könnte ziemlich lang sein) möchte etwas wie folgt aus:

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

wo "selectWithRoles" ist id ausgesuchter definiert in XML . Ist es möglich?

Antwort

2

Eine Antwort gefunden. Für den Fall, dass irgendjemand es brauchen würde: Solange der Namespace in der XML-Datei mit dem vollständig qualifizierten Namen der Schnittstelle übereinstimmt und der Methodenname mit der ID in XML übereinstimmt, wird MyBatis es auf magische Weise ohne Annotation funktionieren lassen.

<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> 

Beachten Sie, dass Paketname mit Namensraum entspricht in XML und Methodennamen übereinstimmt select id:

package foo; 
public Person public Person selectWithRoles(String id); 
+0

Das ist genau das, was ich suchte. Danke vielmals! – codematix