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


問題背景:

在Dao中使用MyBatis進行查詢操作,參數是傳的一個List:studentNameList,但是在執行查詢的時候報錯,具體日志如下:

Shell代碼   收藏代碼
  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.execute(SqlSessionTemplate.java:127)  
  6.     at com.chenzhou.base.mybatis.SqlSessionTemplate.execute(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. ……  

單元測試用例代碼如下:

Java代碼   收藏代碼
  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. }  

studentDao中的getStudentCount方法代碼如下:

Java代碼   收藏代碼
  1. public int getStudentCount(List<String> studentNameList){  
  2.     return super.count("getStudentCount", studentNameList);  
  3. }  

MyBatis mapper.xml定義如下:

Xml代碼   收藏代碼
  1. <!-- 查詢學生數量  -->  
  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>  

根據報錯日志分析,是MyBatis在解析xml時找不到其中聲明的studentNameList,但是在Dao中明明傳的參數就是studentNameList,怎么會報錯呢?

查詢了一下MyBatis官方的說明文檔,終於找到了原因,在http://mybatis.github.io/mybatis-3/zh/dynamic-sql.html#foreach里有一段說明:

寫道
注意 你可以傳遞一個 List 實例或者數組作為參數對象傳給 MyBatis。當你這么做的時 候,MyBatis 會自動將它包裝在一個 Map 中,用名稱在作為鍵。List 實例將會以“list” 作為鍵,而數組實例將會以“array”作為鍵。

因為我傳的參數只有一個,而且傳入的是一個List集合,所以mybatis會自動封裝成Map<"list",studentNameList>。在解析的時候會通過“list”作為Map的key值去尋找。但是我在xml中卻聲明成studentNameList了,所以自然會報錯找不到。

 

解決辦法:

第一種就是修改mapper.xml中foreach標簽內容,把studentNameList修改為list

Xml代碼   收藏代碼
  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>  

不過這種方式我個人不太建議,因為以后如果要擴展該方法,增加集合參數的時候,還得修改xml中的內容。

 

第二種方式,修改dao中的參數傳入方式,手動封裝成map,然后把map當參數傳進去

Dao方法修改為:

Java代碼   收藏代碼
  1. public int getStudentCount(List<String> studentNameList){  
  2.     //把參數手動封裝在Map中  
  3.     Map<String, Object> map = new HashMap<String, Object>();  
  4.     map.put("studentNameList", studentNameList);  
  5.     return super.count("getStudentCount", map);  
  6. }  

然后修改mapper.xml中的parameterType類型為Map

Xml代碼   收藏代碼
  1. <!--注意下面的parameterType類型必須修改為Map類型,foreach中引用的List名稱不用改變-->  
  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>  

修改完后,重新執行了一下測試用例,測試通過。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM