Requirements: Bulk Delete data According to the parameters passed in:
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
This will cause an error.
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 is [list]### Cause:org.apache.ibatis.binding.BindingException:Parameter ' studentnamelist ' not Found. Available parameters is [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 $ $d 4fcf513.getstudentcount (<generated>) at Com.chenzhou.ps.dao.StudentDaoTest.testgetStudentCount (studentdaotest.java:44) ...
According to the error log analysis, is mybatis in parsing the XML is not found in the declaration of Li, but in DAO clearly passed the parameter is Li, how can error ?
inquires the official MyBatis document, finally found the reason, in Http://mybatis.github.io/mybatis-3/zh/dynamic-sql.html#foreach there is a description:
Note that you can pass a List instance or array as a parameter object to MyBatis. When you do this, MyBatis will automatically wrap it in a Map, using the name as the key. The list instance will be "list" as the key, and the array instance will be "array" as the key.
Because I pass the parameter only one, and the incoming is a list collection, so MyBatis will be automatically encapsulated into map< "list",li>. When parsing, the "list" is used as the key value of the map to find. But I declare it as Li in XML, so I will not find the error.
Change:
<delete id= "Deluser" parametertype= "map" >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);
Modify the parameters in the DAO method, manually encapsulated into a map, and then the map when the parameters passed in, so that the MyBatis will follow the parameter map key to match value, because the DAO key and the XML key can match, so OK, if
HashMap Li =new HashMap (); Li.put ("Li", ll);
and in XML
<foreach collection= "Li" ...
Not the same will still error.
Another way to modify it is to modify it directly into
<foreach collection= "List"
The accepted parameters in the XML are still list.
MyBatis Batch operation error: Parameter ' xxxlist ' not found. Available parameters is [list]