java.io.IOException: Could not find resource com/xxx/dao/StudentDao.xml

项目场景:

maven 中使用mybatis 找不到 mapper.xml 文件 Configuration. Cause: java.io.IOException: Could not find resource com/xxx/dao/StudentDao.xml


问题描述

初学mybatis, 连接数据库查询信息,在mybatis主配置文件中配置 mapper.xml 文件,测试类中通过SqlSessionFactoryBuilder 读取mapper.xml 文件找不到
在这里插入图片描述


    public void Test01() throws IOException {
        String config = "mybatis.xml";

        InputStream in = Resources.getResourceAsStream(config);
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        SqlSessionFactory factory = builder.build(in);
        SqlSession sqlSession = factory.openSession();
        List<Student> students = sqlSession.selectList("com.xxx.dao.StudentDao.selectAll");
        students.forEach(student -> System.out.println(student));
        sqlSession.close();
    }

原因分析:

读取 mapper.xml 文件是从 target/classes 下去找,在 pom 文件中未手动配置的情况下,只会将 java 文件编译后的 class 文件放到 target/classes 目录之下,当前未配置,所以找不到 :


解决方案:

在 pom 文件中加上配置,让目录下的 xml 文件会被扫描到

 <build>
	 <resources>
		<resource>
			 <directory>src/main/java</directory><!--所在的目录-->
			 <includes><!--包括目录下的.properties,.xml 文件都会扫描到-->
			 <include>**/*.properties</include>
			 <include>**/*.xml</include>
			 </includes>
			 <filtering>false</filtering>
		 </resource>
	</resources>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值