【Mybatis】子查询总结(一对多Collection),查询父对象时,也查询子对象;

一、需求

比如,我们有两个对象,一个问答,一个评论,类似百度知道一样的东西;我们查询一个问答的时候,可以查看其下面的评论;
我在查询J_questionandanswer这张表的时候,需要查询j_comment这张表,并把j_comment这张表里的数据放到questionAndAnswer这个实体中;操作步骤如下:

二、解决办法

准备工作:
1、j_comment表的创建;
2、comment的实体类创建
3、在j_questionandanswer(别名qa)这个表里加入左连接:

left join j_comment jc on jc.titleid = qa.id

4、在select中,加入想要在j_comment表中查询到的字段;jc是j_comment的别名;
5、QuestionAndAnswer实体中,加入Comment映射的List集合:
这里写图片描述

6、注意:jc表中的id会和qa表中的id冲突,jc.id起别名叫cid;

<!--分页查询-->
<select id="selectByProperties" parameterType="com.jxdx.questionAndAnswer.model.QuestionAndAnswer" resultMap="BaseResultMap">
select <include refid="Base_Column_List_Alias"/>,jc.id AS cid, jc.titlename, jc.titleid, jc.username, jc.userid, jc.comment, jc.create_date from jxdx_questionandanswer qa
<if test="studentid != null">
left join sys_t_user u on u.id = qa.studentid
</if>
<if test="teacherid != null">
left join sys_t_user u on u.id = qa.teacherid
</if>
left join jxdx_comment jc on jc.titleid = qa.id
where qa.deleted=0
<if test="name!=null">
AND qa.name like '%${name}%'
</if>
<if test="studentid!=null">
AND qa.studentid=#{studentid,jdbcType=BIGINT}
</if>
<if test="studentname!=null">
AND qa.studentname=#{studentname,jdbcType=VARCHAR}
</if>
<if test="createDate != null">
AND qa.create_date <![CDATA[>=]]> #{createDate}
</if>
<if test="updateDate != null">
AND qa.update_date <![CDATA[>=]]> #{updateDate}
</if>
<!-- <if test="updateDate != null">
AND qa.update_date <![CDATA[<=]]> date_add(#{updateDate}, INTERVAL 1 DAY)
</if>-->
<if test="teacherid != null">
AND qa.teacherid = #{teacherid,jdbcType=BIGINT}
</if>
<if test="teachername!=null">
AND qa.teachername=#{teachername,jdbcType=VARCHAR}
</if>
<if test="studentclassid!=null">
AND qa.studentclassid=#{studentclassid,jdbcType=BIGINT}
</if>
<if test="studentclassname!=null">
AND qa.studentclassname=#{studentclassname,jdbcType=VARCHAR}
</if>
<if test="question!=null">
AND qa.question=#{question,jdbcType=VARCHAR}
</if>
<if test="answer!=null">
AND qa.answer=#{answer,jdbcType=VARCHAR}
</if>
<if test="status!=null">
AND qa.status=#{status,jdbcType=INTEGER}
</if>
<if test="remark!=null">
AND qa.remark=#{remark,jdbcType=VARCHAR}
</if>
<if test="deleted!=null">
AND qa.deleted=#{deleted,jdbcType=INTEGER}
</if>
<if test="createBy != null">
AND qa.createBy = #{createBy,jdbcType=BIGINT}
</if>
<if test="updateBy != null">
AND qa.updateBy = #{updateBy,jdbcType=BIGINT}
</if>
order by qa.create_date desc
</select>

注:

  • 这里的<include refid="Base_Column_List_Alias"/>
    是查询的字段的集合,可以替换成具体的字段名;

  • ` AND qa.create_date

<collection property="commentList" ofType="com.jxdx.questionAndAnswer.model.Comment" notNullColumn="id">
<id column="cid" property="id" jdbcType="BIGINT"/>
<result column="titlename" property="titleName" jdbcType="VARCHAR" />
<result column="titleid" property="titleId" jdbcType="VARCHAR"/>
<result column="username" property="userName" jdbcType="BIGINT" />
<result column="userid" property="userId" jdbcType="VARCHAR" />
<result column="comment" property="comment" jdbcType="VARCHAR"/>
<result column="remark" property="remark" jdbcType="VARCHAR"/>
<result column="create_date" property="createDate" jdbcType="VARCHAR"/>
</collection>

完成

欢迎关注我的公众号:
【幕桥社区】
这里写图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
MyBatis查询一对多关系,可以使用嵌套查询或者使用 MyBatis 提供的关联查询功能。其中,嵌套查询是指在主查询中嵌套子查询,通过子查询查询出关联表中的数据,然后将其映射到主查询的结果集中。而关联查询则是通过 MyBatis 提供的 association 和 collection 标签来实现,其中 association 标签用于一对一关系的查询collection 标签用于一对多关系的查询。 下面是使用 collection 标签查询一对多关系的示例: ``` <!-- 定义主查询 --> <select id="selectOrder" resultMap="orderResultMap"> SELECT * FROM orders WHERE order_id = #{orderId} </select> <!-- 定义结果集映射 --> <resultMap id="orderResultMap" type="Order"> <id property="id" column="order_id"/> <result property="orderNo" column="order_no"/> <result property="createTime" column="create_time"/> <!-- 使用 collection 标签定义一对多关系 --> <collection property="items" ofType="OrderItem" resultMap="itemResultMap"/> </resultMap> <!-- 定义 OrderItem 的结果集映射 --> <resultMap id="itemResultMap" type="OrderItem"> <id property="id" column="item_id"/> <result property="itemName" column="item_name"/> <result property="price" column="price"/> </resultMap> ``` 在上面的示例中,我们定义了一个主查询 selectOrder,它查询出订单表中指定订单号的订单信息。同,我们使用了 resultMap 标签定义了一个结果集映射 orderResultMap,其中使用了 collection 标签来定义了一对多关系,将订单表和订单明细表关联起来。在 collection 标签中,我们指定了 property 属性为 items,表示将查询出的订单明细数据映射到 Order 对象的 items 属性中。同,我们还定义了一个 OrderItem 的结果集映射 itemResultMap,用于将查询出的订单明细数据映射到 OrderItem 对象中。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陶洲川

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值