Difference between MyBatis @Results and @ResultMap Annotation




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.





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





Replied on June 12, 2015
See also https://github.com/mybatis/mybatis-3/issues/155



Replied on August 01, 2019
Hello

So using @ResultMap or @Results give the same result ? It's just the way to make it that is different ?

Thanks


Write Answer










©2024 concretepage.com | Privacy Policy | Contact Us