查询功能的标签必须设置 resultMap:设置自定义的映射关系 或 resultType:设置默认的映射关系 如果字段名和属性名不一致的情况或者一对多多对多情况用resultMap
resultMap设置自定义映射 只在查询功能中有用 其属性 id 唯一标识,不能重复 type 设置映射关系中的实体类类型
resultMap内置标签
id设置主键的映射关系,result设置普通字段的映射关系,
association处理多对一关系,collection处理一对多
其属性
property:设置映射关系中的属性名,即type属性所设置的实体类类型中的属性名
column:设置映射关系中的字段名,即sql语句查询出的字段名
<resultMap id="empResultMap" type="Emp">
<!-- id设置主键的映射关系
property:设置映射关系中的属性名,即type属性所设置的实体类类型中的属性名
column:设置映射关系中的字段名,即sql语句查询出的字段名
-->
<id property="eid" column="eid"></id>
<!-- result设置普通字段的映射关系-->
<result property="eName" column="emp_name"></result>
<result property="age" column="age"></result>
<result property="sex" column="sex"></result>
<!-- association处理多对一关系,collection处理一对多-->
</resultMap>
后在查询语句中将resultmap与其唯一标识id对应
<select id="getAllEmp" resultMap="empResultMap">
select * from Emp
</select>
那么到底什么时候用association,什么时候用collection呢
association是在多对一 的情况下使用,比如多个员工属于一个部门,那么对员工来说部门就是1
<resultMap id="empAndDeptResultMapTwo" type="Emp">
<id property="eid" column="eid"></id>
<result property="eName" column="emp_name"></result>
<result property="age" column="age"></result>
<result property="sex" column="sex"></result>
<!-- association:处理多对一的映射关系
property:需要处理多对一的映射关系的属性名
javaType:该属性的类型
-->
<association property="dept" javaType="Dept">
<id property="did" column="did"></id>
<result property="dName" column="dept_name"></result>
</association>
</resultMap>
collection是在一对多的情况下使用,比如一个部门有多个员工,对部门来说员工就是多