欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java开发遇到的mybatis报错解决方法

程序员文章站 2022-06-21 22:02:59
20200928资源导出失败的问题20200928资源导出失败的问题20200928资源导出失败的问题方案一:控制台报错信息:解决方法:问题分析:解决分析:方案二:解决思路:解决方法:3作者随着学习,持续更新...方案一:控制台报错信息:java.lang.ExceptionInInitializerErrorat com.ajie.mapper.UserDaoTest.test(UserDaoTest.java:18)at java.base/jdk.internal.reflect.Na...

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文件中生成对应的资源文件。

Java开发遇到的mybatis报错解决方法

解决方法:3

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

2.修改mybatis-config.xml文件

<!--每一个Mapper.XML都需要在Mybatis核心配置文件中注册!--> <mappers> <mapper resource="mybatis/mapper/UserMapper.xml"/> </mappers> 

本文地址:https://blog.csdn.net/weixin_43369210/article/details/108859619

相关标签: java MyBatis