zoukankan      html  css  js  c++  java
  • mybatis级联查询,多对一查询问题

    在使用Mybatis进行多表级联查询时遇到了一个问题:查询结果只有一项,但正确结果是两项。经测试,SQL语句本身没有问题。

    在SQL映射文件(XML)中:

    <!-- 级联查询数据 -->
        <resultMap id="resultUserOhter" type="Uother">
            <id column="id" property="id"  />
            <result column="other" property="other"  />
            <association property="user" javaType="User">
                <id column="id"  property="id" />
                <result column="name" property="name" />
                <result column="info" property="info" />
            </association>
        </resultMap>
        <select id="getUserOhters" parameterType="int" resultMap="resultUserOhter">
            select
             user.id,user.name,user.info,uother.id,uother.ohter from uother,user
            where user.id=uother.user_id
        </select>

    经过试验,这种现象原因是id的问题,使用resultMap进行结果映射的时候其id作为主键,应该唯一,即上面的这种结构的嵌套只能映射多个Uother对应一个User的情况。如果查询结果的Uother的id重复则只保留一项(可能是最后一项)。总之,查询结果中<id>对应的属性值必须是唯一的。

    再次查询可查询到多项数据。

  • 相关阅读:
    《老男孩》
    java中四种阶乘的计算
    DataOutputStream的乱码问题
    java中飞clone方法
    类反射的简单例子
    PHP: 如何连接oracle数据库进行数据读取
    .NET : 如何读取图片中的元数据信息
    重新审视REST
    PHP 的历史
    Image File Format Specifications {转载}
  • 原文地址:https://www.cnblogs.com/esCharacter/p/6745447.html
Copyright © 2011-2022 走看看