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

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

  • 相关阅读:
    python_24_test
    python_23_tuple
    python_22_enumerate
    python_20_列表
    python_21_copy
    python_19_编码解码
    python_18_三元运算
    python_16_自己建立模块
    关于主键(PRIMARY KEY)和自增(AUTO_INCREMENT)结合使用的知识点
    MySQL root用户忘记密码怎么办?修改密码方法:skip-grant-tables
  • 原文地址:https://www.cnblogs.com/esCharacter/p/6745447.html
Copyright © 2011-2022 走看看