mybatis错误——java.io.IOException: Could not find resource com/xxx/xxxMapper.xml

在学习Mybatis的时候,参考网上的教程进行简单demo的搭建,配置的没有问题,然后出现了下面的错误!

  Exception in thread "main" java.lang.RuntimeException: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: java.io.IOException: Could not find resource com/mybatis/mapper/StudentMapper.xml
	at com.mybatis.util.MyBatisSqlSessionFactory.getSqlSessionFactory(MyBatisSqlSessionFactory.java:33)
	at com.mybatis.util.MyBatisSqlSessionFactory.main(MyBatisSqlSessionFactory.java:44)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:606)
	at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Caused by: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: java.io.IOException: Could not find resource com/mybatis/mapper/StudentMapper.xml
	at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:106)
	at org.apache.ibatis.builder.xml.XMLConfigBuilder.parse(XMLConfigBuilder.java:89)
	at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:77)
	at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:63)
	at com.mybatis.util.MyBatisSqlSessionFactory.getSqlSessionFactory(MyBatisSqlSessionFactory.java:31)
	... 6 more
Caused by: java.io.IOException: Could not find resource com/mybatis/mapper/StudentMapper.xml
	at org.apache.ibatis.io.Resources.getResourceAsStream(Resources.java:108)
	at org.apache.ibatis.io.Resources.getResourceAsStream(Resources.java:95)
	at org.apache.ibatis.builder.xml.XMLConfigBuilder.mapperElement(XMLConfigBuilder.java:315)
	at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:104)
	... 10 more

最终通过上网查找找到了三种解决方案,现在整理总结!希望可以帮助到其他人!
在说解决方案之前,先申明我的环境!我会用的开发工具是IDEA ,项目构建使用Maven!网上一些教程使用的Eclipse开发工具,项目是普通的java web项目,所以开发工具和构建项目不同就会存在一些出入(坑)!

我项目的目录和xxxMapper.xml的位置如下图:
这里写图片描述

原因:IDEA是不会编译src的java目录的xml文件,所以在Mybatis的配置文件中找不到xml文件!(也有可能是Maven构建项目的问题,网上教程很多项目是普通的Java web项目,所以可以放到src下面也能读取到)

解决方案1:

不将xml放到src目录下面,将xxxMapper.xml放到Maven构建的resource目录下面!

解决方案2:
在Maven的pom文件中,添加下面代码:

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

解决方案2,参考资料:mybatis 找不到映射器xml文件

解决方案3:

我测试时候只有 mapper resource 这种方式加载不到资源,其他的url class和package都可以,如果想解决问题的话,可以不使用resource这种方式!

亲测三种方式都可以!如果还有其他好的解决办法欢迎留言一起学习讨论!


欢迎访问我的csdn博客,我们一同成长!

不管做什么,只要坚持下去就会看到不一样!在路上,不卑不亢!

博客首页http://blog.csdn.net/u010648555

发布了300 篇原创文章 · 获赞 1890 · 访问量 157万+
展开阅读全文

关于mybatis中config找不到mapper中的xml

11-02

刚刚学习mybatis,尝试在写一个小项目,在配置config中遇到错误 org.apache.ibatis.exceptions.PersistenceException: ### Error building SqlSession. ### The error may exist in mapper/CategoryMapper.xml ### Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: java.io.IOException: Could not find resource mapper/CategoryMapper.xml at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:23) at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:79) at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:63) at util.MyBatisUtil.<clinit>(MyBatisUtil.java:17) at transaction.impl.MyBatisTransactionManager.beginTransaction(MyBatisTransactionManager.java:15) at service.proxy.RoomServiceProxy.findAllCategory(RoomServiceProxy.java:26) at action.Test.main(Test.java:10) Caused by: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: java.io.IOException: Could not find resource mapper/CategoryMapper.xml at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:105) at org.apache.ibatis.builder.xml.XMLConfigBuilder.parse(XMLConfigBuilder.java:88) at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:77) ... 5 more Caused by: java.io.IOException: Could not find resource mapper/CategoryMapper.xml at org.apache.ibatis.io.Resources.getResourceAsStream(Resources.java:104) at org.apache.ibatis.io.Resources.getResourceAsStream(Resources.java:91) at org.apache.ibatis.builder.xml.XMLConfigBuilder.mapperElement(XMLConfigBuilder.java:309) at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:103) ... 7 more Exception in thread "main" java.lang.ExceptionInInitializerError: MyBaitsUtil初始化错误org.apache.ibatis.exceptions.PersistenceException: ### Error building SqlSession. ### The error may exist in mapper/CategoryMapper.xml ### Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: java.io.IOException: Could not find resource mapper/CategoryMapper.xml at util.MyBatisUtil.<clinit>(MyBatisUtil.java:22) at transaction.impl.MyBatisTransactionManager.beginTransaction(MyBatisTransactionManager.java:15) at service.proxy.RoomServiceProxy.findAllCategory(RoomServiceProxy.java:26) at action.Test.main(Test.java:10) 我的mybatis-config <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd" > <configuration> <properties resource="database.properties"></properties> <typeAliases> <package name="entity"/> </typeAliases> <environments default="mybatis"> <environment id="mybatis"> <transactionManager type="JDBC"></transactionManager> <dataSource type="POOLED"> <property name="driver" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </dataSource> </environment> </environments> <mappers> <mapper resource="mapper/CategoryMapper.xml"/> </mappers> </configuration> 我的路径 ![图片说明](https://img-ask.csdn.net/upload/201611/03/1478106755_161057.png) 异常说找不到mpper/Category,可是我感觉我没写错呀 问答

struts1整合mybatisjava.io.IOException: Could not find resource configuration.xml

08-25

[code="java"] java.io.IOException: Could not find resource configuration.xml at org.apache.ibatis.io.Resources.getResourceAsStream(Resources.java:89) at org.apache.ibatis.io.Resources.getResourceAsStream(Resources.java:76) at org.apache.ibatis.io.Resources.getResourceAsReader(Resources.java:134) at com.s1sm.exer.utils.MyBatisUtil.<clinit>(MyBatisUtil.java:20) at com.s1sm.exer.dao.LoginDAO.findAllUsers(LoginDAO.java:19) at com.s1sm.exer.service.impl.LoginMgrImpl.login(LoginMgrImpl.java:16) at com.s1sm.exer.web.actions.LoginAction.execute(LoginAction.java:34) at org.apache.struts.chain.commands.servlet.ExecuteAction.execute(ExecuteAction.java:58) at org.apache.struts.chain.commands.AbstractExecuteAction.execute(AbstractExecuteAction.java:67) at org.apache.struts.chain.commands.ActionCommandBase.execute(ActionCommandBase.java:51) at org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:191) at org.apache.commons.chain.generic.LookupCommand.execute(LookupCommand.java:305) at org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:191) at org.apache.struts.chain.ComposableRequestProcessor.process(ComposableRequestProcessor.java:283) at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913) at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:462) at javax.servlet.http.HttpServlet.service(HttpServlet.java:637) at javax.servlet.http.HttpServlet.service(HttpServlet.java:717) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489) at java.lang.Thread.run(Thread.java:619) [/code] [code="java"] package com.s1sm.exer.utils; import java.io.IOException; import java.io.Reader; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; public class MyBatisUtil { private static SqlSessionFactory sqlSessionFactory = null; static { String resource = "configuration.xml"; Reader reader = null; try { reader = Resources.getResourceAsReader(resource); } catch (IOException e) { e.printStackTrace(); } //use SqlSessionFactoryBuilder to create SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); } public static SqlSessionFactory getSqlSessionFactory() { return sqlSessionFactory; } } [/code] 应该是路径写的不对,但是改了几次,都不行。。。。 附上我的目录结构。 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 数字20 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览