使用mybatis连接数据库报错Could not find resource com/qpf/dao/userMapper.xml

整个的报错信息为

D:\Software\jdk15\bin\java.exe -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:D:\Software\IDEA\IntelliJ IDEA 2020.2.2\lib\idea_rt.jar=6147:D:\Software\IDEA\IntelliJ IDEA 2020.2.2\bin" -Dfile.encoding=GBK -classpath "D:\Software\IDEA\IntelliJ IDEA 2020.2.2\lib\idea_rt.jar;D:\Software\IDEA\IntelliJ IDEA 2020.2.2\plugins\junit\lib\junit5-rt.jar;D:\Software\IDEA\IntelliJ IDEA 2020.2.2\plugins\junit\lib\junit-rt.jar;E:\springboot\Mybatis\Mybatis-Study\mybatis-01\target\test-classes;E:\springboot\Mybatis\Mybatis-Study\mybatis-01\target\classes;D:\Software\maven-jar\mysql\mysql-connector-java\5.1.46\mysql-connector-java-5.1.46.jar;D:\Software\maven-jar\org\mybatis\mybatis\3.5.2\mybatis-3.5.2.jar;D:\Software\maven-jar\junit\junit\4.12\junit-4.12.jar;D:\Software\maven-jar\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar" com.intellij.rt.junit.JUnitStarter -ideVersion5 -junit4 com.qpf.dao.UserDaoTest,test

java.lang.ExceptionInInitializerError
	at com.qpf.dao.UserDaoTest.test(UserDaoTest.java:16)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:564)
	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:220)
	at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:53)
Caused by: org.apache.ibatis.exceptions.PersistenceException: 
### Error building SqlSession.
### The error may exist in com/qpf/dao/userMapper.xml
### Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: java.io.IOException: Could not find resource com/qpf/dao/userMapper.xml
	at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
	at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:80)
	at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:64)
	at com.qpf.utils.MybatisUtils.<clinit>(MybatisUtils.java:20)
	... 23 more
Caused by: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: java.io.IOException: Could not find resource com/qpf/dao/userMapper.xml
	at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:121)
	at org.apache.ibatis.builder.xml.XMLConfigBuilder.parse(XMLConfigBuilder.java:98)
	at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:78)
	... 25 more
Caused by: java.io.IOException: Could not find resource com/qpf/dao/userMapper.xml
	at org.apache.ibatis.io.Resources.getResourceAsStream(Resources.java:114)
	at org.apache.ibatis.io.Resources.getResourceAsStream(Resources.java:100)
	at org.apache.ibatis.builder.xml.XMLConfigBuilder.mapperElement(XMLConfigBuilder.java:372)
	at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:119)
	... 27 more


Process finished with exit code -1

有的人说这是因为xml文件里面存在中文,需要删除或者更换编码方式,改为GBK,但是 我修改了也没用(我使用的jdk是15,结果测试发现,不管有没有中文注释都不影响结果)

顺便说一下,可以在maven里面直接指定jdk版本,避免与IDEA版本不同引发报错,具体代码为

<properties>
        <!-- 项目编码 -->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <!-- 本地编译JDK版本 -->
        <maven.compiler.source>15</maven.compiler.source>
        <!-- 项目部署JDK版本 -->
        <maven.compiler.target>15</maven.compiler.target>
    </properties>

后来发现,这里是Maven静态资源过滤的问题,明明在工程中存在userMapper.xml文件,但还是提示找不到
Could not find resource com/qpf/dao/userMapper.xml

这是因为maven约定大于配置,我们可能会遇到所写的配置文件无法导出或生效的问题,在pom.xml文件加入下面代码就行

<build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
    </build>

这样的话就可以成功输出了

如果出现了下面的界面
在这里插入图片描述
这是因为没有在实体类中定义toString方法

@Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", pwd='" + pwd + '\'' +
                '}';
    }

定义好了之后,再次测试,就成功了
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值