在mapper代理的开发中,程序员需要遵守一些规范,mybatis才能实现mapper接口的代理对象。
它的规范如下:
- mapper.xml的namespace要写所映射接口的全称类名。
- mapper.xml中的每个statement的id要和接口方法的方法名相同
- mapper.xml中定义的每个sql的parameterType要和接口方法的形参类型相同
- mapper.xml中定义的每个sql的resultType要和接口方法的返回值的类型相同
- mapper.xml要和对应的mapper接口在同一个包下
- mapper.xml的命名规范遵守: 接口名+Mapper.xml
如果以上检查完毕,项目都遵守了上述的规范,而且你的项目是Maven项目。但是运行程序还是会出现Mybatis invalid bound statement(not found)的问题,这个时候就需要修改pom.xml文件了。在pom.xml中添加如下的代码:
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<!-- 此配置不可缺,否则mybatis的Mapper.xml将会丢失 -->
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<!--指定资源的位置-->
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>