MyBatis配置mybatis-config中mappers报错-解决方案

news/2024/7/24 10:53:46 标签: MyBatis, ERROR, 配置

xlecho编辑整理,欢迎转载,转载请声明文章来源。欢迎添加echo微信(微信号:t2421499075)交流学习。 百战不败,依不自称常胜,百败不颓,依能奋力前行。——这才是真正的堪称强大!!


在项目中的mybatis-config.mxl中配置了如下配置

<mappers>
    <package name="com.echo.springmybatis.mapper"/>
</mappers>

项目结构如下

项目结构

启动测试类CountryMapperTest报如下错误

org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for com.echo.springmybatis.sql.mapper.CountryMapper.selectAll
### Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for com.echo.springmybatis.sql.mapper.CountryMapper.selectAll

	at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
	at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:150)
	at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:141)
	at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:136)
	at com.echo.springmybatis.CountryMapperTest.testSelectAll(CountryMapperTest.java:20)
	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.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
	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:68)
	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for com.echo.springmybatis.sql.mapper.CountryMapper.selectAll
	at org.apache.ibatis.session.Configuration$StrictMap.get(Configuration.java:888)
	at org.apache.ibatis.session.Configuration.getMappedStatement(Configuration.java:721)
	at org.apache.ibatis.session.Configuration.getMappedStatement(Configuration.java:714)
	at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:147)
	... 26 more

引起原因

这个错误的起因是配置mappers的时候出现的错误,没有注意到mappers中配置package他的特性“必须保证接口名(例如CountryMapper)和xml名(CountryMapper.xml)相同,还必须在同一个包中。”。我的接口和xml明显不在同一个地方。

解决办法

  • 将对应的xml文件放到对应的接口文件下面(还会报错,因为IDEA maven项目默认不会把src下除java文件外的文件打包到classes文件夹下,所以我们还要第二步配置
  • pom文件中加入打包其他文件的配置

pom文件配置如下

<build>
    <resources>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.xml</include>
            </includes>
            <!--默认是true-->
            <!--<filtering>true</filtering>-->
        </resource>
    </resources>
</build>

再次启动测试文件,发现已经能够成功运行了

项目coding地址:https://coding.net/u/xlsorry/p/springmybatis/git


http://www.niftyadmin.cn/n/1639063.html

相关文章

[Discuz]论坛搭建step by step

摘要&#xff1a;本文介绍如何从头开始搭建Discuz论坛&#xff0c;涉及具体内容为apache php tomcat mysql 等工具的配置和使用。 步骤&#xff1a; 1、环境配置以及基础工具安装 2、整合配置 3、安装Discuz 4、相关问题解决 1、环境配置以及基础工具安装 ------------…

MyBatis获取新增数据ID的几种方法

xlecho编辑整理&#xff0c;欢迎转载&#xff0c;转载请声明文章来源。欢迎添加echo微信(微信号&#xff1a;t2421499075)交流学习。 百战不败&#xff0c;依不自称常胜&#xff0c;百败不颓&#xff0c;依能奋力前行。——这才是真正的堪称强大&#xff01;&#xff01; 参考书…

代码之丑(九)——退让的缩进

这是一个让我纠结了很久的话题&#xff1a;缩进。 for (int j 0; j < attributes.size(); j) {Attr *attr attributes.get(j);if (attr NULL ) {continue;}int IsCallFunc -1;if(attr->status() STATUS_NEW || attr->status() STATUS_MODIFIED) {if(strcmp(attr…

MyBatis注解开发报错org.apache.ibatis.builder.IncompleteElementException-解决方案

xlecho编辑整理&#xff0c;欢迎转载&#xff0c;转载请声明文章来源。欢迎添加echo微信(微信号&#xff1a;t2421499075)交流学习。 百战不败&#xff0c;依不自称常胜&#xff0c;百败不颓&#xff0c;依能奋力前行。——这才是真正的堪称强大&#xff01;&#xff01; 出现错…

推荐一个不错的免费svn服务器unfuddle

www.unfuddle.com 经测试&#xff0c;速度还可以 对于本人开发的B2C商城&#xff0c;你可以通过 svn co http://normandy.unfuddle.com/svn/normandy_normandypositionii/NormandyPositionII 获取&#xff0c;具体的账户名密码申请请发送邮件至 quzishengmail.com

MyBatis注解开发-获取刚插入数据的主键

xlecho编辑整理&#xff0c;欢迎转载&#xff0c;转载请声明文章来源。欢迎添加echo微信(微信号&#xff1a;t2421499075)交流学习。 百战不败&#xff0c;依不自称常胜&#xff0c;百败不颓&#xff0c;依能奋力前行。——这才是真正的堪称强大&#xff01;&#xff01;&#…

MyBatis中like的两种写法

xlecho编辑整理&#xff0c;欢迎转载&#xff0c;转载请声明文章来源。欢迎添加echo微信(微信号&#xff1a;t2421499075)交流学习。 百战不败&#xff0c;依不自称常胜&#xff0c;百败不颓&#xff0c;依能奋力前行。——这才是真正的堪称强大&#xff01;&#xff01;&#…

JAVA精确向txt文件输出换行符

当写文件使用writer.write("/n");的方式时候&#xff0c;在txt文件中会被输出为一个黑色方块&#xff0c;当然在editplus等编辑器下&#xff0c;是一个换行符。 但是当读取的时候&#xff0c;这个会被当做一个字符读进来&#xff0c;所以不是严格意义的换行符。 使…