mybatis的resultMap
多对一
使用 resultMap 属性,在查询语句返回时使用 resultMap 属性
<!--
resultMap :自定义映射,主要处理复杂的表关系
- type : 处理的类型,
- id : 唯一标识
-->
<resultMap id="empMap" type="lyy.bean.Emp">
<!--
解决多表查询的问题,只需要 id 和 result 标签
- id : 设置主键的映射关系
- column : 数据库的字段名
- property : 实体类的属性名
- result : 设置其他的映射关系
- column : 数据库的字段名
- property : 实体类的属性名
-->
<id column="eid" property="eid"></id>
<result column="ename" property="ename"/>
<result column="age" property="age"/>
<result column="sex" property="sex"/>
<result column="did" property="dept.did"/>
<result column="dname" property="dept.dname"/>
</resultMap>
<!--
使用 association 完成多对一
-->
<resultMap id="empMapByAss" type="lyy.bean.Emp">
<id column="eid" property="eid"></id>
<result column="ename" property="ename"/>
<result column="age" property="age"/>
<result column="sex" property="sex"/>
<!--
association : 主要处理一对一和多对一,
会根据查询出来的 did 和 dname 创建出 javaType 类型的对象,然后在赋值给 association property 的属性
- property : 对应的的实体类中的字段
- javaType : 设置其类型(创建的对象类型)
-->
<association property="dept" javaType="lyy.bean.Dept">
<id column="did" property="did"/>
<result column="dname" property="dname"/>
</association>
</resultMap>
<!--
多对一,分步查询,使用 association 的 select 属性
property : 处理的哪一个语句
select : 选择需要执行的 sql 语句,需要使用(namespace + id)
column : 需要执行条件的参数值,这个歌参数必须是从数据库查询出来的
-->
<resultMap id="empMapStep" type="lyy.bean.Emp">
<id column="eid" property="eid"></id>
<result column="ename" property="ename"/>
<result column="age" property="age"/>
<result column="sex" property="sex"/>
<association property="dept" select=
