MyBatis batch operation error: Parameter & #39; xxxList & #39; not found. Available parameters are [list], mybatisxxxlist

Source: Internet
Author: User
Tags cdata

MyBatis batch operation error: Parameter 'xxxlist' not found. Available parameters are [list], mybatisxxxlist

Problem Background:

In Dao, MyBatis is used for query. The parameter is a List: studentNameList, but an error is reported when the query is executed. The specific log is as follows:

Shell code
  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.exe cute (SqlSessionTemplate. java: 127)
  6. At com.chenzhou.base.mybatis.SqlSessionTemplate.exe cute (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. ......

The unit test case code is as follows:

Java code
  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. }

The code for the getStudentCount method in studentDao is as follows:

Java code
  1. Public int getStudentCount (List <String> studentNameList ){
  2. Return super. count ("getStudentCount", studentNameList );
  3. }

MyBatis mapper. xml is defined as follows:

Xml Code
  1. <! -- Query the number of students -->
  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>

According to the error log analysis, MyBatis could not find the declared studentNameList when parsing xml, but the parameter explicitly passed in Dao is studentNameList. How can I report an error?

After querying the official MyBatis documentation, I finally found the cause. There are descriptions in http://mybatis.github.io/mybatis-3/zh/dynamic-sql.html#foreach:

Note that you can pass a List instance or array as the parameter object to MyBatis. When you do this, MyBatis automatically wraps it in a Map and uses the name as the key. The List instance uses "list" as the key, and the array instance uses "array" as the key.

Because only one parameter is passed in and a List set is passed in, mybatis is automatically encapsulated into Map <"list", studentNameList>. During parsing, the "list" is used as the key value of the Map. However, I declare studentNameList in xml, so the error cannot be found.

 

Solution:

First, modify the content of the foreach tag in mapper. xml and change studentNameList to list.

Xml Code
  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>

However, I personally do not recommend this method, because if you want to extend this method in the future, you have to modify the content in xml when adding set parameters.

 

The second method is to modify the parameter input method in dao, manually encapsulate it into map, and then transmit the map as the parameter.

Change Dao method:

Java code
  1. Public int getStudentCount (List <String> studentNameList ){
  2. // Manually encapsulate parameters in Map
  3. Map <String, Object> map = new HashMap <String, Object> ();
  4. Map. put ("studentNameList", studentNameList );
  5. Return super. count ("getStudentCount", map );
  6. }

Modify the parameterType type in mapper. xml to Map

Xml Code
  1. <! -- Note that the following parameterType must be changed to the Map type. The List name referenced in foreach does not need to be changed. -->
  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>

After the modification, the test case is re-executed and the test passes.


Original article address: Programming Technology

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.