第一章 Mybatis中自动映射与自定义映射
自动映射【resultType】
自定义映射【resultMap】
1.1 自动映射与自定义映射
- 自动映射【resultType】:指的是自动将表中的字段与类中的属性进行关联映射
- 自动映射解决不了两类问题
- 多表连接查询时,需要返回多张表的结果集
- 单表查询时,不支持驼峰式自动映射【不想为字段定义别名】
- 自动映射解决不了两类问题
- 自定义映射【resultMap】:自动映射解决不了问题,交给自定义映射
- 注意:resultType与resultMap只能同时使用一个
1.2 自定义映射-级联映射
<!-- 自定义映射 【员工与部门关系】-->
<resultMap id="empAndDeptResultMap" type="employee">
<!-- 定义主键字段与属性关联关系 -->
<id column="id" property="id"></id>
<!-- 定义非主键字段与属性关联关系-->
<result column="last_name" property="lastName"></result>
<result column="email" property="email"></result>
<result column="salary" property="salary"></result>
<!-- 为员工中所属部门,自定义关联关系-->
<result column="dept_id" property="dept.deptId"></result>
<result column="dept_name" property="dept.deptName"></result>
</resultMap>
<select id="select