记录一下mybatis一对多数据查询

谁说胖子不能爱 提交于 2020-10-22 00:05:37

需求说明:需要查询一个集合,集合对象中有list<对象>

1、连个resultMap的column不能一样,不然查出数据对象中的list是本对象

2、建议用左连接查询

3、查询出的字段名称要与resultMap中的字段名一直不然会出先list.size()有数据但是集合为null的情况

<sql id="Base_Column_List_DiscussBuch">
   d.id as d_id,d.userId as d_userId,d.articleId as d_articleId,
   d.replyId as d_replyId,d.replyUserId as d_replyUserId,
   d.discuss as d_discuss,d.isDelete as d_isDelete,d.createTime as d_createTime,
   d.updateTime as d_updateTime,u.realName as d_userName,u.avatarColor as d_avatarColor
</sql>

<sql id="Base_Column_List_DiscussBuchDD">
     d.id as dd_id,d.userId as dd_userId,d.articleId as dd_articleId,
     d.replyId as dd_replyId,d.replyUserId as dd_replyUserId,
     d.discuss as dd_discuss,d.isDelete as dd_isDelete,d.createTime as dd_createTime,
     d.updateTime as dd_updateTime,u.realName as dd_userName,u.avatarColor as dd_avatarColor
</sql>
<resultMap type="com.hsing.wooxabbs.entity.Discuss" id="ResultMap">
    <id column="d_id" property="id" jdbcType="INTEGER"/>
    <id column="d_userId" property="userId" jdbcType="VARCHAR"/>
    <id column="d_articleId" property="articleId" jdbcType="INTEGER"/>
    <id column="d_replyId" property="replyId" jdbcType="VARCHAR"/>
    <id column="d_replyUserId" property="replyUserId" jdbcType="VARCHAR"/>
    <id column="d_discuss" property="discuss" jdbcType="VARCHAR"/>
    <id column="d_isDelete" property="isDelete" jdbcType="INTEGER"/>
    <id column="d_createTime" property="createTime" jdbcType="VARCHAR"/>
    <id column="d_updateTime" property="updateTime" jdbcType="VARCHAR"/>
    <id column="d_userName" property="userName" jdbcType="VARCHAR"/>
    <id column="d_avatarColor" property="avatarColor" jdbcType="VARCHAR"/>

    <collection property="replyList" resultMap="BatchListResultMap"/>
</resultMap>
<resultMap type="com.hsing.wooxabbs.entity.Discuss" id="BatchListResultMap">
    <id column="dd_id" property="id" jdbcType="INTEGER"/>
    <id column="dd_userId" property="userId" jdbcType="VARCHAR"/>
    <id column="dd_articleId" property="articleId" jdbcType="INTEGER"/>
    <id column="dd_replyId" property="replyId" jdbcType="VARCHAR"/>
    <id column="dd_replyUserId" property="replyUserId" jdbcType="VARCHAR"/>
    <id column="dd_discuss" property="discuss" jdbcType="VARCHAR"/>
    <id column="dd_isDelete" property="isDelete" jdbcType="INTEGER"/>
    <id column="dd_createTime" property="createTime" jdbcType="VARCHAR"/>
    <id column="dd_updateTime" property="updateTime" jdbcType="VARCHAR"/>
    <id column="dd_avatarColor" property="avatarColor" jdbcType="VARCHAR"/>
    <id column="dd_userName" property="userName" jdbcType="VARCHAR"/>
</resultMap>
<select id="queryAllByArticleId" resultMap="ResultMap">
    select
    d.*,
    dd.*
    from
    (select
    <include refid="Base_Column_List_DiscussBuch"/>
    from
    hsing_article_discuss d,hsing_user u
    where d.articleId=#{articleId} and d.isDelete = 1 and d.userId = u.id and d.replyId is null) d
    left join
    (select
    <include refid="Base_Column_List_DiscussBuchDD"/>
    from
    hsing_article_discuss d,hsing_user u
    where d.articleId=#{articleId} and d.isDelete = 1 and d.userId = u.id and d.replyId is not null) dd
    on dd.dd_replyId = d.d_id and dd.dd_id != d.d_id  order by d.d_createTime desc
</select>
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!