我無法弄清楚如何在複雜的對象中使用mybatis結果。嵌套的mybatis地圖
我有以下的映射器類:
public interface StationMapper {
@MapKey("stationId")
Map<Integer,Station> getStations();
}
這是從下面的結果映射建:
<resultMap id="stationMap" type="Station">
<result column="station_id" property="stationId" />
<result column="another_id" property="notUniqueId" />
<result column="name" property="name" />
</resultMap>
不是地圖,我想爲getStations返回類型()來是一個將地圖(或集合)作爲構造函數的對象,以便我可以在對象構造上執行一些代碼。
public class FancyStationMapHolder {
public FancyStationMapHolder(Map<Integer,Station> stations) {
executeSpecialCode(stations);
}
// OR
public FancyStationMapHolder(Collection<Station> stations) {
executeSpecialCode(stations);
}
}
public interface StationMapper {
FancyStationMapHolder getStations();
}
我不認爲我可以使用<結果映射> <集合>元素(和我試過),因爲沒有實際數據的關係。
或者,我想一個StationMapper方法與此簽名:
@MapKey("notUniqueId")
Map<String,Collection<Station>> getStationsByNotUniqueId();
編輯: 如果我限制它基於一些列我能得到這個複雜的對象,設置屬性,而不是使用一個構造函數,但是我怎麼能沒有關係限制呢?
<resultMap id="fancyMap" type="sample.FancyStationMapHolder">
<result column="someId" property="someId"/>
<collection property="stations" column="someId" resultMap="stationMap"
select="stations" javaType="ArrayList" />
</resultMap>
我更喜歡使用構造函數,所以我會很感激這方面的建議。下面的設置導致此異常:
org.mybatis.spring.MyBatisSystemException:
nested exception is org.apache.ibatis.reflection.ReflectionException:
Error instantiating class sample.FancyStationMapHolder with invalid types
(ArrayList,) or values ([...
<resultMap id="fancyMap" type="sample.FancyStationMapHolder">
<constructor>
<arg column="someId" resultMap="stationMap" select="stations"
javaType="ArrayList" />
</constructor>
<result column="someId" property="someId"/>
</resultMap>