mybatis-resultMap

                            

<!--
1.type 对应类型,可以是javabean, 也可以是其它
2.id 必须唯一, 用于标示这个resultMap的唯一性,在使用resultMap的时候,就是通过id指定
-->
<resultMap type="" id="">

<!-- id, 唯一性,注意啦,这个id用于标示这个javabean对象的唯一性, 不一定会是数据库的主键(不要把它理解为数据库对应表的主键)
property属性对应javabean的属性名,column对应数据库表的列名
(这样,当javabean的属性与数据库对应表的列名不一致的时候,就能通过指定这个保持正常映射了)
-->
<id property="" column=""/>

<!-- result与id相比, 对应普通属性 -->
<result property="" column=""/>

<!--
constructor对应javabean中的构造方法
-->
<constructor>
<!-- idArg 对应构造方法中的id参数 -->
<idArg column=""/>
<!-- arg 对应构造方法中的普通参数 -->
<arg column=""/>
</constructor>

<!--
collection,对应javabean中容器类型, 是实现一对多的关键
property 为javabean中容器对应字段名
column 为体现在数据库中列名
ofType 就是指定javabean中容器指定的类型
-->
<collection property="" column="" ofType=""></collection>

<!--
association 为关联关系,是实现N对一的关键。
property 为javabean中容器对应字段名
column 为体现在数据库中列名
javaType 指定关联的类型
-->
<association property="" column="" javaType=""></association>
</resultMap>

 

<resultMap type="student" id="studentMap">
    
        <!-- 
            数据库中主键是id, 但是我这儿却是指定idCard为主键,为什么? 
            刚刚讲了,id用来表示唯一性, 我们可以认为只要idCard一样,那么他就是同一个学生。
            如果此处用数据库中id, 那么mybatis将会认为数据库中每条记录都是一个student, 这显然不符合逻辑
        -->
        <id property="idCard" column="stu_id_card"/>
        <result property="id" column="stu_id"/>
        <result property="name" column="stu_name"/>
        <result property="deleteFlag" column="stu_delete_flg"/>
        
        <!-- 
            这儿就是实现一对多的关键。 
            在Student中,courseList为List<Course>, 因此,ofType也应该与之对应(当然,我用了别名,不然要蛋疼的写全名了)。
            collection的子标签是在指定Course的映射关系(由于Course的javabean的属性名与数据库的列名不一致)
        -->
        <collection property="courseList" column="stu_course_id" ofType="Course">
            <id property="id" column="course_id"/>
            <result property="name" column="course_name"/>
            <result property="deleteFlag" column="course_delete_flg"/>
        </collection>
    </resultMap>
    
    <!-- 这儿将返回类型设置成了上面指定的studentMap -->
    <select id="findStudentById" resultMap="studentMap">
        SELECT s.*, c.* FROM t_student s LEFT JOIN t_course c ON s.stu_course_id=c.course_id WHERE s.stu_id_card=#{idCard}
    </select>


左向外连接的结果集包括LEFT JOIN子句中指定的左表的所有行,而不仅仅是连接列所匹配的行。如果左表的某行在右表中没有匹配行,则在相关联的结果集行中右表的所有选择列表列均为空值。



1.集合的嵌套查询(select)

<collection property="类中的属性名" javaType="ArrayList" ofType="另一个类名" column="关联主键ID(用于嵌套查询SQL语句传入参数,多个用逗号分开)" select="另一个select映射SQL的ID" />

<select parameterType="int" resultType="另一Java类名" id="另一个select映射SQL的ID">
SQL语句
<select>

(column属性的值必须与相应的SQL查询语句中的列名相同。MyBatis会将第一条SQL语句查询出来的该列的值用于所嵌套的SQL映射语句的入参。因第一条SQL语句查询出来的每个该列的值都将用于执行另一个SQL语句,所以嵌套的SQL语句将被多次执行)

2.集合的嵌套结果(resultMap)
<collection property="Java属性名" ofType="另一Java类名" javaType="ArrayList" resultMap="另一个resultMap的ID"/>
<resultMap="另一个resultMap的ID" type="另一Java类名">
<id property="id" column="关联主键ID"/>
........
</resultMap>

(column属性的值必须与相应的SQL查询语句的列名一样)

 


//例子:
<resultMap type="Student" id="StudentResult">   <id property="id" column="id" />   <result property="name" column="name" />   <result property="age" column="age" />   <association property="grade" column="gradeid" javaType="Grade"   select="selectGrade">     <id property="gid" column="id" />     <result property="gn" column="gn" />   </association>
  //association和collection的colunm属性值对应数据库中的字段   <collection property="courseList" javaType="ArrayList" ofType="Course" column="id" select="selectCourses"/> </resultMap> <select id="searchStudents" parameterType="Map" resultMap="StudentResult">   select * from student   where age=#{age} </select> <select id="selectGrade" parameterType="int" resultType="Grade">   SELECT *   FROM grade WHERE gid = #{gid} </select> <select id="selectCourses" parameterType="int" resultType="Course">   SELECT *   FROM Course WHERE stuid = #{id} </select>

 





posted @ 2015-08-10 17:14  QinH  阅读(299)  评论(0编辑  收藏  举报