MyBatis批量操作报错:Parameter 'xxxList' not found. Available parameters are [list]

2018-03-27 13:59:52

问题背景:

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

Shell代码  收藏代码

  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. ……  

单元测试用例代码如下:

Java代码  收藏代码

  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方法代码如下:

Java代码  收藏代码

  1. public int getStudentCount(List<String> studentNameList){  

  2.     return super.count("getStudentCount", studentNameList);  

  3. }  

MyBatis mapper.xml定义如下:

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

Xml代码  收藏代码

  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方法修改为:

Java代码  收藏代码

  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

Xml代码  收藏代码

  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>  

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

  • 2019-11-19 01:24:01

    使用JAVA8 filter对List多条件筛选

    记录项目开发的过程中遇到的一些问题及解决方法,由于公司操作数据库都是统一使用工具生成的存在一些多表查询模糊查询,这些操作只能在集合方面下手了,比如发送邮件记录方面查询,对用户的名字及邮件模糊检索 年龄匹配查询。

  • 2019-11-21 18:13:08

    如何在vue单页应用中使用百度地图

    百度开发者平台已经封装了基于vue的地图组件,详细使用,请参考官网: https://dafrok.github.io/vue-baidu-map/#/zh/start/installation 网上有一些是直接在index.html页面全部引用的,本人强烈反对此种使用方式,因为我们项目是组件化的单页应用,强行引入多页应用的开发方式,会破坏整个项目的框架,严重影响性能。有些甚至还在vue单页应用中引入jquery,感觉这都是一些反人类的骚操作,不到万不得已,不建议使用。

  • 2019-11-25 17:04:10

    Throttle 和 Debounce 的本质及一个简单的实现

    Throttle,Debounce 就不把这两个词翻译成中文了,直接解释他们的概念。实际上这两个东西本质上是一样的,作用都是「为了避免某个『事件』在『一个较短的时间段内』内连续被触发从而引起的其对应的『事件处理函数』不必要的连续执行」。那么区别在哪呢?

  • 2019-11-25 17:05:44

    js实现 throttle 和 debounce,节流,防抖详解

    throttle 节流:drag改变浏览器大小,触发onresize函数,实现拖动每过1秒输出一次,不足1秒,1秒后输出一次。多用于高频操作,如抢票、抢购等,无论点击多少次,只固定间隔执行一次,以减轻压力。debounce防抖:drag改变浏览器大小,触发onresize函数,实现拖动停顿1秒输出。多用于输入框,当某一次输入后停顿满n秒才会去触发远程搜索。

  • 2019-11-25 17:37:01

    百度地图GeoUtils示例

    百度地图JavaScript开源库,是一套基于百度地图API二次开发的开源的代码库。目前提供多个lib库,帮助开发者快速实现在地图上添加Marker、自定义信息窗口、标注相关开发、区域限制设置、几何运算、实时交通、检索与公交驾车查询、鼠标绘制工具等功能。