簡體   English   中英

Mybatis-從SELECT返回包含Hashmap的對象

[英]Mybatis - Returning Object containing Hashmap from SELECT

我正在使用Mybatis-3,並且遇到以下情況:

我有類-用戶,如下所示:

public class User extends GeneralDto {

    private String userId;
    private String email;
    private String firstName;
    private String lastName;
    private long creationTimestamp;
    private long updateTimestamp;
    private List<String> tags;
    private HashMap<String, String> attributes;
    private HashMap<String, String> accounts;

    get and set to all + equals + hashcode.
}

這兩個哈希圖包含未知的鍵和String類型的值(這使我很麻煩)。

我已經使用插入方法將此類映射到表中。 並將其映射到以下表方案:

User (userIdentity, userId, email, firstName, lastName, creationTimestamp, updateTimestamp)

UserAttribute (userIdentity, attributeName, attributeValue, creationTimestamp, updateTimestamp)

UserTag (userIdentity, tagName, creationTimestamp, updateTimestamp)

UserAccount (userIdentity, accountIdentity, role, creationTimestamp, updateTimestamp)

我需要創建一個GET方法。 該方法接收UserKey對象,該對象包含作為用戶密鑰的userId。 並返回用戶類的實例。

這是SELECT語句,它連接所有表並從每個表中獲取相關數據:

<select id="getUser" parameterType="com.intel.aa.iot.mybatis.UserResultHandler" resultMap="userResultMap" resultOrdered="true">
    SELECT
        U.userId as userId,
        U.email as email,
        U.firstName as firstName,
        U.lastName as lastName,
        U.creationTimestamp as creationTimestamp,
        U.updateTimestamp as updateTimestamp,
        UT.tagName as tagName,
        UAT.attributeName as attributeName,
        UAT.attributeValue as attributeValue,
        A.accountId as accountId,
        UAC.role as role
    FROM User U
        LEFT OUTER JOIN UserTag UT ON U.userIdentity = UT.userIdentity
        LEFT OUTER JOIN UserAttribute UAT ON U.userIdentity = UAT.userIdentity
        LEFT OUTER JOIN UserAccount UAC ON U.userIdentity = UAC.userIdentity
        LEFT OUTER JOIN ACCOUNTS A ON UAC.accountIdentity = A.accountIdentity
    WHERE U.userId = #{userKey.userId}

由於存在聯接,此查詢可能返回多個行,但是所有行都是給定的userId。

我的問題是如何將其映射到作為用戶類實例的一個結果中。 我嘗試使用結果圖,但在映射哈希圖時遇到問題。 然后,我嘗試使用ResultHandler-返回名為UserProperties的類,該類將每個哈希圖包含為兩個列表(請參見下面的代碼),但不幸的是,它也無法正常工作-最終僅為每個列表保存了一個值。

UserProperties類:

public static class UserProperties {
    private User user;
    private List<String> attributeNames;
    private List<String> attributeValues;
    private List<String> accountIds;
    private List<String> accountRoles;

    get and set to all
}

的ResultMap:

<resultMap id="userResultMap" type="com.intel.aa.iot.mybatis.UserMapper$UserProperties">
    <association property="user" javaType="com.intel.aa.iot.dto.User">
        <id property="userId" column="userId"/>
        <result property="email" column="email"/>
        <result property="firstName" column="firstName"/>
        <result property="lastName" column="lastName"/>
        <result property="creationTimestamp" column="creationTimestamp"/>
        <result property="updateTimestamp" column="updateTimestamp"/>
        <collection property="tags" javaType="java.util.ArrayList" column="tagName" ofType="java.lang.String"/>
    </association>
    <collection property="attributeNames"  javaType="java.util.ArrayList" column="attributeName" ofType="java.lang.String"/>
    <collection property="attributeValues" javaType="java.util.ArrayList" column="attributeValue" ofType="java.lang.String"/>
    <collection property="accountIds" javaType="java.util.ArrayList" column="accountId" ofType="java.lang.String"/>
    <collection property="accountRoles" javaType="java.util.ArrayList" column="role" ofType="java.lang.String"/>
</resultMap>

我如何處理這些Hasmap?

謝謝!

在過去的幾年中,我本人也遇到過同樣的問題,但是我還沒有找到理想的解決方案。 最終,問題在於mybatis不允許您指定哈希圖的鍵-它使用結果屬性名稱作為鍵。

為解決此問題,我采取的兩種方法遠非完美,但如果它們對您有用,我將在此處概述它們:

  1. 使用結果處理程序。 我看到您說您已經嘗試過此操作,但僅獲得了第一個價值-您是否有機會共享您的代碼? 查詢中返回的每一行都會調用一次結果處理程序,因此您不想每次都創建一個新對象。 僅當您沒有與該行中的ID匹配的對象時,才想創建一個對象,然后根據以下各行迭代地構建它。

  2. 將名稱和值構建為哈希表列表,然后使用選擇攔截器(mybatis插件)調用對象內部的方法來構造您的真實哈希表。 為此,在結果圖中,您需要在類型hashmap的關聯下指定名稱和值結果。 然后,Mybatis將把它變成一個看起來像這樣的列表:

    [列表[Hashmap {“ attributeName” = name1,“ attributeValue” = value1}],[Hashmap {“ attributeName” = name2,“ attributeValue” = value2}]

然后,您可以在攔截器中調用在該哈希表上進行迭代的方法,以構建所需的哈希表。

兩種解決方案都不是很漂亮。 第一個解決方案失去了結果圖的優雅,第二個解決方案弄臟了您的域對象。 但是,如果您對更多詳細信息感興趣,請告訴我,我將添加一些代碼示例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM