我不知道如何在复杂的对象中使用 mybatis 结果。
我有以下映射器类:
public interface StationMapper {
@MapKey("stationId")
Map<Integer,Station> getStations();
}
它是根据以下 resultMap 构建的:
<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();
Edit:如果我根据某些列限制它并设置属性而不是使用构造函数,我可以得到这个复杂的对象,但是在没有关系限制的情况下如何才能做到这一点呢?
<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>