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>对应的属性值必须是唯一的。

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

  • 相关阅读:
    C# 中的栈和堆
    C# 中的基本数值类型
    多个 .NET 框架
    简单介绍托管执行和 CLI
    C# 控制台输入和输出
    在 C# 中使用变量
    C# 语法基础
    LeetCode 1482. 制作 m 束花所需的最少天数
    C# 基础(更新中)
    圆形靶内的最大飞镖数量
  • 原文地址:https://www.cnblogs.com/esCharacter/p/6745447.html
Copyright © 2011-2022 走看看