resultMap指定属性封装
当二级属性可以在同一条sql查出
<select id="get" resultMap="baseResultMap">
SELECT id, name, deptId
FROM employee
WHERE id = #{id}
</select>
<resultMap type="Employee" id="baseResultMap">
<!--什么列名对应值封装到对象的什么属性上-->
<id column="id" property="id"/>
<result column="name" property="name"/>
<!--特别地,属性可以是一级属性的属性(二级属性)-->
<result column="deptId" property="dept.id"/>
</resultMap>
**associate指定属性封装**
当二级属性可以在同一条sql查出
<resultMap type="Employee" id="baseResultMap">
<id column="id" property="id"/>
<result column="name" property="name"/>
<!--此处可以用前缀减少列标题的书写繁琐-->
<association columnPrefix="d_" property="dept" javaType="Department">
<result column="id" property="id"/>
<result column="name" property="name"/>
</association>
</resultMap>
associate发送额外查询
当二级属性不能在同一条sql查出
<resultMap type="Employee" id="baseResultMap">
<id column="id" property="id"/>
<result column="name" property="name"/>
<!-- 使用额外 SQLassociation 针对的关联属性配置,
非集合类型
select 发送什么额外SQL,通常指向已有的sql语句(namespace+sqlID)
column 发送额外 SQL 参数取上一条 SQL 哪个列的值
property 封装员工对象的什么属性-->
<association
select="cn.wolfcode.mapper.DepartmentMapper.get"
column="deptId" property="dept"
javaType="Department"/>
</resultMap>
<select id="get" resultMap="baseResultMap">
SELECT id, name, deptId FROM employee WHERE id =
#{id}
</select>
<!--额外sql语句-->
<select id="get" resultType="Department">
SELECT id, name FROM department WHERE id = #{id}
</select>
collection封装集合属性
当存在三级属性时
<resultMap type="Department" id="baseResultMap">
<id column="id" property="id"/>
<result column="name" property="name"/>
<!--若关联属性是集合类型,使用collection来配置select发送额外的SQL
column:额外SQL查询用的参数,在主sql中查出
property:查询结果封装到哪个一级属性里
oftype:指定二级属性的类型(此处为Employee),因为子sql指定了,可以忽略
javatype:指定一级属性的类型(此处为“java.util.ArrayList”),可以忽略
-->
<collection
select="cn.wolfcode.mapper.EmployeeMapper.queryByDeptId"
column="id" property="employees" oftype="Employee"/>
</resultMap>
<select id="get" resultMap="baseResultMap">
SELECT id, name FROM department WHERE id = #{id}
</select>
<!--额外sql-->
<select id="queryByDeptId" resultType="Employee">
SELECT id, name, deptId FROM employee WHERE deptId = #{deptId}
</select>