dynamic ResultMap

199 views
Skip to first unread message

ningpp

unread,
Oct 5, 2021, 11:54:21 PM10/5/21
to mybatis-user
with SelectProvider and dynamic-sql, i can build a very complex query sql by user query,
but i can not dynamic  build the ResultMap .
 : 
```
class EmployeeQuery {
    //when this is true, i join hobby table in sql
    private boolean withHobby;
    //when this is true, i join skill table in sql
    private boolean withSkill;

    private List<HobbyVO> hobbys;
    private List<SkillVO> skills;

    //i have almost ten withTable flag
    //every withTable flag do a join.
}

when withHobby is true  and  withSkill is true
the result map like this:
<resultMap id="vo_with_skill_hobby_ResultMap" type="xxx.EmployeeVO">

    <id column="employee_id" jdbcType="VARCHAR" property="id" />
    <result column="employee_name" jdbcType="VARCHAR" property="name" />
    <result column="employee_gender" jdbcType="INTEGER" property="gender" />

    <collection property="hobbys" ofType="xxx.HobbyVO">
        <id column="hobby_id" property="id" />
        <result column="hobby_name" jdbcType="VARCHAR" property="name" />
    </collection>

    <collection property="skills" ofType="xxx.SkillVO">
        <id column="skill_id" property="id" />
        <result column="skill_name" jdbcType="VARCHAR" property="name" />
        <result column="skill_category_id" jdbcType="VARCHAR" property="categoryId" />
    </collection>
</resultMap>
```

i read the source code, i found resutlmap build in init(annotation or xml).
is there anyway to build the resultmap dynamicly?

Guy Rouillier

unread,
Oct 6, 2021, 1:59:32 AM10/6/21
to mybati...@googlegroups.com
Did you take a look at ResultHandler?  See discussion here:


with the following comment: "How about specifying resultType="map" and building the result object using ResultHandler?"
--
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/573d386d-490d-4f72-b159-cf92be9f3446n%40googlegroups.com.
-- 
Guy Rouillier

Jeff Butler

unread,
Oct 6, 2021, 7:31:19 AM10/6/21
to mybati...@googlegroups.com
Currently there is no way to use a dynamic result map. One way to work around this is to use a return type of List<Map<String, Object>> - this can be used with any query and provides a raw Map of each row from the query. You could then translate this map into an object tree of your choice.

MyBatis Dynamic SQL includes a utility interface org.mybatis.dynamic.sql.util.mybatis3.CommonSelectMapper that you can use for this purpose. If you inject this interface into a MyBatis configuration you can use it for any query.

Jeff Butler


--
Reply all
Reply to author
Forward
0 new messages