ibatis使用resultMap处理级联查询

resultmap和resultclass的区别:
一:当结果集列名和类的属性名完全相对应的时候,则可直接用resultClass直接指定查询结果类型。
二:当查询结果集与属性名对应不上的时候,就可以采用resultMap指定列名与对象属性名之间的对应关系,否则对应不上的属性将为null或0.

CREATE TABLE `langlang_vcourse_comments` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键id', `vcourse_id` int(11) DEFAULT NULL COMMENT '微视频id', `user_id` int(11) DEFAULT NULL COMMENT '用户id', `comments` varchar(100) DEFAULT NULL COMMENT '点评内容', `created_at` timestamp NULL DEFAULT NULL COMMENT '创建时间', `updated_at` timestamp NULL DEFAULT NULL COMMENT '更新时间', `parent_id` int(11) DEFAULT '0' COMMENT '父节点id', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT;

多级评论,树形结构。实体对象如下:

public class Comment extends BaseModel implements Serializable {
    private Integer id;
    private Integer user_id;
    private String comments;
    private Integer vcourse_id ;
    private String  userName;
    private String  headImg;
    private Integer parent_id;
    List<Comment> commentList;
    //省略set和get方法
}
 <resultMap id="CommentMap"  class="com.wizincloud.vcourse.model.Comment">
    <result column="id" property="id" jdbcType="INTEGER" />
    <result column="user_id" property="user_id" jdbcType="INTEGER" />
    <result column="comments" property="comments" jdbcType="VARCHAR" />
    <result column="vcourse_id" property="vcourse_id" jdbcType="INTEGER" />
    <result column="parent_id" property="parent_id" jdbcType="INTEGER" />
    <result column="created_at" property="created_at" jdbcType="Date" />
    <result column="userName" property="userName" jdbcType="VARCHAR" />
    <result column="id" property="commentList" select="selectCommentList"/>
  </resultMap>

  <select id="selectCommentList" parameterClass="int" resultClass="com.wizincloud.vcourse.model.Comment">
     select t.* from langlang_vcourse_comments t where parent_id=#id# order by id asc
   </select>



   <select id="getCommentList" parameterClass="com.wizincloud.vcourse.model.Comment" resultMap="CommentMap">
        SELECT
 c.*,u.name as userName
 FROM
 langlang_vcourse_comments c
 LEFT JOIN langlang_vcourse v ON v.id = c.vcourse_id
 LEFT JOIN langlang_users u ON u.id = c.user_id
 WHERE c.vcourse_id = #vcourse_id# 
     <dynamic prepend=""> 
         <isNotNull property="orderCol">
         order by $orderCol$
         <isNotNull property="ascDesc"> 
         $ascDesc$
         </isNotNull> 
         </isNotNull> 
         </dynamic>
          <dynamic prepend=""> 
         <isNotNull property="rowNumStart"> 
         <isNotNull property="pageSize"> 
         LIMIT #rowNumStart#,#pageSize# 
         </isNotNull> 
         </isNotNull> 
        </dynamic> 
    </select>
​

你可能感兴趣的:(ibatis,resultMap)