<resultMapid="detailedBlogResultMap"type="pw.Blog"><!--
id 和 result 元素都将一个列的值映射到一个简单数据类型(String, int, double, Date 等)的属性或字段;
这两者之间的唯一不同是,id 元素表示的结果将是对象的标识属性,这会在比较对象实例时用到。
这样可以提高整体的性能,尤其是进行缓存 和嵌套结果映射(也就是连接映射)的时候。
column--数据表字段
property--java类属性
--><idcolumn="id"property="objectID"jdbcType="INTEGER"/><resultcolumn="blog_title"property="title"jdbcType="VARCHAR"/><!--
用于在实例化类时,注入结果到构造方法中
idArg - ID 参数;标记出作为 ID 的结果可以帮助提高整体性能
arg - 将被注入到构造方法的一个普通结果
--><constructor><idArgcolumn="blog_id"javaType="int"name="id"/><argcolumn="age"javaType="_int"name="age"/><argcolumn="username"javaType="String"name="username"/></constructor><!-- 映射一个java对象 --><associationproperty="author"javaType="pw.Author"><idproperty="id"column="author_id"/><resultproperty="username"column="author_username"/><resultproperty="password"column="author_password"/><resultproperty="email"column="author_email"/><resultproperty="bio"column="author_bio"/><resultproperty="favouriteSection"column="author_favourite_section"/></association><!-- 映射一个集合 --><collectionproperty="posts"ofType="pw.Post"><idproperty="id"column="post_id"/><resultproperty="subject"column="post_subject"/><!-- select 的值为<select>标签的id --><associationproperty="author"javaType="pw.Author"select="selectAuthor"/><collectionproperty="comments"ofType="Comment"><idproperty="id"column="comment_id"/></collection><collectionproperty="tags"ofType="pw.Tag"><idproperty="id"column="tag_id"/></collection><!--
鉴别器,相当于Java 语言中的 switch 语句
resultType-- 的值为 <resultMap>标签的id
--><discriminatorjavaType="int"column="draft"><casevalue="1"resultType="carResult"/><casevalue="2"resultType="truckResult"><resultproperty="boxSize"column="box_size"/><resultproperty="extendedCab"column="extended_cab"/></case></discriminator></collection><!-- select的值为<select>标签的id --><collectionproperty="posts"javaType="ArrayList"column="id"ofType="Post"select="selectPostsForBlog"/></resultMap>
二、一对一关联查询
<mappernamespace="com.lcb.mapping.userMapper"><!--association 一对一关联查询 --><selectid="getClass"parameterType="int"resultMap="ClassesResultMap">
select * from class c,teacher t where c.teacher_id=t.t_id and c.c_id=#{id}
</select><resultMaptype="com.lcb.user.Classes"id="ClassesResultMap"><!-- 实体类的字段名和数据表的字段名映射 --><idproperty="id"column="c_id"/><resultproperty="name"column="c_name"/><associationproperty="teacher"javaType="com.lcb.user.Teacher"><idproperty="id"column="t_id"/><resultproperty="name"column="t_name"/></association></resultMap></mapper>
三、一对多关联查询
<mappernamespace="com.lcb.mapping.userMapper"><!--collection 一对多关联查询 --><selectid="getClass2"parameterType="int"resultMap="ClassesResultMap2">
select * from class c,teacher t,student s where c.teacher_id=t.t_id and c.c_id=s.class_id and c.c_id=#{id}
</select><resultMaptype="com.lcb.user.Classes"id="ClassesResultMap2"><idproperty="id"column="c_id"/><resultproperty="name"column="c_name"/><associationproperty="teacher"javaType="com.lcb.user.Teacher"><idproperty="id"column="t_id"/><resultproperty="name"column="t_name"/></association><collectionproperty="student"ofType="com.lcb.user.Student"><idproperty="id"column="s_id"/><resultproperty="name"column="s_name"/></collection></resultMap></mapper>
一、示列<resultMap id="detailedBlogResultMap" type="pw.Blog"> <!-- id 和 result 元素都将一个列的值映射到一个简单数据类型(String, int, double, Date 等)的属性或字段; 这两者之间的唯一不同是,id 元素表示的结果将是对象的标识属性,这会在比较对象实例时用到。 这样可...