通过 mapper 接口加载映射文件,这对于后面 ssm三大框架 的整合是非常重要的。那么什么是通过 mapper 接口加载映射文件呢?
我们首先看以前的做法,在全局配置文件 mybatis-configuration.xml 通过 <mappers> 标签来加载映射文件,那么如果我们项目足够大,有很多映射文件呢,难道我们每一个映射文件都这样加载吗,这样肯定是不行的,那么我们就需要使用 mapper 接口来加载映射文件
以前的做法:

1、定义 userMapper 接口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | |
2、在全局配置文件 mybatis-configuration.xml 文件中加载 UserMapper 接口(单个加载映射文件)
3、编写UserMapper.xml 文件
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | |
4、测试
|
5、批量加载映射文件
|
6、注意
1、UserMapper 接口必须要和 UserMapper.xml 文件同名且在同一个包下,也就是说 UserMapper.xml 文件中的namespace是UserMapper接口的全类名
2、UserMapper接口中的方法名和 UserMapper.xml 文件中定义的 id 一致
3、UserMapper接口输入参数类型要和 UserMapper.xml 中定义的 parameterType 一致
4、UserMapper接口返回数据类型要和 UserMapper.xml 中定义的 resultType 一致