排错方案见后文
报错日志如下:
org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: org.apache.ibatis.reflection.ReflectionException: Error instantiating interface com.guohui.dao.StudentMapper with invalid types () or values (). Cause: java.lang.NoSuchMethodException: com.guohui.dao.StudentMapper.<init>()
### The error may exist in com/guohui/dao/StudentMapper.xml
### The error may involve com.guohui.dao.StudentMapper.getStudent
### The error occurred while handling results
### SQL: select * from student
### Cause: org.apache.ibatis.reflection.ReflectionException: Error instantiating interface com.guohui.dao.StudentMapper with invalid types () or values (). Cause: java.lang.NoSuchMethodException: com.guohui.dao.StudentMapper.<init>()
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:153)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:145)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:140)
at org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:147)
at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:80)
at org.apache.ibatis.binding.MapperProxy$PlainMethodInvoker.invoke(MapperProxy.java:145)
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:86)
at com.sun.proxy.$Proxy6.getStudent(Unknown Source)
at com.guohui.dao.TestStudentMapper.testStudent(TestStudentMapper.java:15)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:221)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)
Caused by: org.apache.ibatis.reflection.ReflectionException: Error instantiating interface com.guohui.dao.StudentMapper with invalid types () or values (). Cause: java.lang.NoSuchMethodException: com.guohui.dao.StudentMapper.<init>()
at org.apache.ibatis.reflection.factory.DefaultObjectFactory.instantiateClass(DefaultObjectFactory.java:88)
at org.apache.ibatis.reflection.factory.DefaultObjectFactory.create(DefaultObjectFactory.java:53)
at org.apache.ibatis.reflection.factory.DefaultObjectFactory.create(DefaultObjectFactory.java:45)
at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.createResultObject(DefaultResultSetHandler.java:667)
at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.createResultObject(DefaultResultSetHandler.java:642)
at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.getRowValue(DefaultResultSetHandler.java:404)
at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleRowValuesForSimpleResultMap(DefaultResultSetHandler.java:361)
at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleRowValues(DefaultResultSetHandler.java:335)
at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleResultSet(DefaultResultSetHandler.java:308)
at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleResultSets(DefaultResultSetHandler.java:201)
at org.apache.ibatis.executor.statement.PreparedStatementHandler.query(PreparedStatementHandler.java:65)
at org.apache.ibatis.executor.statement.RoutingStatementHandler.query(RoutingStatementHandler.java:79)
at org.apache.ibatis.executor.SimpleExecutor.doQuery(SimpleExecutor.java:63)
at org.apache.ibatis.executor.BaseExecutor.queryFromDatabase(BaseExecutor.java:325)
at org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:156)
at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:109)
at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:89)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:151)
... 30 more
Caused by: java.lang.NoSuchMethodException: com.guohui.dao.StudentMapper.<init>()
at java.lang.Class.getConstructor0(Class.java:3082)
at java.lang.Class.getDeclaredConstructor(Class.java:2178)
at org.apache.ibatis.reflection.factory.DefaultObjectFactory.instantiateClass(DefaultObjectFactory.java:60)
... 47 more
这个问题困扰我半天,都是按照标准的格式配置的Mapper.xml映射文件,最后还是报错
究其原因,给兄弟们罗列了如下的几个排错方案:
检查自己的Mapper.xml配置文件和接口类是否绑定正确
接口:
public interface StudentMapper {
//查询学生和老师
List<Student> getStudent();
}
Mapper.xml:
注意下面的namespace要写接口的全限定名
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--configuration:核心配置文件-->
<mapper namespace="com.guohui.dao.StudentMapper">
检查Mapper.xml文件中的查询id是否和接口中的方法一致
接口和xml文件:
public interface StudentMapper {
//查询学生和老师
List<Student> getStudent();
}
<select id="getStudent" resultType="com.guohui.pojo.Student">
select * from student
</select>
我半天忽略的一点,希望兄弟们不要犯我这样低级错误
检查Maapper.xml配置文件中的resultType是否是实体类的全限定名(如果在config.xml核心配置文件中设定了别名可以写别名)
我就是把这个返回值类型写成了接口,所以就G了
4.检查我们的Mapper.xml配置文件是否在config.xml核心配置文件中进行注册,注意:每新建一个Mapper.xml文件,都养成习惯去核心配置文件中进行注册,注册方式多种多样,以下进行简单罗列:
(1)使用resources
<!-- 每一个Mapper.xml文件都需要在我们的Mybatis核心配置文件中进行注册映射-->
<!-- mappers:映射器-->
<mappers>
<!-- 这里要写Mapper.xml的全限定名-->
<mapper resource="com/guohui/dao/UserMapper.xml"/>
</mappers>
(2)使用Class
<!-- 每一个Mapper.xml文件都需要在我们的Mybatis核心配置文件中进行注册映射-->
<!-- mappers:映射器-->
<mappers>
<!-- 这里要写同一个包下的接口的全限定名-->
<mapper class="com.guohui.dao.StudentMapper"/>
<mapper class="com.guohui.dao.TeacherMapper"/>
</mappers>
(3)使用Package
<!-- 每一个Mapper.xml文件都需要在我们的Mybatis核心配置文件中进行注册映射-->
<!-- mappers:映射器-->
<mappers>
<!--使用package,name就写包名即可,mybatis会扫描下面的所有相关的xml文件-->
<package name="com.guohui.dao"/>
</mappers>
希望以上的排错方法对兄弟们有用,欢迎指正!