问题:
在mybatis-config.xml文件里的<mappers></mappers>
里配置class映射时,xml文件已和接口文件放在同一路径下,运行仍报错
<!--class:映射单个文件 ,指定的接口文件要遵循指定规则:mapper.xml和mapper.java必须是同一路径下,且名称保持相同-->
<mapper class="com.tulun.maventest.mapper.StudentMapper"/>
错误信息如下:
Exception in thread “main” org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.tulun.maventest.mapper.StudentMapper.selectStudentById
at org.apache.ibatis.binding.MapperMethod$SqlCommand.(MapperMethod.java:223)
原因:
java下的mapper文件里的接口对应的StudentMapper.xml文件并没有被加载
解决方案:
1.确保接口文件和其xml文件名字完全一致
2.在pom.xml文件里,找到<bulild></build>
标签,给里面添加如下代码。作用是让编辑器去resources和java路径下去加载xml文件。只有先加载到了,才能去用指定位置的xml文件。
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>**/*.tld</include>
<include>**/*.xls</include>
<include>**/*.xlsx</include>
</includes>
<!--<filtering>true</filtering>-->
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.tld</include>
<include>**/*.xls</include>
<include>**/*.xlsx</include>
<include>**/*.xml</include>
</includes>
<!--<filtering>true</filtering>-->
</resource>
</resources>
3.编辑完重新运行即可