详解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>
到此这篇关于详解Mybatis 传递参数类型为List的取值问题的文章就介绍到这了,更多相关Mybatis 传递参数类型为List的取值内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
详解在SpringBoot中@Transactional事物操作和事物无效问题排查
这篇文章主要介绍了详解在SpringBoot中@Transactional事物操作和事物无效问题排查,本文详细的介绍了SpringBoot中集成使用@Transactional注解操作事物以及事物开启后无效的问题排查,需要的朋友可以参考下2021-06-06
解决maven打包排除类不生效maven-compiler-plugin问题
总结:在Spring Boot项目B中作为项目A的依赖时,排除启动类不生效的原因是被其他类引用或父POM引入,解决方法是跳过test编译或注释掉@SpringBootTest(classes={BApplication.class})2024-11-11
基于Springboot+Netty实现rpc的方法 附demo
这篇文章主要介绍了基于Springboot+Netty实现rpc功能,在父项目中引入相关依赖结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2022-02-02


最新评论