iteye_7290
iteye_7290
2011-08-25 09:59
浏览 832
已采纳

struts1整合mybatis【java.io.IOException: Could not find resource configuration.xml】

[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.(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]

应该是路径写的不对,但是改了几次,都不行。。。。
附上我的目录结构。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

5条回答 默认 最新

  • AngelAndAngel
    AngelAndAngel 2011-08-25 10:08
    已采纳

    你这个还是放到src下面比较好点。

    点赞 评论
  • AngelAndAngel
    AngelAndAngel 2011-08-25 10:01

    你的configuration.xml是放在src下面吗 是的话就用
    String resource = "/configuration.xml";

    点赞 评论
  • AngelAndAngel
    AngelAndAngel 2011-08-25 10:09

    不然的话得在action里面加上
    String fileName = request.getSession().getServletContext().getRealPath(
    "/")
    这个是服务器路径,然后再加上web-inf/你的文件。

    点赞 评论
  • renpeng301
    renpeng301 2011-08-25 10:10

    [code="java"] String resource = "/WEB-INF/configuration.xml"; [/code]

    点赞 评论
  • rainbow702
    rainbow702 2011-08-25 10:14

    你好,你可以试下这么写:

    [code="java"]
    String resource = "org/mybatis/example/Configuration.xml";

    Reader reader = Resources.getResourceAsReader(resource);

    sqlMapper = new SqlSessionFactoryBuilder().build(reader);
    [/code]

    其中,"org/mybatis/example/Configuration.xml"就是 Configuration.xml 的位置了。

    另外,推荐你看一下这个帖子,上面讲得比较详细:
    http://dongwei.iteye.com/blog/705519

    点赞 评论

相关推荐