MyBatis,集合的嵌套结果为空

时间:2015-02-24 08:43:07

标签: collections mybatis

我正在关注MyBatis网站中的示例创建一个具有嵌套集合的映射器,当我执行select操作时,我的对象(Provider)返回但其中的集合(ProviderParameter)为空,当我去到数据库工具并应用相同的查询我得到了预期的结果(该集合也返回)。

这是我的映射器:

   <resultMap id="Provider" type="xxx.Provider">
        <result column="idProvider" property="idProvider"/>
        <result column="providerType" property="providerType"/>
        <result column="username" property="username"/>
        <result column="password" property="password"/>
        <result column="licenceInformation" property="licenseInformation"/>
        <collection property="parameters" ofType="xxx.ProviderParameter">
            <result column="idProviderParameter" property="idProviderParameter"/>
            <result column="name" property="name"/>
            <result column="value" property="value"/>
        </collection>
    </resultMap>

选择:

<select id="getProviderById" resultType="xxx.Provider">
    select P.idProvider         as idProvider
          ,P.providerType       as providerType
          ,P.username           as username
          ,P.password           as password
          ,P.licenceInformation as licenceInformation
          ,PP.idProviderParameter as idProviderParameter
          ,PP.name              as name
          ,PP.value             as value
     from [dbo].[msg_Provider] AS P 
     left outer join [dbo].[msg_ProviderParameter] AS PP on P.idProvider = PP.idProvider     
     where P.idProvider = #{idProvider}
</select>

1 个答案:

答案 0 :(得分:0)

我的问题是在select I put result而不是resultMap中,

所以选择应该是:

    <select id="getProviderById" resultMap="Provider">
    select P.idProvider         as idProvider
          ,P.providerType       as providerType
          ,P.username           as username
          ,P.password           as password
          ,P.licenceInformation as licenceInformation
          ,PP.idProviderParameter as idProviderParameter
          ,PP.name              as name
          ,PP.value             as value
     from [dbo].[msg_Provider] AS P 
     left outer join [dbo].[msg_ProviderParameter] AS PP on P.idProvider = PP.idProvider     
     where P.idProvider = #{idProvider}
</select>