1.association 持久化对象组合对象时,被组合的对象用association标签
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.queen.mybatis.mapper.UserMapper">
<resultMap type="com.queen.mybatis.bean.User" id="userResultMap">
<id property="id" column="id"/>
<result property="loginId" column="loginId" />
<result property="userName" column="userName"/>
<result property="note" column="note"/>
<!--assocication可以指定联合的JavaBean对象
property="role"指定哪个属性是联合的对象
javaType:指定这个属性对象的类型
-->
<association property="role" javaType="com.queen.mybatis.bean.Role">
<id column="role_id" property="id"/>
<result column="roleName" property="roleName"/>
</association>
</resultMap>
<select id="getUserById" resultMap="userResultMap">
select m.id id, m.loginId loginId, m.userName userName, m.roleId roleId,m.note note, n.id role_id, n.roleName roleName
from t_user m left join t_role n on m.roleId=n.id
where m.id=#{id}
</select>
</mapper>
2.Collection 持久化对象与被组合对象是一对多关系时
<resultMap id="ClazzResultMap" type="com.cn.hnust.pojo.ClazzEntity" >
<id column="classID" property="clazzID" jdbcType="INTEGER" />
<result column="className" property="clazzName" jdbcType="VARCHAR" />
<collection property="studentList" column="classID" javaType="ArrayList"
ofType="com.cn.hnust.pojo.StudentEntity" select="getStudentByClassID"/>
</resultMap>
<resultMap id="StudentResultMap" type="com.cn.hnust.pojo.StudentEntity">
<id property="stuID" column="stuID" />
<result property="stuName" column="stuName" />
<result property="stuAge" column="stuAge" />
<result property="stuAddress" column="stuAddress" />
</resultMap>
<select id="getClassByID" resultMap="ClazzResultMap" parameterType="java.lang.Integer" >
select classID,className
from class_t
where classID = #{clazzID}
</select>
<select id="getStudentByClassID" resultMap="StudentResultMap" parameterType="java.lang.Integer" >
select stuID,stuName,stuAge,stuAddress,classID
from student_t
where classID = #{clazzID}
</select>
3.extends 标签的使用
代表resultMap CocBeanResult 中包含resultMap CocBeanResult的所有字段
<resultMap id="CocBeanResult" type="CocBean">
<result property="name" column="NAME"/>
<result property="volume" column="VOLUME"/>
</resultMap>
<resultMap id="simpleRow" type="CocTreeNode" extends="CocBeanResult">
<result property="level1" column="LEVEL1"/>
<result property="level2" column="LEVEL2"/>
</resultMap>