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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

Fate-p   2021-03-15 我要评论
想了解详解Mybatis 传递参数类型为List的取值问题的相关内容吗,Fate-p在本文为您仔细讲解Mybatis 传递参数类型为List的取值的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:MyBatis传入参数为List,Mybatis,传递参数List取值,下面大家一起来学习吧。

问题描述:

参数传递为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" 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>

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们