首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >mybatis接收list集合[什么的传递]

mybatis接收list集合[什么的传递]

作者头像
Java架构师必看
发布2022-03-22 15:29:07
发布2022-03-22 15:29:07
2.6K00
代码可运行
举报
文章被收录于专栏:Java架构师必看Java架构师必看
运行总次数:0
代码可运行

大家好,我是架构君,一个会写代码吟诗的架构师。今天说一说mybatis接收list集合[什么的传递],希望能够帮助大家进步!!!

完整错误如下:

org.apache.ibatis.binding.BindingException: Parameter ‘customerIdList’ not found. Available parameters are collection, list

解释:

当我们传递一个 List 实例或者数组作为参数对象传给 MyBatis。当你这么做的时 候,MyBatis 会自动将它包装在一个 Map 中,用名称在作为键。List 实例将会以“list” 作为键,而数组实例将会以“array”作为键。所以,当我们传递的是一个List集合时,mybatis会自动把我们的list集合包装成以list为Key值的map。

代码语言:javascript
代码运行次数:0
运行
复制
DAO 层:
Long selectCustomerCountList( List customerIdList);

XML文件:
<select id="selectCustomerCountList" parameterType="java.util.List" resultType="java.lang.Long">
        select count(1) from np_customer_info where id in
        <foreach item="item" collection="customerIdList" separator="," open="(" close=")" index="">  #{item, jdbcType=INTEGER}    
        </foreach> 
    </select>
==========================
注意:DAO 层接口的参数名与XML 文件中的collection的属性值一致,是导致的问题的主要原因。

解决方法

第一种:利用Mybatis给我们的封装进行XML配置,将我们的XML中collection属性值设置为list。

代码语言:javascript
代码运行次数:0
运行
复制
DAO 层:
Long selectCustomerCountList( List customerIdList);

XML文件:
<select id="selectCustomerCountList" parameterType="java.util.List" resultType="java.lang.Long">
        select count(1) from np_customer_info where id in
        <foreach item="item" collection="list" separator="," open="(" close=")" index="">  #{item, jdbcType=INTEGER}    
        </foreach> 
    </select>
======================
注意:此时collection强制指定为list且不可改变

第二种: 利用注解@Param指定我们的入参名称

代码语言:javascript
代码运行次数:0
运行
复制
DAO层:
Long selectCustomerCountList(@Param("customerIdList") List customerIdList);

XML文件:
<select id="selectCustomerCountList" parameterType="java.util.List" resultType="java.lang.Long">
        select count(1) from np_customer_info where id in
        <foreach item="item" collection="customerIdList" separator="," open="(" close=")" index="">  #{item, jdbcType=INTEGER}    
        </foreach> 
    </select>

======================
注意: 此时的DAO层参数名可以 @Param("customerIdList") 与 collection的属性值一致

第三种:将我们的List包装成Map参数进行传递

代码语言:javascript
代码运行次数:0
运行
复制
在Service业务处理层次上面将参数进行包装
public Long selectCustomerCountMap(List customerIdList) {   
        Map maps = new HashMap();
        maps.put("customerIds", customerIdList);
        return customerMapper.selectCustomerCountMap(maps);
    }
    DAO层:
    Long selectCustomerCountMap(Map maps);
XML文件:

<select id="selectCustomerCountMap" parameterType="java.util.Map" resultType="java.lang.Long">
        select count(1) from np_customer_info where id in
        <foreach item="item" collection="customerIds" separator="," open="(" close=")" index="">  #{item, jdbcType=INTEGER}    
        </foreach> 
    </select>
==============
注意: 入参类型是java.util.Map而不再是List ,此时的collection属性值为Map中的Key值。

今天文章到此就结束了,感谢您的阅读,Java架构师必看祝您升职加薪,年年好运。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-03-162,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档