问题背景:

在Dao中使用MyBatis进行查询操作,参数是传的一个List:studentNameList,但是在执行查询的时候报错,具体日志如下:

  1. com.chenzhou.base.mybatis.IbatisSystemException: SqlSession operation; nested exception is org.apache.ibatis.exceptions.PersistenceException:
  2. ### Error querying database.  Cause: org.apache.ibatis.binding.BindingException: Parameter 'studentNameList' not found. Available parameters are [list]
  3. ### Cause: org.apache.ibatis.binding.BindingException: Parameter 'studentNameList' not found. Available parameters are [list]
  4. at com.chenzhou.base.mybatis.SqlSessionTemplate.wrapException(SqlSessionTemplate.java:341)
  5. at com.chenzhou.base.mybatis.SqlSessionTemplate.execute(SqlSessionTemplate.java:127)
  6. at com.chenzhou.base.mybatis.SqlSessionTemplate.execute(SqlSessionTemplate.java:106)
  7. at com.chenzhou.base.mybatis.SqlSessionTemplate.selectOne(SqlSessionTemplate.java:138)
  8. at com.chenzhou.dao.GenericMybatisDao.count(GenericMybatisDao.java:306)
  9. at com.chenzhou.cds.ps.dao.impl.StudentDao.getStudentCount(StudentDao.java:42)
  10. at com.chenzhou.cds.ps.dao.impl.StudentDao$$FastClassByCGLIB$$8819e766.invoke(<generated>)
  11. at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:191)
  12. at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:689)
  13. at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
  14. at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:80)
  15. at com.chenzhou.util.LogUtil.doMethodInfo(LogUtil.java:85)
  16. at com.chenzhou.util.LogUtil.doDebugMethodLog(LogUtil.java:36)
  17. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  18. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
  19. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
  20. at java.lang.reflect.Method.invoke(Method.java:597)
  21. at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:621)
  22. at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:610)
  23. at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:65)
  24. at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
  25. at org.springframework.aop.aspectj.AspectJAfterThrowingAdvice.invoke(AspectJAfterThrowingAdvice.java:55)
  26. at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
  27. at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
  28. at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
  29. at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:90)
  30. at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
  31. at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:622)
  32. at com.chenzhou.cds.ps.dao.impl.StudentDao$$EnhancerByCGLIB$$d4fcf513.getStudentCount(<generated>)
  33. at com.chenzhou.ps.dao.StudentDaoTest.testgetStudentCount(StudentDaoTest.java:44)
  34. ……

单元测试用例代码如下:

  1. @Test
  2. public void testgetStudentCount(){
  3. List<String> studentNameList = new ArrayList<String>();
  4. studentNameList.add("chenzhou");
  5. studentNameList.add("zhangsan");
  6. studentNameList.add("lisi");
  7. int count = studentDao.getStudentCount(studentNameList);
  8. System.out.println(count);
  9. }

studentDao中的getStudentCount方法代码如下:

  1. public int getStudentCount(List<String> studentNameList){
  2. return super.count("getStudentCount", studentNameList);
  3. }

MyBatis mapper.xml定义如下:

  1. <!-- 查询学生数量  -->
  2. <select id="Student.getStudentCount" parameterType="java.util.List" resultType="java.lang.Integer">
  3. <![CDATA[
  4. SELECT
  5. COUNT(*)
  6. FROM
  7. t_student WHERE 1=1
  8. ]]>
  9. <if test="studentNameList != null">
  10. AND student_name in
  11. <foreach collection="studentNameList" item="item" open="(" separator="," close=")">
  12. #{item}
  13. </foreach>
  14. </if>
  15. </select>

根据报错日志分析,是MyBatis在解析xml时找不到其中声明的studentNameList,但是在Dao中明明传的参数就是studentNameList,怎么会报错呢?

查询了一下MyBatis官方的说明文档,终于找到了原因,在http://mybatis.github.io/mybatis-3/zh/dynamic-sql.html#foreach里有一段说明:

写道
注意 你可以传递一个 List 实例或者数组作为参数对象传给 MyBatis。当你这么做的时 候,MyBatis 会自动将它包装在一个 Map 中,用名称在作为键。List 实例将会以“list” 作为键,而数组实例将会以“array”作为键。

因为我传的参数只有一个,而且传入的是一个List集合,所以mybatis会自动封装成Map<"list",studentNameList>。在解析的时候会通过“list”作为Map的key值去寻找。但是我在xml中却声明成studentNameList了,所以自然会报错找不到。

解决办法:

第一种就是修改mapper.xml中foreach标签内容,把studentNameList修改为list

  1. <if test="list != null">
  2. AND student_name in
  3. <foreach collection="list" item="item" open="(" separator="," close=")">
  4. #{item}
  5. </foreach>
  6. </if>

不过这种方式我个人不太建议,因为以后如果要扩展该方法,增加集合参数的时候,还得修改xml中的内容。

第二种方式,修改dao中的参数传入方式,手动封装成map,然后把map当参数传进去

Dao方法修改为:

  1. public int getStudentCount(List<String> studentNameList){
  2. //把参数手动封装在Map中
  3. Map<String, Object> map = new HashMap<String, Object>();
  4. map.put("studentNameList", studentNameList);
  5. return super.count("getStudentCount", map);
  6. }

然后修改mapper.xml中的parameterType类型为Map

  1. <!--注意下面的parameterType类型必须修改为Map类型,foreach中引用的List名称不用改变-->
  2. <select id="Student.getStudentCount" parameterType="java.util.Map" resultType="java.lang.Integer">
  3. <![CDATA[
  4. SELECT
  5. COUNT(*)
  6. FROM
  7. t_student WHERE 1=1
  8. ]]>
  9. <if test="studentNameList != null">
  10. AND student_name in
  11. <foreach collection="studentNameList" item="item" open="(" separator="," close=")">
  12. #{item}
  13. </foreach>
  14. </if>
  15. </select>

修改完后,重新执行了一下测试用例,测试通过。

MyBatis批量操作报错:Parameter 'xxxList' not found. Available parameters are [list]的更多相关文章

  1. Mybatis传多个参数的问题 及MyBatis报错 Parameter '0' not found. Available parameters are [arg1, arg0, param1 问题

    对于使用Mybatis ,传多个参数,我们可以使用对象封装外,还可以直接传递参数 对象的封装,例如查询对象条件basequery对象 <select id="getProductByP ...

  2. Mybatis报错:Parameter 'list' not found. Available parameters are [groupList, param1]

    GroupDao.java 里面定义的方法: void batchInsertLog(@Param("groupList") List<MktPromotionIntegra ...

  3. IDEA下运行 mybatis报错 Parameter 'arg0' not found. Available parameters are [autoRecharge, id, param1, param2]

    电脑换系统之后重新安装一了 一下idea 项目运行时出现了以下错误, [autoRecharge, id, param1, param2] 或 [arg0, id, arg1, param2] 参考地 ...

  4. MyBatis报错 Parameter '0' not found. Available parameters are [arg1, arg0, param1, param2]

    修改 <update id="updateStatusById" parameterType="java.lang.Integer"> update ...

  5. mybatis foreach报错It was either not specified and/or could not be found for the javaType Type handler

    或许是惯性思维,在mybatis使用foreach循环调用的时候,很多时候都是传一个对象,传一个List的情况很少,所以写代码有时候会不注意就用惯性思维方法做了. 今天向sql传参,传了一个List作 ...

  6. spring 整合Mybatis 《报错集合,总结更新》

    错误:java.lang.NoClassDefFoundError: org/aspectj/weaver/reflect/ReflectionWorld$ReflectionWorldExcepti ...

  7. Mybatis数据库连接报错:对实体 "characterEncoding" 的引用必须以 ';' 分隔符结尾

    Mybatis数据库连接报错:对实体 "characterEncoding" 的引用必须以 ';' 分隔符结尾 ============================== 蕃薯耀 ...

  8. 【mybatis】mybatis访问报错:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found) 或者 feign被调用方使用的mybatis总报空指针异常java.lang.NullPointerException,而变量都没有问题的情况

    mybatis访问报错:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found) 需要检查的步骤: ...

  9. 【Mybatis】mybatis查询报错org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'areaName' in 'class java.lang.String'

    mybatis查询报错: Caused by: org.apache.ibatis.reflection.ReflectionException: There is no getter for pro ...

随机推荐

  1. [转载]在rhel 6 x86_64 上安装oracle 11g xe

    原文地址:在rhel 6 x86_64 上安装oracle 11g xe作者:pccom Oracle 11g xe for linux目前只有x86_64 版本,没有i386, i686 版本,如果 ...

  2. MySQL 获取某一个分类ID的所有父或子分类查询结果

    创建一个自定义函数: ; DELIMITER $$ USE `dressv_website`$$ DROP FUNCTION IF EXISTS `fn_QueryRelation`$$ CREATE ...

  3. go 学习 ---数据类型

    25个关键字 程序声明:import, package 程序实体声明和定义:chan, const, func, interface, map, struct, type, var 程序流程控制:go ...

  4. RHCE7 管理I-12归档文件并在Linux系统间复制文件

    tar命令使用 默认tar只有归档的功能,没有压缩功能 tar [option...] [file]... -c,--create     创建 -x,--extract,--get   解压 -t, ...

  5. 深入Javascript中apply、call、bind

    最近在看一下node package的源码,发现很多里面都包含了function这个对象的apply.call.bind这三个方法,于是想拿出来再看看.. apply.call 在 javascrip ...

  6. 定时登录下载sftp服务器上的某些有规则的文件

    [root@1-live duizhangdan]# cat wget.sh #!/bin/bash#定时下载sina sftp服务器上的bak_dir=`date -d"5 day ago ...

  7. 为什么easyui的datagrid里getSelections还有getChecked只能获取一行值呢?

    http://jquery-easyui.wikidot.com/forum/t-280470 如果没有 idField属性,getSelections就只能获取一个行的值了 注意:如果用了前面说的& ...

  8. CSS中overflow:scroll怎么设置只上下滚动而不左右滚动

    CSS中"overflow:scroll"默认是左右,上下都滚动.怎么设置只上下滚动而不左右滚动,下面有个不错的解决方法 CSS中"overflow:scroll&quo ...

  9. 解决Pycharm中module 'pip' has no attribute 'main'的问题

    背景:pip升级至10.0.1后,使用Pycharm安装Package时一直提示module 'pip' has no attribute 'main'报错信息. 解决方法: 找到Pycharm安装目 ...

  10. oop klass

    https://www.infoq.com/articles/Introduction-to-HotSpot 借助HotSpot SA来一窥PermGen上的对象 找出栈上的指针/引用 虚拟机随谈(一 ...