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
- 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 ' studentnamelist ' 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:)
- 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:
- At Com.chenzhou.util.LogUtil.doMethodInfo (Logutil.java:)
- At Com.chenzhou.util.LogUtil.doDebugMethodLog (Logutil.java:)
- At Sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
- At Sun.reflect.NativeMethodAccessorImpl.invoke (Nativemethodaccessorimpl.java:)
- At Sun.reflect.DelegatingMethodAccessorImpl.invoke (Delegatingmethodaccessorimpl.java:)
- 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: +)
- 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: )
- At Org.springframework.aop.framework.ReflectiveMethodInvocation.proceed (Reflectivemethodinvocation.java:172 )
- At Org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke (Exposeinvocationinterceptor.java: )
- 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:)
- ......
The unit test case code is as follows:
Java code
- @Test
- Public void Testgetstudentcount () {
- list<string> studentnamelist = new arraylist<string> ();
- Studentnamelist.add ("Chenzhou");
- Studentnamelist.add ("Zhangsan");
- Studentnamelist.add ("Lisi");
- int count = Studentdao.getstudentcount (studentnamelist);
- System.out.println (count);
- }
The Getstudentcount method code in Studentdao is as follows:
Java code
- public int Getstudentcount (list<string> studentnamelist) {
- Return Super.count ("Getstudentcount", studentnamelist);
- }
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