MyBatis批量操作报错:Parameter 'xxxList' not found. Available parameters are [list]
问题背景:
在Dao中使用MyBatis进行查询操作,参数是传的一个List:studentNameList,但是在执行查询的时候报错,具体日志如下:
- com.chenzhou.base.mybatis.IbatisSystemException: SqlSession operation; nested exception is org.apache.ibatis.exceptions.PersistenceException:
- ### Error querying database. Cause: org.apache.ibatis.binding.BindingException: Parameter 'studentNameList' not found. Available parameters are [list]
- ### Cause: org.apache.ibatis.binding.BindingException: Parameter 'studentNameList' not found. Available parameters are [list]
- at com.chenzhou.base.mybatis.SqlSessionTemplate.wrapException(SqlSessionTemplate.java:341)
- at com.chenzhou.base.mybatis.SqlSessionTemplate.execute(SqlSessionTemplate.java:127)
- at com.chenzhou.base.mybatis.SqlSessionTemplate.execute(SqlSessionTemplate.java:106)
- at com.chenzhou.base.mybatis.SqlSessionTemplate.selectOne(SqlSessionTemplate.java:138)
- at com.chenzhou.dao.GenericMybatisDao.count(GenericMybatisDao.java:306)
- at com.chenzhou.cds.ps.dao.impl.StudentDao.getStudentCount(StudentDao.java:42)
- at com.chenzhou.cds.ps.dao.impl.StudentDao$$FastClassByCGLIB$$8819e766.invoke(<generated>)
- at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:191)
- at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:689)
- at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
- at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:80)
- at com.chenzhou.util.LogUtil.doMethodInfo(LogUtil.java:85)
- at com.chenzhou.util.LogUtil.doDebugMethodLog(LogUtil.java:36)
- at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
- at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
- at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
- at java.lang.reflect.Method.invoke(Method.java:597)
- at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:621)
- at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:610)
- at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:65)
- at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
- at org.springframework.aop.aspectj.AspectJAfterThrowingAdvice.invoke(AspectJAfterThrowingAdvice.java:55)
- at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
- at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
- at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
- at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:90)
- at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
- at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:622)
- at com.chenzhou.cds.ps.dao.impl.StudentDao$$EnhancerByCGLIB$$d4fcf513.getStudentCount(<generated>)
- at com.chenzhou.ps.dao.StudentDaoTest.testgetStudentCount(StudentDaoTest.java:44)
- ……
单元测试用例代码如下:
- @Test
- public void testgetStudentCount(){
- List<String> studentNameList = new ArrayList<String>();
- studentNameList.add("chenzhou");
- studentNameList.add("zhangsan");
- studentNameList.add("lisi");
- int count = studentDao.getStudentCount(studentNameList);
- System.out.println(count);
- }
studentDao中的getStudentCount方法代码如下:
- public int getStudentCount(List<String> studentNameList){
- return super.count("getStudentCount", studentNameList);
- }
MyBatis mapper.xml定义如下:
- <!-- 查询学生数量 -->
- <select id="Student.getStudentCount" parameterType="java.util.List" resultType="java.lang.Integer">
- <![CDATA[
- SELECT
- COUNT(*)
- FROM
- t_student WHERE 1=1
- ]]>
- <if test="studentNameList != null">
- AND student_name in
- <foreach collection="studentNameList" item="item" open="(" separator="," close=")">
- #{item}
- </foreach>
- </if>
- </select>
根据报错日志分析,是MyBatis在解析xml时找不到其中声明的studentNameList,但是在Dao中明明传的参数就是studentNameList,怎么会报错呢?
查询了一下MyBatis官方的说明文档,终于找到了原因,在http://mybatis.github.io/mybatis-3/zh/dynamic-sql.html#foreach里有一段说明:
因为我传的参数只有一个,而且传入的是一个List集合,所以mybatis会自动封装成Map<"list",studentNameList>。在解析的时候会通过“list”作为Map的key值去寻找。但是我在xml中却声明成studentNameList了,所以自然会报错找不到。
解决办法:
第一种就是修改mapper.xml中foreach标签内容,把studentNameList修改为list
- <if test="list != null">
- AND student_name in
- <foreach collection="list" item="item" open="(" separator="," close=")">
- #{item}
- </foreach>
- </if>
不过这种方式我个人不太建议,因为以后如果要扩展该方法,增加集合参数的时候,还得修改xml中的内容。
第二种方式,修改dao中的参数传入方式,手动封装成map,然后把map当参数传进去
Dao方法修改为:
- public int getStudentCount(List<String> studentNameList){
- //把参数手动封装在Map中
- Map<String, Object> map = new HashMap<String, Object>();
- map.put("studentNameList", studentNameList);
- return super.count("getStudentCount", map);
- }
然后修改mapper.xml中的parameterType类型为Map
- <!--注意下面的parameterType类型必须修改为Map类型,foreach中引用的List名称不用改变-->
- <select id="Student.getStudentCount" parameterType="java.util.Map" resultType="java.lang.Integer">
- <![CDATA[
- SELECT
- COUNT(*)
- FROM
- t_student WHERE 1=1
- ]]>
- <if test="studentNameList != null">
- AND student_name in
- <foreach collection="studentNameList" item="item" open="(" separator="," close=")">
- #{item}
- </foreach>
- </if>
- </select>
修改完后,重新执行了一下测试用例,测试通过。
MyBatis批量操作报错:Parameter 'xxxList' not found. Available parameters are [list]的更多相关文章
- Mybatis传多个参数的问题 及MyBatis报错 Parameter '0' not found. Available parameters are [arg1, arg0, param1 问题
对于使用Mybatis ,传多个参数,我们可以使用对象封装外,还可以直接传递参数 对象的封装,例如查询对象条件basequery对象 <select id="getProductByP ...
- Mybatis报错:Parameter 'list' not found. Available parameters are [groupList, param1]
GroupDao.java 里面定义的方法: void batchInsertLog(@Param("groupList") List<MktPromotionIntegra ...
- IDEA下运行 mybatis报错 Parameter 'arg0' not found. Available parameters are [autoRecharge, id, param1, param2]
电脑换系统之后重新安装一了 一下idea 项目运行时出现了以下错误, [autoRecharge, id, param1, param2] 或 [arg0, id, arg1, param2] 参考地 ...
- MyBatis报错 Parameter '0' not found. Available parameters are [arg1, arg0, param1, param2]
修改 <update id="updateStatusById" parameterType="java.lang.Integer"> update ...
- mybatis foreach报错It was either not specified and/or could not be found for the javaType Type handler
或许是惯性思维,在mybatis使用foreach循环调用的时候,很多时候都是传一个对象,传一个List的情况很少,所以写代码有时候会不注意就用惯性思维方法做了. 今天向sql传参,传了一个List作 ...
- spring 整合Mybatis 《报错集合,总结更新》
错误:java.lang.NoClassDefFoundError: org/aspectj/weaver/reflect/ReflectionWorld$ReflectionWorldExcepti ...
- Mybatis数据库连接报错:对实体 "characterEncoding" 的引用必须以 ';' 分隔符结尾
Mybatis数据库连接报错:对实体 "characterEncoding" 的引用必须以 ';' 分隔符结尾 ============================== 蕃薯耀 ...
- 【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) 需要检查的步骤: ...
- 【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 ...
随机推荐
- iOS AppIcon尺寸和上传ITunes构建版本尺寸和iPhone屏幕尺寸
避免忘记. 记录一下 App Icon: 29X2940X4058X5876X7687X8780X80120X120152X152167X167180X180 ITunes构建版本: 1242 x 2 ...
- 【LeetCode】43. Multiply Strings
Multiply Strings Given two numbers represented as strings, return multiplication of the numbers as a ...
- AP_建立银行信息总行、分行、账户(设定)
2014-06-04 Created By BaoXinjian
- Linux内核(5) - 内核学习的相关资源
“世界上最缺的不是金钱,而是资源.”当我在一份报纸上看到这句大大标题时,我的第一反应是——作者一定是个自然环保主义者,然后我在羞愧得反省自身的同时油然生出一股对这样的无产主义理想者无比崇敬的情绪来. ...
- Python多线程之线程创建和终止
python主要是通过thread和threading这两个模块来实现多线程支持. python的thread模块是比較底层的模块,python的threading模块是对thread做了一些封装,能 ...
- C# 动态获取代码所在行号
通过System.Diagnostics.StackTrace获取代码所在行号和文件信息 获取行号信息 /// <summary> /// Get line number of code ...
- 多主机共享ssh Public/Private Key
前期服务器比较少,所有代码都放在github的私库中,在自己的github 设置中设置SSH keys就可以拉下相应的库中的代码到本地与服务器了,但是最近服务器多家了几台,每台都生成key加到gith ...
- 在 ASP.NET Web API 中,使用 命名空间(namespace) 来作为路由的参数
这个问题来源于我想在 Web API 中使用相同的控制器名称(Controller)在不同的命名空间下,但是 Web API 的默认 路由(Route) 机制是会忽略命名空间的不同的,如果这样做,会看 ...
- Java 小数类 及四舍五入的方法 精度非常高的小数时用
注意假设结果是无限位小数,不指定位数进行四舍五入的话会报错 import java.util.Scanner; import java.math.BigDecimal; public class Ma ...
- [cocos2dx笔记005]一个字符串管理配置类
在用vs开发cocos2dx过程中.要显示的中文,要求是UTF-8格式的才干正常显示出来.但VS通常是ANSI格式保存,这样,在代码中写入的中文字符串,执行后.显示的就是乱码. 为了正确显示中文.或支 ...