Mybatis结果图只输出三个记录中的一个

时间:2015-07-15 18:29:04

标签: mybatis

我有3条记录要从药物表中填充:

select * from ccda_medication where client_id = 100009;
client_id | rxcui  | drug  | medicationtime | status | directions |pharmacynote
-----------+--------+---------------------------------------+----------------+--------+------------------------------------+
100009 | 573621 | Albuterol 0.09 MG/ACTUAT [Proventil]  | 20120806   | ACTIVE | 2 puffs every 6 hours PRN wheezing |
100009 | 866924 | Metoprolol Tartrate 25 MG Oral Tablet | 20120806   | ACTIVE | by mouth once daily|
100009 | 197517 | Clarithromycin 500 MG Oral Tablet | 20120806   | ACTIVE | by mouth twice daily for 7 days|
(3 rows)

调试还显示从数据库中检索到3条记录。

==>  Preparing: select * from ccda_medication where client_id = ?
==> Parameters: 100009(Integer)
<==Columns: client_id, rxcui, drug, medicationtime, status, directions, pharmacynote
<==Row: 100009, 573621, Albuterol 0.09 MG/ACTUAT [Proventil], 20120806, ACTIVE, 2 puffs every 6 hours PRN wheezing,
<==Row: 100009, 866924, Metoprolol Tartrate 25 MG Oral Tablet, 20120806, ACTIVE, by mouth once daily,
<==Row: 100009, 197517, Clarithromycin 500 MG Oral Tablet, 20120806, ACTIVE, by mouth twice daily for 7 days,
<==  Total: 3

但是当我打电话给 selectMedications 时,我只记录一条记录 - 最后一条记录:行:100009,197517 有趣的是,如果我将Substance扁平化以直接包含所有字段(没有内部类),我将获得所有3条记录,因此列名称是正确的。

Mapper文件如下:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="ccda.mapper.interfaces.MedicationMapper">
<select id="**selectMedications**"
    resultMap="MedicationsResultMap"
    parameterType="Integer">
    select * from ccda_medication where client_id = #{id}
</select>

<resultMap id="MedicationsResultMap" type="ccda.model.Substance">
      <result property="status" column="status"/>

      <association property="substanceTime" javaType="ccda.model.EffectiveTime">   
            <result property="low" column="medicationtime"/>
            <result property="high" column="medicationtime"/>  
      </association>   

      <association property="medication" javaType="ccda.model.Medication">                    
             <result property="rxcui" column="rxcui"/>
              <result property="drug" column="drug"/>                 
              <result property="directions" column="directions"/>
              <result property="fillInstructions" column="pharmacynote"/>
      </association>   
</mapper>

Substance,Medication和MedicationMapper如下:

package ccda.model;
public class Substance
{
    public String status;
    public EffectiveTime substanceTime;   
    public Medication medication;
}

package ccda.model;
public class Medication
{   
    public int rxcui;
    public String drug;   
    public String directions;
    public String fillInstructions;
}

package ccda.mapper.interfaces;
import java.util.List;
import ccda.model.Medication;
import ccda.model.Substance;
public interface MedicationMapper
{
    List<Substance> selectMedications( int id );
}
你可以帮我弄明白为什么会这样吗?非常感谢!

1 个答案:

答案 0 :(得分:1)

问题是没有指定物质的标识符字段。因此,mybatis不知道返回实体的身份是什么,在这种情况下,它认为所有记录都指向同一个实体。

请在id中指定resultMap,然后使用ccda_medication中的唯一字段。