嵌套的mybatis地图 [英] nested mybatis map

查看:74
本文介绍了嵌套的mybatis地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何在复杂的对象中使用mybatis结果.

I cannot figure out how to use mybatis results in a complex object.

我有以下映射器类:

public interface StationMapper {
   @MapKey("stationId")
   Map<Integer,Station> getStations();
}

是根据以下resultMap构建的:

which is built from the following resultMap:

<resultMap id="stationMap" type="Station">
   <result column="station_id" property="stationId" />
   <result column="another_id" property="notUniqueId" />
   <result column="name" property="name" />
</resultMap>

我希望使用getMaps()的返回类型作为一个对象,该对象将地图(或集合)作为构造函数,以便我可以在对象构造上执行一些代码.

Instead of a map, I'd like the return type for getStations() to be an object which takes the map (or a collection) as a constructor so that I can execute some code on object construction.

public class FancyStationMapHolder {
   public FancyStationMapHolder(Map<Integer,Station> stations) {
      executeSpecialCode(stations);
   }
   // OR
   public FancyStationMapHolder(Collection<Station> stations) {
      executeSpecialCode(stations);
   }
}
public interface StationMapper {
   FancyStationMapHolder getStations();
}

我认为我不能使用< resultMap>< collection>元素(并且我已经尝试过),因为没有实际的数据关系.

I don't think I can use the <resultMap> <collection> element (and I've tried) because there is no actual data relationship.

或者,我想要带有此签名的StationMapper方法:

Alternately, I'd like a StationMapper method with this signature:

@MapKey("notUniqueId")
Map<String,Collection<Station>> getStationsByNotUniqueId();

如果我根据某些列对其进行限制并设置属性而不是使用构造函数,则可以获得此复杂对象,但是如何在没有关系限制的情况下做到这一点呢?

I can get this complex object if I restrict it based on some column and set a property rather than use a constructor, but how can I do it without the relational restriction?

<resultMap id="fancyMap" type="sample.FancyStationMapHolder">
  <result column="someId" property="someId"/>
  <collection property="stations" column="someId" resultMap="stationMap"
   select="stations" javaType="ArrayList" />
</resultMap>

我更喜欢使用构造函数,所以我也很乐意就此提出建议.以下设置导致此异常:

I prefer to use the constructor, so I'd appreciate advice on that as well. The following setup results in this Exception:

 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>

推荐答案

MyBatis很好地完成了将列映射到POJO属性的工作,并且为高级映射做了一些额外的工作.但总的来说,我建议不要尝试通过MyBatis将您的业务逻辑放入映射/编组中.我自己走过这条路,大部分从制图仪中得到了我需要的东西,但后来全部删除了.

MyBatis does a great job of mapping columns to POJO attributes and does a little bit of extra work for fancier mappings. But in general I would advice against trying to put your business logic into mapping / marshalling via MyBatis. I travelled this road myself and mostly got what I needed out of the mappers, but later removed all of it.

因此,只需从您的映射器方法中获取List<Station>,然后将其转换为所需转换器中放置在DAO或Controller或Service中的私有转换器方法中的所需模型即可.最好总是用至少一个抽象层覆盖映射器,该抽象层将多个更新组合到单个事务中,处理乐观锁,隐藏内部帮助容器,高级缓存逻辑等.

So, simply get List<Station> from your mapper method and convert it to the desired model in a private converter method placed in your DAO or Controller or Service that needs this stuff. It is almost always better to cover mappers with at least one abstraction layer that combines multiple updates into single transactions, handles optimistic locks, hides internal helper containers, advanced caching logic etc.

一个好处是,这样的代码将更易于维护,并且下一个开发人员不必研究MyBatis映射的特殊性.另外,抓取List<Station>还可以将同一查询重新用于其他目的,并合理地对其进行缓存等.

One benefit is, such code will be easier to maintain, and the next developer does not have to dive into the peculiarities of MyBatis mapping. Also, grabbing List<Station> also allows to re-use the same query for other purposes, cache it reasonably well etc.

如果List<Station>占用过多资源并且查询位于关键性能路径上,请使用MyBatis ResultHandler遍历JDBS ResultSet.

If List<Station> takes too much resources and query is on a critical performance path, use MyBatis ResultHandler to iterate over JDBS ResultSet.

这篇关于嵌套的mybatis地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆