• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

详解Mybatis 传递参数类型为List的取值问题

mybatis 搞代码 4年前 (2022-01-09) 20次浏览 已收录 0个评论

问题描述:

参数传递为List时:

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

DAO 层:

List<User> selectUserByIDs( List IDs);

XML文件:

<select id="selectUserByIDs" parameterType="java.util.List" resultType="user">
    select * from user
    <where>
      <if test="IDs != null and IDs.size() >0">
        <foreach collection="IDs" open=" and id in (" close=")" item="uid" separator=",">
          #{uid}
        </foreach>
      </if>
    </where>
</select>

报错信息:

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

解决方法:

方法一:将我们的XML中collection属性值直接设置为list

DAO 层:

List<User> selectUserByIDs( List IDs);

XML文件:

<select id="selectUserByIDs" parameterType="java.util.List" resultType="user">
    select * from user
    <where>
      <if test="list != null and list.size() >0">
        <foreach collection="list" open=" and id in (" close=")" item="uid" separator=",">
          #{uid}
        </foreach>
      </if>
    </where>
</select>

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

DAO层:

List<User> selectUserByIDs(@Param("IDs") List IDs);

XML文件:

<select id="selectUserByIDs" parameterType="java.util.List" resultTyp<strong style="color:transparent">来2源gaodaima#com搞(代@码&网</strong>e="user">
    select * from user
    <where>
      <if test="IDs != null and IDs.size() >0">
        <foreach collection="IDs" open=" and id in (" close=")" item="uid" separator=",">
          #{uid}
        </foreach>
      </if>
    </where>
</select>

到此这篇关于详解Mybatis 传递参数类型为List的取值问题的文章就介绍到这了,更多相关Mybatis 传递参数类型为List的取值内容请搜索搞代码以前的文章或继续浏览下面的相关文章希望大家以后多多支持搞代码


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:详解Mybatis 传递参数类型为List的取值问题

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址