我用的MyBatis-3的工作,我有以下情況:的MyBatis - 返回從SELECT
我有類 - 用戶看起來如下:
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.
}
兩個包含HashMap包含String類型的未知鍵和值(它們讓我很麻煩)。
我已經使用insert方法將此類映射到表中。並將其映射到以下表格方案:
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方法。該方法接收包含作爲用戶密鑰的userId的UserKey對象。並返回用戶類的一個實例。
這是連接所有表,並會從每個相關數據的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的類,其中包含每個hashmap作爲兩個列表(請參閱下面的代碼),但不幸的是它並不工作 - 每個列表最終只保存一個值。
的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>
什麼是我處理這些Hasmaps方式?
謝謝!
感謝你們,我終於使用了UserProperties類,然後在mybatis之外創建了類User的一個實例。正如你所說,這個解決方案不太好或不好。你的第二個建議似乎很有趣,但我必須使用特定的API,所以我不能將散列映射更改爲散列映射列表。 – Yuval 2014-09-14 05:44:35