运行mybatis项目时找不到Mapper.xml配置文件

运行mybatis项目时找不到Mapper.xml配置文件

错误提示如下:


Caused by: org.apache.ibatis.exceptions.PersistenceException: 
### Error building SqlSession.
### The error may exist in priv/sehun/dao/user/UserMapper.xml
### Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: java.io.IOException: Could not find resource priv/sehun/dao/user/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 priv.sehun.utils.MyBatisUtils.<clinit>(MyBatisUtils.java:20)
	... 23 more

原因:在IDEA的maven项目中,默认源代码目录下(src/main/java目录)的xml等资源文件并不会在编译的时候一块打包进【target】目录下classes文件夹

而我们运行时,是在target目录下去找Mapper.xml映射文件的,自然就会找不到了
运行mybatis项目时找不到Mapper.xml配置文件_第1张图片

解决方案:

1.在pom.xml中加一段代码:

 <build>
        <resources>
            <resource>
                <directory>src/main/javadirectory>
                <includes>
                    <include>**/*.propertiesinclude>
                    <include>**/*.xmlinclude>
                includes>
                <filtering>falsefiltering>
            resource>
            <resource>
                <directory>src/main/resourcesdirectory>
                <includes>
                    <include>**/*.propertiesinclude>
                    <include>**/*.xmlinclude>
                includes>
                <filtering>falsefiltering>
            resource>
        resources>

    build>

这段代码会让maven在导出项目时,将我们的配置及资源导入classes文件夹里

2.直接手动将Mapper.xml映射文件复制到target/classes目录下的对应位置。

【注意】

不要忘记在mybatis配置文件中关联你的映射文件

mybatis.config.xml

  
    <mappers>
        <mapper resource="priv/sehun/dao/userMapper.xml"/>
    mappers>

你可能感兴趣的:(其它)