码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-04-06 23:16:48      阅读:1106      评论:0      收藏:0      [点我收藏+]

标签:

需求: 根据传入的参数批量 删除数据:

DAO:

List ll = new ArrayList<Integer>();
		for(int i=10;i<25;i++){
			ll.add(i);
		}
	  
		int res = userMapper.delUser(li);
		System.out.println(res);

xml:

   <delete id="delUser" parameterType="list" >
		delete  from users where id in 
		<foreach collection="li" index="index" open="(" close=")" separator="," item="itm">
			#{itm}
		</foreach>
	</delete> 

这样处理会报错

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 'li' 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)
……

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

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

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

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

更改:

   <delete id="delUser" parameterType="map" >
		delete  from users where id in 
		<foreach collection="li" index="index" open="(" close=")" separator="," item="itm">
			#{itm}
		</foreach>
	</delete> 


List ll = new ArrayList<Integer>();
		for(int i=10;i<25;i++){
			ll.add(i);
		}
	    HashMap li =new HashMap();
	    li.put("li", ll);
		int res = userMapper.delUser(li);
		System.out.println(res);

修改dao中的参数传入方式,手动封装成map,然后把map当参数传进去,这样mybatis会跟据参数map的key去匹配value,由于dao的key和xml中的key一样可以匹配上,所以OK,如果

 HashMap li =new HashMap();
	    li.put("li", ll);


和xml中

<foreach collection="li"  ........
不一样仍然会报错。


另一种修改的方式是直接修改成

<foreach collection="list"
xml中的接受的参数仍然是list即可。

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

标签:

原文地址:http://blog.csdn.net/liyantianmin/article/details/44907249

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!