Error building SqlSession Could not find resource org/spring/Mapper/UserMapper.xml

编译器:Maven Idea

Mybatis初学的时候遇到的错误如下:

Exception in thread "main" org.apache.ibatis.exceptions.PersistenceException: 
### Error building SqlSession.
### The error may exist in org/spring/Mapper/UserMapper.xml
### Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause:         java.io.IOException: Could not find resource org/spring/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 MyBatis.MybatisTest.main(MybatisTest.java:20)


按照上面读取错误的原因是没有找到配置文件

我的文件目录是:

Error building SqlSession Could not find resource org/spring/Mapper/UserMapper.xml_第1张图片


我的配置文件是:

xml version="1.0" encoding="UTF-8" ?>
configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">



    
    
        name="logImpl" value="LOG4J"/>
    

    default="mysql">
        
        id="mysql">
            
            type="JDBC"/>
            
            type="POOLED">
                name="driver"  value="com.mysql.jdbc.Driver"/>
                name="url"  value="jdbc:mysql:///mybatis"/>
                name="username"  value="root"/>
                name="password"  value="123456"/>
            
        
    

    
    
        resource="org/spring/Mapper/UserMapper.xml"/>
    




我的Mapper文件是:

xml version="1.0" encoding="UTF-8"?>
mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

namespace="org.spring.Mapper.UserMapper">

    
    
    
    id="save" parameterType="org.spring.domain.User"
            useGeneratedKeys="true">
        INSERT INTO TB_USER(name,sex,age)
        VALUES(#{name},#{sex},#{age})
    

我的测试程序是:

package MyBatis;

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.spring.domain.User;

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

public class MybatisTest {

    public static void main(String[] args) {
        //读取配置文件
        try {
            InputStream inputStream=Resources.getResourceAsStream("mybatis-config.xml");

            //创建SqlSessionFactory的实例
            SqlSessionFactory sessionFactory= new SqlSessionFactoryBuilder().build(inputStream);

            //从工厂获取一个Session
            SqlSession session=sessionFactory.openSession();
            //创建User实例
            User user=new User("admin","男",23);
            //插入数据
            session.insert("classes.Mapper.UserMapper.save",user);
            //提交事物
            session.commit();
            //关闭事物
            session.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}


错误原因:将Usermapper.xml文件放到src/java目录下,Idea编译不出在java下的xml文件,需要将xml文件放到resources目录下才行

修正后的目录结构:

Error building SqlSession Could not find resource org/spring/Mapper/UserMapper.xml_第2张图片


你可能感兴趣的:(Mybatis)