mybatis 一对多查询

实体类
 


public class CommunityInfoVo implements Serializable {
   
private static final long serialVersionUID = -6617353484144306181L;
    private long
dynamicsId;
    private
String headImgUrl;
    private
String nickname;
    private
String dynSource;
    private
String createTime;
    private
String place;
    private
String content;
    private
String contentType;
    private
String picUrl;
    private
List<DynamicsPraise> pointList;
   
//private List<DynamicsComment> commentList;

   
public long getDynamicsId() {
       
return dynamicsId;
   
}

   
public void setDynamicsId(long dynamicsId) {
       
this.dynamicsId = dynamicsId;
   
}

   
public String getHeadImgUrl() {
       
return headImgUrl;
   
}

   
public void setHeadImgUrl(String headImgUrl) {
       
this.headImgUrl = headImgUrl;
   
}

   
public String getNickname() {
       
return nickname;
   
}

   
public void setNickname(String nickname) {
       
this.nickname = nickname;
   
}

   
public String getDynSource() {
       
return dynSource;
   
}

   
public void setDynSource(String dynSource) {
       
this.dynSource = dynSource;
   
}

   
public String getCreateTime() {
       
return createTime;
   
}

   
public void setCreateTime(String createTime) {
       
this.createTime = createTime;
   
}

   
public String getPlace() {
       
return place;
   
}

   
public void setPlace(String place) {
       
this.place = place;
   
}

   
public String getContent() {
       
return content;
   
}

   
public void setContent(String content) {
       
this.content = content;
   
}

   
public String getContentType() {
       
return contentType;
   
}

   
public void setContentType(String contentType) {
       
this.contentType = contentType;
   
}

   
public String getPicUrl() {
       
return picUrl;
   
}

   
public void setPicUrl(String picUrl) {
       
this.picUrl = picUrl;
   
}

   
public List<DynamicsPraise> getPointList() {
       
return pointList;
   
}

   
public void setPointList(List<DynamicsPraise> pointList) {
       
this.pointList = pointList;
   
}

    public List<DynamicsComment> getCommentList() {
        return commentList;
    }

    public void setCommentList(List<DynamicsComment> commentList) {
       this.commentList = commentList;
    }

}

 

 

 

 

 





/**

 * 动态点赞表(DynamicsPraise)实体类

 *

 * @author makejava

 * @since 2019-04-16 17:19:24

 */

@Data

public class DynamicsPraise implements Serializable {

    private static final long serialVersionUID = 884091475425726882L;

    

    private long id;

    //动态Id

    private long dynamicsId;

    //点赞人

    private long userId;

    //点赞时间

    @JsonFormat(pattern = "yyyy-MM-dd hh:mm:ss")

    private Date createTime;



}
/**
 * 动态评论表(DynamicsComment)实体类
 *
 * @author makejava
 * @since 2019-04-16 17:20:07
 */
@Data
public class DynamicsComment implements Serializable {
    private static final long serialVersionUID = 796901961388730920L;
    
    private Long id;
    //动态id
    private Long dynamicsId;
    //评论用户id
    private Long userId;
    //评论内容
    private JSONObject content;
    //评论时间
    @JsonFormat(pattern = "yyyy-MM-dd hh:mm:ss",timezone = "GMT+8")
    private Date createTime;

}

 

 

xml


<resultMap  id="dynamicsSubjectMap" type="com.kunda.store.community.vo.CommunityInfoVo">
    <id
property="dynamicsId" column="id"/>
    <result
property="headImgUrl" column="head_img_url"/>
    <result
property="nickname" column="nickname"/>
    <result
property="dynSource" column="source"/>
    <result
property="createTime" column="create_time"/>
    <result
property="place" column="place"/>
    <result
property="content" column="content"/>
    <result
property="contentType" column="type"/>
    <result
property="picUrl" column="pic_url"/>
    <collection
property="pointList" ofType="com.kunda.store.community.entity.DynamicsPraise" column="id" select="selectPraise" />

    <collection
property="commentList" ofType="com.kunda.store.community.entity.DynamicsComment" column="id" select="selectPraise"/>
</resultMap>


<!--通过实体作为筛选条件查询-->
<select id="queryAll" resultMap="dynamicsSubjectMap">
   
select
    ds.
id, ds.content, ds.pic_url,ds.place, ds.type, ds.source, ds.user_id, ds.store_id, ds.create_time
   
from wisdom_store.dynamics_subject ds
   
<where>
        <if
test="id != null">
           
and id = #{dynamicsId}
       
</if>
        <if
test="content != null and content != ''">
           
and content = #{content}
       
</if>
        <if
test="picUrl != null and picUrl != ''">
           
and pic_url = #{picUrl}
       
</if>
        <if
test="place != null and place != ''">
           
and place = #{place}
       
</if>
        <if
test="type != null">
           
and type = #{type}
       
</if>
        <if
test="source != null">
           
and source = #{source}
       
</if>
        <if
test="userId != null">
           
and user_id = #{userId}
       
</if>
        <if
test="storeId != null">
           
and store_id = #{storeId}
       
</if>
        <if
test="createTime != null">
           
and create_time = #{createTime}
       
</if>
    </where>
</select>



<!--查询点赞列表-->
<select id="selectPraise" resultType="com.kunda.store.community.entity.DynamicsPraise">
   
select
     
id, dynamics_id, user_id, create_time
   
from wisdom_store.dynamics_praise
    where
dynamics_id = #{dynamicsId}
</select>

<!--查询评论列表-->
<select id="selectComment" resultType="com.kunda.store.community.entity.DynamicsComment">
   
select
      
id,
      
dynamics_id dynamicsId,
      
user_id userId,
      
content,
      
create_time createTime
    from wisdom_store.dynamics_comment
    where
dynamics_id = #{dynamicsId}
</select>

 

 

 

数据库表

 

查询结果

 

Result

{

    "code": 1,

    "msg": "OK",

    "data": [

        {

            "dynamicsId": 1,

            "headImgUrl": null,

            "nickname": null,

            "dynSource": "1",

            "createTime": "2019-04-17 00:00:00.0",

            "place": null,

            "content": "this is a content,you will know about what you want here.",

            "contentType": "1",

            "picUrl": "http://web.fsyizhuang.com/upload/image/20181205/2i1b1544000398972.png",

            "pointList": [

                {

                    "id": 1,

                    "dynamicsId": 1,

                    "userId": 10000614,

                    "createTime": "2019-04-16 04:00:00"

                },

                {

                    "id": 2,

                    "dynamicsId": 1,

                    "userId": 0,

                    "createTime": null

                }

            ],

            "commentList": [

                {

                    "id": 1,

                    "dynamicsId": 1,

                    "userId": 10000614,

                    "createTime": "2019-04-16 04:00:00"

                },

                {

                    "id": 2,

                    "dynamicsId": 1,

                    "userId": 0,

                    "createTime": null

                }

            ]

        },

        {

            "dynamicsId": 2,

            "headImgUrl": null,

            "nickname": null,

            "dynSource": null,

            "createTime": null,

            "place": null,

            "content": null,

            "contentType": null,

            "picUrl": null,

            "pointList": [],

            "commentList": []

        }

    ]

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值