Mybatis查询结果列表嵌套列表

笔记:

使用mybatis查询一列表,结果为List<Map<String,Object>>,列表内Map又包含一个列表.

使用mybatis查询的话,大致就是下边这个样子。

注:结果中的fId、ids需唯一

<!-- 主查询 -->
<select id="name" parameterType="Map" resultMap="ResultMap">
      select t.id as `id`,
             t.fId as `fId`,
             t.id as `ids`,
             t.name as `name`
       from test t
</select>

<!-- 封装查询结果 -->
<resultMap id="ResultMap" type="Map" >
   <collection property="list1" column="fId" javaType="ArrayList" select="name1"/> 
   <collection property="list2" column="ids" javaType="ArrayList" select="name2"/> 
</resultMap>

<!-- 子查询列表1 -->
<select id="name1" parameterType="String" resultType="Map">
       select t.id as `id`,
              t.name as `name`
       from test1 t
       <trim prefix="WHERE" prefixOverrides="AND|OR">
            <if test="value != null">
                  and t.fId = #{value}
            </if>
       </trim>
</select>

<!-- 子查询列表2 -->
  <select id="name2" parameterType="String" resultType="Map">
       select t.id as `id`,
              t.name as `name`
       from test2 t
       <trim prefix="WHERE" prefixOverrides="AND|OR">
            <if test="value != null">
                  and t.id = #{value}
            </if>
       </trim>
</select>

 查询结果为:List-->

                           Map-->

                                      id

                                      list1-->

                                                 Map-->

                                                            id

                                                            name

                                      list2-->

                                                 Map-->

                                                            id

                                                            name

                                      name

                           Map-->....

                                      ....

猜你喜欢

转载自g0314.iteye.com/blog/1672677
今日推荐