mybatis中resultMap使用之返回分組資料
阿新 • • 發佈:2019-02-18
1. resultMap
1.1 引言
resultMap是mybatis最重要的強大元素。通過描述資料的關係結構,將結果集進行對映到java類或java bean中,達到結果集複雜處理的目的。本文解決的主要問題的分組資料的返回
1.2 問題
假設有如下sql查詢語句
select id, otherId from mytalbe
該sql查詢語句查詢結果如下
id otherId
01 00001
01 00002
01 00003
02 00004
有java實體類如下
class Id{ private String id; private List<String> otherId; }
怎樣通過一次查詢將結果以分組的形式放到List< Id >中呢?
通過執行一次查詢,返回如下形式的資料
List<Id> aa = new ArrayList<Id>();
aa = ...//通過mybatis執行slq
aa:[
{01,[00001,00002,00003,00004]},
{02,[00004]}
]
解決上述問題呢,就需要用到resultMap了
2. 帶有collection 屬性的resultMap
<resultMap id="myid" type="Id"> <id Property="id" column="id"> <collecton property="otherId" ofType="String" javaType="ArrayList"> <result colume="otherId"> </collection> </resultMap>
問題解決,上述的關係對映足以滿足要求。需要注意的是< id > 標籤,這是分組的標識。一定要注意。
關於resultMap各標籤的意義,請自行查資料吧。