SuxLeing 2023-04-19 15:01 采纳率: 33.3%
浏览 30
已结题

mybatis使用association多表联查只返回一条结果

大致描述
同样一条SQL语句在navicat中查询的结果数量和mybatis查询得到的数量不同。

详细情况
SQL语句

SELECT pd.patrol_id, pd.work_id, u_info.user_name, u_info.user_tel, u_info.user_email, 
  pd.start_node_id AS start_id, start_.node_code AS start_code, start_.latitude AS start_latitude, start_.longitude AS start_longitude, 
  pd.end_node_id AS end_id, end_.node_code AS end_code, end_.latitude AS end_latitude, end_.longitude AS end_longitude 
FROM patrol_division AS pd 
  LEFT JOIN pipeline_node AS start_ ON pd.start_node_id = start_.node_id 
  LEFT JOIN pipeline_node AS end_ ON pd.end_node_id = end_.node_id 
  LEFT JOIN user_info AS u_info ON pd.work_id = u_info.user_id 
WHERE pd.patrol_id = 2;

navicat查询的结果

img

mybatis查询的结果

img

mybatis配置文件

    <resultMap id="DivisionResultMap" type="PatrolDivision">
        <id column="patrol_id" jdbcType="INTEGER" property="patrolId" />
        <association property="workUser" javaType="User" resultMap="WorkResultMap"/>
        <association property="startNode" javaType="PipelineNode" resultMap="StartNodeResultMap"/>
        <association property="endNode" javaType="PipelineNode" resultMap="EndNodeResultMap"/>
    </resultMap>

    <resultMap id="WorkResultMap" type="User">
        <id column="work_id" jdbcType="INTEGER" property="userId" />
        <result column="user_name" jdbcType="VARCHAR" property="userName" />
        <result column="user_email" jdbcType="VARCHAR" property="userEmail" />
        <result column="user_tel" jdbcType="VARCHAR" property="userTel" />
    </resultMap>

    <resultMap id="StartNodeResultMap" type="PipelineNode">
        <id column="start_id" jdbcType="INTEGER" property="nodeId" />
        <result column="start_code" jdbcType="VARCHAR" property="nodeCode" />
        <result column="start_longitude" jdbcType="DECIMAL" property="longitude" />
        <result column="start_latitude" jdbcType="DECIMAL" property="latitude" />
    </resultMap>

    <resultMap id="EndNodeResultMap" type="PipelineNode">
        <id column="end_id" jdbcType="INTEGER" property="nodeId" />
        <result column="end_code" jdbcType="VARCHAR" property="nodeCode" />
        <result column="end_longitude" jdbcType="DECIMAL" property="longitude" />
        <result column="end_latitude" jdbcType="DECIMAL" property="latitude" />
    </resultMap>
  • 写回答

2条回答 默认 最新

  • limit、T 2023-04-19 15:50
    关注

    collection用这个

    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 5月24日
  • 创建了问题 4月19日