Home  >  Forum  >  MyBatis
Post New Query

Difference between MyBatis @Results and @ResultMap Annotation


joined on
November 09,2013
Asked on March 18,2015
I am learning MyBatis using annotation. I am getting two annotation as @Results and @ResultMap. Need to understand what is difference between them.


joined on
March 14,2013
Replied on March 18,2015

@Results : Use this annotation to map table column with java property that helps to get result in @Select annotation.

@Results({
        @Result(property = "user_id", column = "id"),
        @Result(property = "firstName", column = "first_name"),

})
@Select("SELECT id, name, district from user WHERE id = #{id}")
User selectUser(int id);
 

@ResultMap : Using this annotation, we refer resultmap id. 

Suppose in mapper xml, we have a resultmap tag as

<resultMap id="userResult" type="village">
    <id property="id" column="user_id" />
    <result property="firstName" column="first_name"/>
</resultMap>

We can use it with @ResultMap as

@ResultMap("userResult")
@Select("SELECT id, name, district from user WHERE id = #{id}")
User selectUser(int id);


joined on
June 12,2015
Replied on June 12,2015

See also https://github.com/mybatis/mybatis-3/issues/155

Write Answer









Copyright ©2017 concretepage.com, all rights reserved |Privacy Policy | Contact Us