mybatis一对多的查询的resultMap 及insert

tech2025-07-16  2

<resultMap id ="indicatorCardOverviewResultMap"  type="com.dbvs.IndicatorCardOverviewDTO">

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

 <result column="card_name" property= "cardName" />

 <result column="date_type" property= "dateType" />

 <result column="updated_time" property= "updatedTime" />

<!--用id分组 用ofType这样的话分页器PageInfo失效,如果要生效需要都改为子查询select-->

<collection property="dimension"  ofType ="com.dbvs.DimensionDTO"  column ="id">

   <!-- DimensionDTO对象下面还有个List<String> codes 属性  用dimension_type分组-->

     <result column="dimension_type" property= "dimensionType" />

     <result column="dimension_name" property= "dimensionName" />

    <collection property="codes"  ofType ="java.lang.String"  column ="dimension_type">

        <result column="code"/>

    </collection>

  </collection>

<!--  IndicatorCardOverviewDTO的属性defaultDateType是一个对象,这个属性需要用id,去其他表查询  ,等于这里引用一个select子查询,id作为参数-->

<collection property="defaultDateType" 

        select ="com.mapper.IndicatorCardMapper.getDefaultDateType"  column ="id">

</collection>

</resultMap>

 

<resultMap id ="defaultDateTypeMap"  type="com.dbvs.DefaultDateType">

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

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

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

</resultMap>

 

<select id ="getDefaultDateType"> resultMap = "defaultDateTypeMap">

  select

  demision_type,

  demision_name,

  code

  from dimension

 where 

  card_id =#{id}

</select>

 

一对多insert  InsertIndicatorCardOverviewDTO有属性List<DimensionDTO> dimensin , DimensionDTO里面又有List<String> codes属性

<insert id="insertIndicatorCardOverviewForLimitDimension"  parameterType = "com.dbvs.InsertIndicatorCardOverviewDTO">

  <foreach collection = "dimension" item="item" index="index">

    <foreach collection ="item.codes" item ="code" index ="index" separator=";" open="" close =";">

    insert into ind_card_limit_dimension

    (

   card_limit_id,

   dimension_type,

  dimension_name,

  code

 )

 values

 (

 #{item.id},

 #{item.dimensionType},

 #{item.dimensionName},

 #{code}

</insert>

 

最新回复(0)