0

我必须表

Tasks:
 id
 title
 Description

Task_reply_mapping
task_id
parent_id

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

    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

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

在我的映射器文件中,我有

  <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>

或者我在映射器类中做错了什么。

对象中的记录被放置,就像首先索引它放置最新任务,无论是它的回复还是新任务等等。

它应该像这样插入记录

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

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

4

2 回答 2

1

Not sure where the problem is.

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

Try this:

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

Mapping

<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>

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

于 2013-04-25T07:05:14.680 回答
1

第二个join必须与Task_reply_mapping表:

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 = trm.parent_id

这是一个演示。

于 2013-04-24T13:56:17.213 回答