简体   繁体   English

如何使用另一个表的键从 myBatis 获取 Map?

[英]How to get Map from myBatis with a key of another table?

I have a request, which gives me one object of Advantage class:我有一个请求,它给了我一个 object 的优势 class:

<resultMap id="AdvantageResult" type="Advantage">
        <id property="id" jdbcType="BIGINT" javaType="java.lang.Long" column="id"/>
        <result property="code" column="code"/>
        <result property="name" column="name"/>
        <result property="description" column="description"/>
        <result property="asIs" column="as_is"/>
        <result property="toBe" column="to_be"/>
        <result property="availableName" column="available_name"/>
        <result property="availableNameShort" column="available_name_short"/>
        <result property="availableDescription" column="available_description"/>
        <result property="availableDescriptionShort" column="available_description_short"/>
        <result property="activeName" column="active_name"/>
        <result property="activeNameShort" column="active_name_short"/>
        <result property="activeDescription" column="active_description"/>
        <result property="activeDescriptionShort" column="active_description_short"/>
    </resultMap>

Here is my request, where I use the map:这是我的请求,我使用 map:

<select id="findAdvantageByLoyaltyAndConfigDetailId" resultMap="AdvantageResult">
        select  a.id, a.code, a.name, a.description, a.as_is, a.to_be,
                a.available_name, a.available_name_short, a.available_description, a.available_description_short,
                a.active_name, a.active_name_short,  a.active_description, a.active_description_short
        from advantage a
                 left join detail_advantage da on da.advantage_id = a.id
        where da.config_detail_id = #{configDetailId}
    </select>

I want to get Map <Long, Advantage>, where the long key will be the param #{configDetailId} How should I rewrite the mapper?我想获取 Map <Long, Advantage>,其中长键将是参数 #{configDetailId} 我应该如何重写映射器?

I could think of two approaches.我可以想到两种方法。

  1. Convert the returned Advantage to Map in Java code.将返回的Advantage转换为Map代码中的 Map。
    Defining a simple default method in the Java mapper interface should be sufficient.在 Java 映射器接口中定义一个简单的默认方法就足够了。

     Advantage internalSelect(Long configDetailId); default Map<Long, Advantage> select(Long configDetailId) { return Map.of(configDetailId, internalSelect(configDetailId)); }
  2. Add a private field to Advantage that holds configDetailId and use @MapKey .Advantage添加一个包含configDetailId并使用@MapKey的私有字段。

     private Long configDetailId;

    Then include the parameter to the column list of the select and the result map.然后将参数包含到 select 和结果 map 的列列表中。

     select... , #{configDetailId} as configDetailId from...
     <result property="configDetailId" column="configDetailId" />

    Then add @MapKey to your Java mapper interface.然后将@MapKey添加到您的 Java 映射器接口。

     @MapKey("configDetailId") Map<Long, Advantage> findAdvantageByLoyaltyAndConfigDetailId(Long configDetailId);

@MapKey is handy when there is an existing property for the 'key'.当“键”存在现有属性时, @MapKey很方便。
Otherwise, I would recommend the first approach.否则,我会推荐第一种方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM