1.Mybatis 实现多表查询方式
1.1 业务装配.对两个表编写单表查询语句,在业务(Service)把查询的两个结果进行关联
1.2 使用AutoMapping特性,在实现两表联合查询时通过别名完成映射.
1.3 使用 MyBatis 的<resultMap>标签进行实现.
2.多表查询时,类中包含另一个类的对象的分类
2.1 单个对象
2.2 集合对象.
3.resultMap 标签
3.1 <resultMap>标签写在mapper.xml中,由程序员控制SQL查询结果与实体类的映射关系.
3.2 默认 MyBatis 使用 AutoMapping 特性.
3.3使用<resultMap>标签时,<select>标签不写 resultType 属性,而是使用 resultMap 属性引用<resultMap>标签.
4. 使用 resultMap 实现单表映射关系
4.1 数据库设计
4.2 实体类设计
4.3mapper.xml 代码
<resultMap type="teacher" id="mymap">
<!-- 主键使用 id 标签配置映射关系 -->
<id column="id" property="id1" />
<!-- 其他列使用 result 标签配置映射关系 -->
<result column="name" property="name1"/>
</resultMap>
<select id="selAll" resultMap="mymap"> select * from teacher </select>
5. 使用 resultMap 实现关联单个对象(N+1 方式)
5.1N+1 查询方式,先查询出某个表的全部信息,根据这个表的信息
查询另一个表的信息.
5.2 与业务装配的区别:
5.3.1 在 service 里面写的代码,由 mybatis 完成装配
5.3 实现步骤:
5.3.1 在 Student 实现类中包含了一个 Teacher 对象
public class Student {
private int id;
private String name;
private int age;
private int tid;
private Teacher