本文介绍了在Mybatis中使用sql显示一对多映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要上桌

Tasks:
 id
 title
 Description

Task_reply_mapping
task_id
parent_id

我编写了以下查询以获取数据

I have written the following query to get the data

    select t1.id,t1.title,t1.description,t2.id,t2.title,t2.description
from tasks t1
left join Task_reply_mapping trm on t1.id =  trm.task_id
    left join tasks t2 on t2.id = t1.id
    order by fe.created_at desc limit 0,10

这似乎工作正常,但是没有正确填充数据.我想知道此查询是否正确吗?

This seems to be working fine but its not populating the data correctly. I want to know if this query is correct?

我的映射器文件中有

  <resultMap id="TaskResultMap" type="com.mycom.myproj.bean.TaskBean">
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="title" jdbcType="VARCHAR" property="title" />
    <result column="description" jdbcType="VARCHAR" property="description" />
  <collection ofType="com.mycom.myproj.bean.TaskBean" property="replyTask">
    <id column="id" jdbcType="BIGINT" property="id" />
        <result column="title" jdbcType="VARCHAR" property="title" />
        <result column="description" jdbcType="VARCHAR" property="description" />
   </collection>
  </resultMap>

或者我在mapper类中做错了事.

Or I am doing something wrong in mapper class.

对象中的记录的放置方式像第一个索引一样,即放置最新任务(无论是答复还是新任务),等等.

Records in objects are placed like at first index its putting the latest task whether its reply or new task and so on.

它应该插入这样的记录

   Task1
     --1st reply task
     --2nd reply task
  Task 2
      --1st reply task
      --2nd reply task

如果需要更多信息,请告诉我.

Please let me know if more information is required.

推荐答案

不确定问题出在哪里.

我认为查询和映射可能不正确.

I think maybe the query and the mapping are not correct.

尝试一下:

select
    t1.id
    ,t1.title
    ,t1.description
    ,t2.id as id_2
    ,t2.title as title_2
    ,t2.description as description_2
from tasks t1
    left join Task_reply_mapping trm on t1.id =  trm.task_id
    left join tasks t2 on t2.id = t1.id
order by fe.created_at desc limit 0,10

映射

<resultMap id="TaskResultMap" type="com.mycom.myproj.bean.TaskBean">
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="title" jdbcType="VARCHAR" property="title" />
    <result column="description" jdbcType="VARCHAR" property="description" />
    <collection ofType="com.mycom.myproj.bean.TaskBean" property="replyTask">
    <id column="id_2" jdbcType="BIGINT" property="id" />
        <result column="title_2" jdbcType="VARCHAR" property="title" />
        <result column="description_2" jdbcType="VARCHAR" property="description" />
   </collection>
</resultMap>

在查询中,您必须输入id,两个"title and two description",以便可能出现问题.

In you query, you have to id, two ´titleand twodescription` so the problem could be there.

这篇关于在Mybatis中使用sql显示一对多映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 09:29