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 文件,测试类中通过SqlSe
·
项目场景:
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>
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐



所有评论(0)