org.apache.ibatis.exceptions.PersistenceException错误和Caused by: java.lang.NoSuchMethodException

排错方案见后文

报错日志如下:

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映射文件,最后还是报错

究其原因,给兄弟们罗列了如下的几个排错方案:

  1. 检查自己的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">
  1. 检查Mapper.xml文件中的查询id是否和接口中的方法一致

接口和xml文件:

public interface StudentMapper {
    //查询学生和老师
     List<Student> getStudent();
}
   <select id="getStudent" resultType="com.guohui.pojo.Student">
        select * from student
    </select>
  1. 我半天忽略的一点,希望兄弟们不要犯我这样低级错误

检查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>

希望以上的排错方法对兄弟们有用,欢迎指正!

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Be explorer

若认可笔者文章,手头富裕盼支持

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值