ibats不需要javabean靠map实现一对多例子配置

<sqlMap namespace="ProductClass">

      //产品小类结果集

<resultMap id="manyResult" class="java.util.HashMap">

<result property="xiaoid" column="xiaoid"/>

<result property="xiaoname" column="xiaoname"/>

<result property="parentid" column="parentid"/>

</resultMap>

    //产品大类结果集

<resultMap id="oneResult" class="java.util.HashMap">

<result property="daid" column="daid"  />

<result property="daname" column="daname" />

<result property="children" javaType="java.util.ArrayList" select="selectMany" column="daid" />

//这而的children属性javaType必须指定是java.util.List或java.util.Collection

</resultMap>

<select id="selectOne" resultMap="oneResult">

SELECT JZDL_DM as

daid,JZDL_MC

as daname from DM_JZDL

</select>

<select id="selectMany" resultMap="manyResult" parameterClass="string">

SELECT JZXL_DM as

xiaoid,JZXL_MC as xiaoname,JZDL_DM as parentid

from

DM_JZXL where

JZDL_DM=#daid#

</select>

 

</sqlMap>

 

 

<resultMap class="Department" id="dep" groupBy="id">

<result property="id" column="_id"/>

<result property="name" column="_name"/>

<result property="location" column="_location"/>

<result property="employees" resultMap="department.emp"/>

</resultMap>

<resultMap class="cn.com.legendapl.ibatis.domain.Employee" id="emp">

<result property="id" column="e_id"/>

<result property="name" column="e_name"/>

<result property="title" column="e_title"/>

</resultMap>

<select id="query" parameterClass="java.util.Map" resultMap="dep">

select

d._id,

d._name,

d._location,

e._id as e_id,

e._name as e_name,

e._title as e_title

from

t_dep as d

left join

t_emp as e

on

d._id = e._dep_id

<dynamic prepend="where">

<isNotEmpty property="id" prepend="and"> d._id = #id# </isNotEmpty>

</dynamic>

   </select>

 

 

 

在名为dep的resultMap中配置一项groupBy="id",这样,ibatis在处理结果集时,把id相同的几项“看成”一项来处理。 
注意:groupBy属性的配置,是指的映射到的模型的property name 而不是查询的结果集的列名。 

猜你喜欢

转载自tangguolong99.iteye.com/blog/1770773
今日推荐