MyBatis之ResultMap

ResultMap 的属性列表

 

resultMap标签介绍

  • constructor - 用于在实例化类时,注入结果到构造方法中
    • idArg - ID 参数;标记出作为 ID 的结果可以帮助提高整体性能
    • arg - 将被注入到构造方法的一个普通结果
  • id – 一个 ID 结果;标记出作为 ID 的结果可以帮助提高整体性能,用于主键
  • result – 注入到字段或 JavaBean 属性的普通结果
  • association – 一个复杂类型的关联;许多结果将包装成这种类型
  • 嵌套结果映射 – 关联可以是 resultMap 元素,或是对其它结果映射的引用
  • collection – 一个复杂类型的集合
    • 嵌套结果映射 – 集合可以是 resultMap 元素,或是对其它结果映射的引用
  • discriminator – 使用结果值来决定使用哪个 resultMap
    • case – 基于某些值的结果映射.嵌套结果映射 – case 也是一个结果映射,因此具有相同的结构和元素;或者引用其它的结果映射

 

id & result标签参数详解

 

示例

<resultMap id="FeeInfoList" type="com.atguigu.lease.web.admin.vo.fee.FeeKeyVo">
        <id property="id" column="id"/>
        <result property="name" column="key_name"/>
        <collection property="feeValueList" ofType="com.atguigu.lease.model.entity.FeeValue">
            <id column="value_id" property="id"/>
            <result column="value_name" property="name"/>
            <result column="value_unit" property="unit"/>
            <result column="key_id" property="feeKeyId"/>
        </collection>
    </resultMap>

    <select id="listFeeInfo" resultMap="FeeInfoList">
        select k.id,
               k.name       key_name,
               v.id         value_id,
               v.name       value_name,
               v.unit       value_unit,
               v.fee_key_id key_id
        from fee_key k
                 left join fee_value v on k.id = v.fee_key_id and v.is_deleted = 0
        where k.is_deleted = 0
    </select>

其中:

public class FeeValue extends BaseEntity {

    private static final long serialVersionUID = 1L;

    @Schema(description = "费用value")
    @TableField(value = "name")
    private String name;

    @Schema(description = "收费单位")
    @TableField(value = "unit")
    private String unit;

    @Schema(description = "费用所对的fee_key编码")
    @TableField(value = "fee_key_id")
    private Long feeKeyId;

}


public class FeeKeyVo extends FeeKey {

    @Schema(description = "杂费value列表")
    private List<FeeValue> feeValueList;
}


public class FeeKey extends BaseEntity {

    private static final long serialVersionUID = 1L;

    @Schema(description = "付款项key")
    @TableField(value = "name")
    private String name;

}

结果会自动映射:

[
    {
      "id": 1,
      "name": "停车费",
      "feeValueList": [
        {
          "id": 3,
          "name": "400",
          "unit": "元/月",
          "feeKeyId": 1
        },
        {
          "id": 2,
          "name": "300",
          "unit": "元/月",
          "feeKeyId": 1
        },
        {
          "id": 1,
          "name": "200",
          "unit": "元/月",
          "feeKeyId": 1
        }
      ]
    },
    {
      "id": 2,
      "name": "网费",
      "feeValueList": [
        {
          "id": 7,
          "name": "500",
          "unit": "元/年",
          "feeKeyId": 2
        },
        {
          "id": 6,
          "name": "1000",
          "unit": "元/年",
          "feeKeyId": 2
        },
        {
          "id": 5,
          "name": "60",
          "unit": "元/月",
          "feeKeyId": 2
        },
        {
          "id": 4,
          "name": "50",
          "unit": "元/月",
          "feeKeyId": 2
        }
      ]
    }
]

 

posted @ 2024-06-18 17:40  r1-12king  阅读(44)  评论(0编辑  收藏  举报