liminxincs
2019-04-26 13:28
采纳率: 100%
浏览 1.1k
已采纳

idea测试出错,一直报xml文件错误

写了一个实验类,运行结果。出现错误信息如下代码,请大神帮忙指导一下哪里错误了

Exception in thread "main" org.apache.ibatis.exceptions.PersistenceException: 
### Error building SqlSession.
### The error may exist in SoneMybatis/SonMybatis.xml
### Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: java.io.IOException: Could not find resource SoneMybatis/SonMybatis.xml
    at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:26)
    at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:82)
    at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:66)
    at com.cwj.Text.Text.Text1(Text.java:42)
    at com.cwj.Text.Text.main(Text.java:34)
Caused by: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: java.io.IOException: Could not find resource SoneMybatis/SonMybatis.xml
    at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:109)
    at org.apache.ibatis.builder.xml.XMLConfigBuilder.parse(XMLConfigBuilder.java:92)
    at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:80)
    ... 3 more
Caused by: java.io.IOException: Could not find resource SoneMybatis/SonMybatis.xml
    at org.apache.ibatis.io.Resources.getResourceAsStream(Resources.java:110)
    at org.apache.ibatis.io.Resources.getResourceAsStream(Resources.java:97)
    at org.apache.ibatis.builder.xml.XMLConfigBuilder.mapperElement(XMLConfigBuilder.java:320)
    at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:107)
    ... 5 more

``
测试类:
/**
 * Copyright (C), 2015-2019, XXX有限公司
 * FileName: Text
 * Author:   50427
 * Date:     2019/4/25 21:00
 * Description:
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.cwj.Text;

import com.cwj.User.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.logging.log4j.core.helpers.SystemClock;

import java.io.IOException;
import java.io.InputStream;

/**
 * 〈一句话功能简述〉<br> 
 * 〈〉
 *
 * @author 50427
 * @create 2019/4/25
 * @since 1.0.0
 */
public class Text {
    public static void main(String[] args) {
        try {
            Text1();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private static void Text1() throws IOException {
        InputStream input = Resources.getResourceAsStream("Mybatis.xml");

        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(input);
        SqlSession session =factory.openSession();
        User user=session.selectOne("Mybatis.xml",1);
        System.out.print(user);
        session.commit();
    }

}


映射xml文件:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--
    namespace:命名空间,它的作用就是对SQL进行分类化管理,可以理解为SQL隔离
    注意:使用mapper代理开发时,namespace有特殊且重要的作用
 -->
<mapper namespace="test">
    <!--
        [id]:statement的id,要求在命名空间内唯一
        [parameterType]:入参的java类型
        [resultType]:查询出的单条结果集对应的java类型
        [#{}]: 表示一个占位符?
        [#{id}]:表示该占位符待接收参数的名称为id。注意:如果参数为简单类型时,#{}里面的参数名称可以是任意定义
     -->
    <!-- id查询-->
    <select id="findUserById" parameterType="int" resultType="com.cwj.User.User">
        SELECT * FROM u_login WHERE uid = #{uid}
    </select>
    <!-- 模糊查询-->
    <select id="findUserBy" parameterType="String" resultType="com.cwj.User.User">
        SELECT * FROM u_login WHERE uname like %${value}%
    </select>
</mapper>


全局文件xml:

<?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>
    <!-- 配置mybatis的环境信息 -->
    <environments default="development">
        <environment id="development">
            <!-- 配置JDBC事务控制,由mybatis进行管理 -->
            <transactionManager type="JDBC"></transactionManager>
            <!-- 配置数据源,采用dbcp连接池 -->
            <dataSource type="POOLED">
                <property name="driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
            <property name="url" value="jdbc:sqlserver://localhost:1433;DatabaseName=Login?useUnicode=true&amp;characterEncoding=utf-8"/>
            <property name="username" value="sa"/>
            <property name="password" value="123456"/>
        </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="SoneMybatis/Soner.xml"></mapper>
    </mappers>
</configuration>





log4j.properties文件:

# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

![图片说明]([图片](https://img-ask.csdn.net/upload/201904/26/1556265572_660705.png "")
https://img-ask.csdn.net/upload/201904/26/1556265572_660705.png)
![图片说明](https://img-ask.csdn.net/upload/201904/26/1556265668_459453.png)
![图片说明](https://img-ask.csdn.net/upload/201904/26/1556265677_469377.png)
![图片说明](https://img-ask.csdn.net/upload/201904/26/1556265693_482838.png)
![图片说明](https://img-ask.csdn.net/upload/201904/26/1556265699_520514.png)
![图片说明](https://img-ask.csdn.net/upload/201904/26/1556265706_599402.png)
![图片说明](https://img-ask.csdn.net/upload/201904/26/1556265793_940721.png)








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

1条回答 默认 最新

  • ghq-yes 2019-04-26 13:35
    已采纳

    图片说明
    图片找不到,问题里面是可以贴图片的
    这个问题,找不到配置文件,一看配置里面路径,二看加载,你自己找下在哪里吧

    点赞 打赏 评论

相关推荐 更多相似问题