Can resultMap defined by @Results annotation be reused?

211 views
Skip to first unread message

Wei Lin

unread,
May 20, 2021, 12:37:06 AM5/20/21
to mybatis-user
If <resultMap> is defined in xml, it could be reused by referencing, like:

<select id="selectUsers" parameterType="int" resultMap="userResultMap">

But when using @Results annotation, even an id is given like the following (copied from https://mybatis.org/mybatis-3/java-api.html):

@Results(id = "userResult", value = { @Result(property = "id", column = "uid", id = true), @Result(property = "firstName", column = "first_name"), @Result(property = "lastName", column = "last_name") }) @Select("select * from users where id = #{id}") User getUserById(Integer id);

Is it possible to reuse this "userResult" somwhere else?

Jeff Butler

unread,
May 20, 2021, 6:28:24 AM5/20/21
to mybati...@googlegroups.com
You can use the @ResultMap annotation to reference another result map - @ResultMap("userResult")

Jeff Butler

 

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mybatis-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/32f92d2a-14cc-420c-9268-849356680afan%40googlegroups.com.

Wei Lin

unread,
May 24, 2021, 7:53:06 AM5/24/21
to mybatis-user
Thanks for the information.

By the way, it seem currently the target for @Results is method, but intuitively I think it should be on a mapper interface (or maybe a wider scope), because in xml <resultMap> is not declared at a specific sql statement, but referenced by the latter with an ID. Any ideas?

Jeff Butler

unread,
May 24, 2021, 8:39:41 AM5/24/21
to mybati...@googlegroups.com
You can reference a resultMap defined in XML. Use the ID of the <resultMap>. There is a Kotlin example here: https://github.com/jeffgbutler/mybatis-kotlin-examples/tree/master/src/main/kotlin/example04

If your Java interface is in a different package than the namespace declared in XML, then use the fully qualified ID in the annotation like @ResultMap("foo.BarResult")

Jeff Butler


Reply all
Reply to author
Forward
0 new messages