需求: 根據傳入的參數批量 刪除數據:html
DAO:java
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:git
<delete id="delUser" parameterType="list" > delete from users where id in <foreach collection="li" index="index" open="(" close=")" separator="," item="itm"> #{itm} </foreach> </delete>
這樣處理會報錯github
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,怎麼會報錯呢?spring
查詢了一下MyBatis官方的說明文檔,終於找到了緣由,在http://mybatis.github.io/mybatis-3/zh/dynamic-sql.html#foreach裏有一段說明:sql
寫道apache
注意 你能夠傳遞一個 List 實例或者數組做爲參數對象傳給 MyBatis。當你這麼作的時 候,MyBatis 會自動將它包裝在一個 Map 中,用名稱在做爲鍵。List 實例將會以「list」 做爲鍵,而數組實例將會以「array」做爲鍵。數組
由於我傳的參數只有一個,並且傳入的是一個List集合,因此mybatis會自動封裝成Map<"list",li>。在解析的時候會經過「list」做爲Map的key值去尋找。可是我在xml中卻聲明成li了,因此天然會報錯找不到。mybatis
更改:app
<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便可。