mybatis報錯

Sept_zhang發表於2020-09-28

20200928資源匯出失敗的問題

方案一:

控制檯報錯資訊:

java.lang.ExceptionInInitializerError
	at com.ajie.mapper.UserDaoTest.test(UserDaoTest.java:18)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:566)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
	at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
	at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
	at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
	at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
	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/ajie/mapper/UserMapper.xml
### Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: java.io.IOException: Could not find resource com/ajie/mapper/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.ajie.utils.MyBatiUtils.<clinit>(MyBatiUtils.java:25)
	... 26 more
Caused by: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: java.io.IOException: Could not find resource com/ajie/mapper/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)
	... 28 more
Caused by: java.io.IOException: Could not find resource com/ajie/mapper/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)
	... 30 more


Process finished with exit code -1

解決方法:

pox.xml中新增:

    <!--在build中配置resources,來防止我們資源匯出失敗的問題-->
    <build>
        <resources>
            <!--可以不加這個 resource ,idea預設匯出這個-->
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
            <!--必須加這個 resource -->
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>

問題分析:

報錯的原因是 java 包下存在xml檔案不能匯出:如果把資原始檔放在java目錄下,idea資源匯出會出現失敗,不會在對應的class檔案中生成對應的資原始檔。

錯誤資訊 The error may exist in com/ajie/mapper/UserMapper.xml,Cause: java.io.IOException: Could not find resource com/ajie/mapper/UserMapper.xml

解決分析:

設定Maven靜態資源過濾

<resource>
    <!--匯出src/main/resources 裡的資源 -->
    <directory>src/main/resources</directory>
    <includes>
        <!--包含 *.properties和*.xml檔案-->
        <include>**/*.properties</include>
        <include>**/*.xml</include>
    </includes>
    <filtering>true</filtering>
</resource>
<resource>
    <!--匯出src/main/java 裡的資源 -->
    <directory>src/main/java</directory>
    <includes>
        <include>**/*.properties</include>
        <include>**/*.xml</include>
    </includes>
    <filtering>true</filtering>
</resource>

方案二:

解決思路:

mapper.xml檔案放在resources 目錄下,idea會正常資源匯出,並在對應的class檔案中生成對應的資原始檔。

目錄

解決方法:3

1.我的mapper.xml存放目錄mybatis/mapper/UserMapper.xml.

2.修改mybatis-config.xml檔案

<!--每一個Mapper.XML都需要在Mybatis核心配置檔案中註冊!-->
<mappers>
    <mapper resource="mybatis/mapper/UserMapper.xml"/>
</mappers>

作者隨著學習,持續更新…

相關文章