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属性可以设置多个字段,用,分隔。

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
MyBatisResultMap是用来映射查询结果到Java对象的工具。Association是ResultMap的一个标签,用于描述一个一对一的关联关系。 在ResultMap使用Association标签可以定义一个复杂类型的关联关系,它可以将查询结果的某些字段映射到一个子对象。Association标签需要设置property属性来指定Java对象对应的属性名,column属性来指定查询结果对应的列名。 使用Association标签的示例代码如下所示: ``` <resultMap id="orderResultMap" type="Order"> <id property="id" column="order_id" /> <result property="orderNo" column="order_no" /> <association property="customer" javaType="Customer"> <id property="customerId" column="customer_id" /> <result property="customerName" column="customer_name" /> <result property="customerAddress" column="customer_address" /> </association> </resultMap> ``` 在这个示例,我们定义了一个名为orderResultMapResultMap,映射的Java对象类型是Order。在Order对象,有一个名为customer的属性,它是一个Customer类型的对象。使用Association标签,我们定义了customer属性的映射关系,将查询结果的customer_id映射到Customer对象的customerId属性,将customer_name映射到customerName属性,将customer_address映射到customerAddress属性。 这样,在查询结果如果有对应的关联数据,MyBatis就会自动将查询结果映射到Java对象的关联属性。 希望这个解释能帮到你,如果有需要进一步了解的话可以收藏起来哦!<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [MyBatisResultMap的association和collection标签详解(图文例子)](https://blog.csdn.net/qq_52423918/article/details/120828850)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [mybatisresultmap、association、collection使用](https://blog.csdn.net/weixin_44236424/article/details/125765174)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

方大拿拿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值