我有一个要求 im Mybatis 3,将包含连接的选择查询的结果转换为嵌套的哈希图结果。
例如,考虑以下 mybatis 映射器条目:
<select
id="testCaseSelect"
fetchSize="1000"
flushCache="false"
resultMap="testCaseResultMap" resultType="list">
SELECT TESTCASE_ID,
TC.TITLE,
TC.DESCRIPTION ,
TC.CREATION_DATE ,
TCS.STATUS_NAME,
TCP.PRIORITY_NAME ,
TCCBY.FIRST_NAME,
TCCBY.LAST_NAME,
TCST.SEQUENCE_NO ,
TCST.DESCRIPTION ,
TCST.EXPECTED_RESULT ,
TCCMNTS.COMMENT_TEXT ,
TCCMNTS.LOGGING_DATE
FROM TESTCASE TC
INNER JOIN TESTCASE_STATUS TCS
ON TC.STATUS_ID=TCS.STATUS_ID
INNER JOIN TESTCASE_PRIORITY TCP
ON TC.PRIORITY_ID=TCP.PRIORITY_ID
INNER JOIN USERS TCCBY
ON TC.CREATED_BY=TCCBY.USER_ID
LEFT JOIN TESTCASE_STEPS TCST
ON TC.TESTCASE_ID=TCST.TESTCASE_ID
LEFT JOIN TESTCASE_COMMENTS TCCMNTS
ON TC.TESTCASE_ID=TCCMNTS.TESTCASE_ID WHERE TESTCASE_ID BETWEEN 326780 AND 326800
</select>
<resultMap
id="testCaseResultMap"
autoMapping="true"
type="hashMap" >
<result
column="TESTCASE_ID"
property="tcId" />
<result
column="TITLE"
property="title" />
<result
column="DESCRIPTION"
property="description" />
<result
column="CREATION_DATE"
property="createdOn" />
<association
columnPrefix="TCS"
javaType="hashMap"
property="testCaseStatus" >
<id
column="STATUS_ID"
property="id" />
<result
column="STATUS_NAME"
property="status" />
</association>
<association
columnPrefix="TCP"
javaType="hashMap"
property="testCasePriority" >
<id
column="PRIORITY_ID"
property="id" />
<result
column="PRIORITY_NAME"
property="priority" />
</association>
<association
columnPrefix="TCCBY"
javaType="hashMap"
property="testCaseCreator" >
<id
column="USER_ID"
property="id" />
<result
column="FIRST_NAME"
property="firstName" />
<result
column="LAST_NAME"
property="lastName" />
</association>
<collection
columnPrefix="TCST"
javaType="hashMap"
ofType="list"
property="testCaseSteps" >
<id
column="STEP_ID"
property="id" />
<result
column="SEQUENCE_NO"
property="stepNo" />
<result
column="description"
property="step" />
<result
column="expected_result"
property="result" />
</collection>
<collection
columnPrefix="TCCMNTS"
javaType="hashMap"
ofType="list"
property="testCaseComments" >
<id
column="COMMENT_ID"
property="id" />
<result
column="COMMENT_TEXT"
property="comment" />
<result
column="LOGGING_DATE"
property="commentedOn" />
</collection>
</resultMap>
测试用例对象有一个优先级对象,由对象、状态对象、步骤集合和注释集合创建。
public static void main(String[] args)
{
SqlSessionFactory factory = getSqlSessionFactory();
SqlSession session = factory.openSession();
List<Map> list = session.selectList("testCaseSelect");
System.out.print(Arrays.toString(list.toArray()));
}
当我运行上面的代码时,我得到一个包含 34 个哈希映射的列表,每个返回的行都有一个。每个 hashmap 包含 column_name 及其值作为键值对。
我想要的是有一个具有以下结构的哈希映射:
root(hashMap)=>{
tcid(String),
title(String),
description(String),
createdOn(String),
testCaseStatus(hashMap)=>{
status(String)
},
testCasePriority(hashMap)=>{
priority(String)
},
testCaseCreator(hashMap)=>{
firstName(String),
lastName(String)
},
testCaseSteps(list of hashMap):[{
stepno(String),
step(String),
result(String),
}],
testCaseComments(list of hashMap):[{
comment(String),
commentedOn(String)
}],
}
在mybatis中可以实现吗?