MyBatis Mapper Namespace

Source: Internet
Author: User
Tags cdata

Http://www.mybatis.org/mybatis-3/zh/sqlmap-xml.html#insert_update_and_delete

org.apache.ibatis.exceptions.PersistenceException: # # # Error querying database. Cause:org.apache.ibatis.executor.ExecutorException: No constructor found in Tk.mybatis.springboot.model.Course matching [Java.lang.Integer, java.lang.string]### the error may exist in mapper/coursemapper.xml### The error may involve tk.mybatis.springboot.mapper.coursemapper.selectall### the error occurred whileHandling results### Sql:select*From course### Cause:org.apache.ibatis.executor.ExecutorException: No constructor found in Tk.mybatis.springboot.model.Course matching [Java.lang.Integer, java.lang.String] at Org.apache.ibatis.exceptions.ExceptionFactory.wrapException (Exceptionfactory.java:30) at Org.apache.ibatis.session.defaults.DefaultSqlSession.selectList (Defaultsqlsession.java:122) at Org.apache.ibatis.session.defaults.DefaultSqlSession.selectList (Defaultsqlsession.java:113) at Org.apache.ibatis.binding.MapperMethod.executeForMany (Mappermethod.java:122) at Org.apache.ibatis.binding.MapperMethod.execute (Mappermethod.java:64) at Org.apache.ibatis.binding.MapperProxy.invoke (Mapperproxy.java:53) at Com.sun.proxy. $Proxy 6.selectAll (Unknown Source)

org.apache.ibatis.jdbc.RuntimeSqlException:Error executing:create TABLE course (id INTEGER, NAME VARCHAR (20< c1>)).  Line missing end  -of-line Terminator (;) = Create TABLE course (ID INTEGER, NAME VARCHAR) at    Org.apache. Ibatis.jdbc.ScriptRunner.executeLineByLine (Scriptrunner.java:141)    at Org.apache.ibatis.jdbc.ScriptRunner.runScript (Scriptrunner.java:101)

Interface Tk.mybatis.springboot.mapper.CourseMapper is not known to the Mapperregistry.    At Org.apache.ibatis.binding.MapperRegistry.getMapper (Mapperregistry.java:+)    at Org.apache.ibatis.session.Configuration.getMapper (Configuration.java:689)    at Org.apache.ibatis.session.defaults.DefaultSqlSession.getMapper (Defaultsqlsession.java:250)

The interface that defines the behavior in MyBatis, which needs to be registered in the XML file:

<! DOCTYPE Mapper Public "-//mybatis.org//dtd mapper 3.0//en" "Http://mybatis.org/dtd/mybatis-3-mapper.dtd" ><  Mappernamespace= "Tk.mybatis.springboot.mapper.CourseMapper" >

Issue background:

In DAO, the query operation using MyBatis, the parameter is a list:studentnamelist, but in the execution of the query error, 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 is [list]
  3. # # # Cause:org.apache.ibatis.binding.BindingException:Parameter ' studentnamelist ' not found. Available parameters is [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:)
  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:
  15. At Com.chenzhou.util.LogUtil.doMethodInfo (Logutil.java:)
  16. At Com.chenzhou.util.LogUtil.doDebugMethodLog (Logutil.java:)
  17. At Sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
  18. At Sun.reflect.NativeMethodAccessorImpl.invoke (Nativemethodaccessorimpl.java:)
  19. At Sun.reflect.DelegatingMethodAccessorImpl.invoke (Delegatingmethodaccessorimpl.java:)
  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: +)
  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: )
  28. At Org.springframework.aop.framework.ReflectiveMethodInvocation.proceed (Reflectivemethodinvocation.java:172 )
  29. At Org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke (Exposeinvocationinterceptor.java: )
  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 $ $d 4fcf513.getstudentcount (<generated>)
  33. At Com.chenzhou.ps.dao.StudentDaoTest.testgetStudentCount (Studentdaotest.java:)
  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 Getstudentcount method code 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:

<!--Check the number of students -  <SelectID= "Student.getstudentcount"ParameterType= "Java.util.List"Resulttype= "Java.lang.Integer">     <! [Cdata[SELECT COUNT (*) from t_student WHERE 1=1]]>     <offTest= "Studentnamelist! = null">and Student_name in<foreachCollection= "Studentnamelist"Item= "Item"Open="("Separator=","Close=")">#{item}</foreach>      </if>  </Select>  

According to the error log analysis, is mybatis in parsing the XML is not found in the declaration of the studentnamelist, but in the DAO clearly passed the parameter is studentnamelist, 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.
List InstanceWill be " List"As a key,
The array instance will use "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",studentnamelist>. When parsing, the "list" is used as the key value of the map to find. But I declare in the XML is studentnamelist, so naturally will error can not find.

Workaround:

The first is to modify the contents of the foreach tag in Mapper.xml and change the studentnamelist to list

<offTest= "List! = null">and Student_name in<foreachCollection= "List"Item= "Item"Open="("Separator=","Close=")">#{item}</foreach>  </if>  

But this way I personally do not recommend, because later if you want to extend the method, increase the collection parameters, you have to modify the contents of the XML.

The second way, modify the parameters in the DAO in the way, manually encapsulated into a map, and then the map as a parameter to pass in

The DAO method is modified to:

 Public int getstudentcount (list<string> studentnamelist) {       // The parameters are manually encapsulated in the map       New hashmap<string, object>();       Map.put ("Studentnamelist", studentnamelist);        return Super. Count ("Getstudentcount", map);   

Then modify the ParameterType type in Mapper.xml to map

<!--Note that the following ParameterType type must be modified to the map type, and the list name referenced in foreach does not change -  <SelectID= "Student.getstudentcount"ParameterType= "Java.util.Map"Resulttype= "Java.lang.Integer">     <! [Cdata[SELECT COUNT (*) from t_student WHERE 1=1]]>     <ifTest= "Studentnamelist! = null">and Student_name in<foreachCollection= "Studentnamelist"Item= "Item"Open="("Separator=","Close=")">#{item}</foreach>      </if>  </Select>  

After the modification, re-executed the test case, test pass.

Http://www.cnblogs.com/winkey4986/p/3480328.html

MyBatis Mapper Namespace

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.