多对一的理解
多个学生对应一个老师
搭建测试环境
1.IDEA安装Lombok插件(这个插件可以帮助自动生成set,get方法很方便)
2.引入Maven依赖

在代码中增加注释


4.编写实体类对应的Mapper接口

5.编写Mapper接口对应的mapper.xml配置文件
StudentMapper.xml,TeacherMapper.xml

按查询嵌套处理
1.给StudentMapper接口增加方法

2.编写对应的Mapper文件
从学生表和老师表中查出学生id,学生姓名,老师姓名
对查出的结果做结果集映射,集合使用collection.
JavaType和ofType都是用来指定对象类型的
JavaType指定pojo中属性的类型
ofType指定的是映射到list集合属性中pojo的类型

按结果嵌套处理
1.TeacherMapper接口编写方法

2.编写接口对应的Mapper配置文件

3.将Mapper文件注册到MyBatis-config文件中
4.测试

总结
关联-association用于一对一和多对一
集合-collection用于一对多
JavaType指定pojo中属性的类型
ofType指定映射到list集合属性中pojo的类型
网友评论