MyBatis主子表查询

一、Mybatis查询语句

<select id="selectByProblemId" resultMap="findByProblemId">
      SELECT
          cp.id,
          cp.problem_id,
          cp.vote,
          cp.detail,
          cp.isdelete,
          cp.user_id,
          cp.islike,
          cp.headpic,
          cp.username,
          op.picture_url
      FROM
          co_reply AS cp
          LEFT JOIN co_picture AS op ON cp.id = op.attach_id
          AND op.attach_type = 1
      WHERE
          cp.problem_id = #{problemId,jdbcType=INTEGER}
          AND cp.isdelete = 0
      ORDER BY
          cp.vote DESC,
          cp.create_time DESC
  </select>

二、通过XML将查询结果转成主子表方式

  <resultMap id="findByProblemId" type="com.yunxue.entity.CoReply">
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="problem_id" jdbcType="INTEGER" property="problemId" />
    <result column="vote" jdbcType="INTEGER" property="vote" />
    <result column="detail" jdbcType="VARCHAR" property="detail" />
    <result column="isdelete" jdbcType="INTEGER" property="isdelete" />
    <result column="user_id" jdbcType="INTEGER" property="userId" />
    <result column="headpic" jdbcType="INTEGER" property="headpic" />
    <result column="username" jdbcType="INTEGER" property="username" />
    <result column="islike" jdbcType="INTEGER" property="islike" />
    <collection property="coPictures" ofType="com.yunxue.entity.CoPicture">
      <id column="r_id" jdbcType="INTEGER" property="id" />
      <result column="picture_url" jdbcType="VARCHAR" property="pictureUrl" />
    </collection>
  </resultMap>

三、Mapper文件

 List<CoReply> selectByProblemId(@Param("problemId") Integer problemId,@Param("user_id") Integer user_id);

四、Vo类

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Data
@ApiModel(value="CoReply对象", description="")
public class CoReply {
    @ApiModelProperty(value = "主键ID")
    private Integer id;
    @ApiModelProperty(value = "用户ID")
    private Integer userId = 0;
    @ApiModelProperty(value = "问题ID")
    private Integer problemId = 0;
    @ApiModelProperty(value = "点赞数")
    private Integer vote = 0;
    @ApiModelProperty(value = "答案详情")
    private String detail = "";
    @ApiModelProperty(value = "是否删除 0、否 1、是")
    private Boolean isdelete = false;
    @ApiModelProperty(value = "用户昵称")
    private String username = "";
    @ApiModelProperty(value = "头像地址")
    private String headpic = "";
    @ApiModelProperty(value = "是否点赞")
    private boolean islike = false;
    @ApiModelProperty(value = "图片地址")
    private List<CoPicture> coPictures = new ArrayList<>(0);
    @ApiModelProperty(value = "创建日期")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
    private Date createTime;

五、实体类

@Data
@ApiModel(value="CoPicture对象", description="")
@JsonInclude(JsonInclude.Include.NON_NULL)
public class CoPicture {
    @ApiModelProperty(value = "主键ID")
    private Integer id;
    @ApiModelProperty(value = "图片地址")
    private String pictureUrl;
    @ApiModelProperty(value = "问题ID")
    private Integer attachId;
    @ApiModelProperty(value = "问题类型(0、问题 1、回答)")
    private Boolean attachType;
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值