MyBatis resultMap中collection过滤空字段

在使用MyBatis查询数据时,返回值可以定义为resultMap

如果返回的对象中有列表,还可以使用collection标签进行定义。

此时,如果不想某些字段为空的数据加入列表,可以使用notNullColumn属性进行定义:

<resultMap id="resultMapDemo" type="返回值类型" >
    <id property="id" column="id" />
    <result property="name" column="name"/>
    <collection property="childList" notNullColumn="id,name" ofType="列表项类型">
      <id property="id" column="id"/>
      <result property="name" column="name"/>
      <result property="age" column="age"/>
    </collection>
</resultMap>

notNullColumn属性对应的是column属性的值,可以设置多个字段,用,分隔。

以上代码会过滤掉idname的数据加入列表,实现了resultMap中collection过滤空字段的功能。

猜你喜欢

转载自blog.csdn.net/qq_37770674/article/details/132361021