MyBatis:关联查询,相同字段名冲突(关联查询只返回了一条子记录)

简介: MyBatis:关联查询,相同字段名冲突(关联查询只返回了一条子记录)

目录

解决办法

案例:关联查询,相同字段名冲突

SQL语句-关联查询

Mapper配置

测试异常:关联显示了一条步骤信息,实际用户有4条步骤信息

问题分析:

问题解决1:别名

适用场景

处理办法

问题解决2:去掉相同属性的字段

适用场景

处理办法



解决办法

sql语句中给相同的属性起别名,

同时设置 resultMap 中的column为别名


案例:关联查询,相同字段名冲突

需求,根据vrTaskUuid查询用户任务和步骤信息,需要根据vrTaskUuid关联查询vr_user_oper_info(任务信息)和vr_user_oper_step_info(步骤信息)


SQL语句-关联查询


Mapper配置

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ruoyi.vr.mapper.VrUserOperAndStepMapper">
<!--任务信息-->
    <resultMap id="VrUserOperAndStepResult" type="com.ruoyi.vr.domain.VrUserOperAndStepDO">
        <id column="id" jdbcType="BIGINT" property="id" />
        <result column="user_id" jdbcType="BIGINT" property="userId" />
        <result column="vr_app_id" jdbcType="BIGINT" property="vrAppId" />
        <result column="task_id" jdbcType="BIGINT" property="taskId" />
        <result column="vr_task_uuid" jdbcType="VARCHAR" property="vrTaskUuid" />
        <result column="vr_app_status" jdbcType="INTEGER" property="vrAppStatus" />
        <result column="task_type" jdbcType="VARCHAR" property="taskType" />
        <result column="task_score" jdbcType="INTEGER" property="taskScore" />
        <result column="task_duration" jdbcType="INTEGER" property="taskDuration" />
        <result column="task_step_num" jdbcType="INTEGER" property="taskStepNum" />
        <result column="task_step_user" jdbcType="INTEGER" property="taskStepUser" />
        <result column="task_total_value" jdbcType="INTEGER" property="taskTotalValue" />
        <result column="task_observation" jdbcType="VARCHAR" property="taskObservation" />
        <result column="task_platform" jdbcType="VARCHAR" property="taskPlatform" />
        <result column="task_quality" jdbcType="VARCHAR" property="taskQuality" />
        <result column="stream_port" jdbcType="INTEGER" property="streamPort" />
        <result column="http_port" jdbcType="INTEGER" property="httpPort" />
        <result column="ip_addr" jdbcType="VARCHAR" property="ipAddr" />
        <result column="create_by" jdbcType="VARCHAR" property="createBy" />
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
        <collection property="stepInfos" javaType="java.util.List" resultMap="VrUserOperStepInfoResult"/>
    </resultMap>
<!--任务步骤信息-->
    <resultMap id="VrUserOperStepInfoResult" type="com.ruoyi.vr.domain.VrUserOperStepInfoDO">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="vr_task_uuid" jdbcType="VARCHAR" property="vrTaskUuid"/>
        <result column="task_step_name" jdbcType="BIGINT" property="taskStepName"/>
        <result column="step_score_value" jdbcType="INTEGER" property="stepScoreValue"/>
        <result column="step_score_user" jdbcType="INTEGER" property="stepScoreUser" />
        <result column="step_oper_duration" jdbcType="BIGINT" property="stepOperDuration"/>
        <result column="step_begin" jdbcType="TIMESTAMP" property="stepBegin"/>
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
    </resultMap>
 <!--查询用户任务和步骤-->
    <sql id="selectUserOperAndStep">
        select
            u.id, u.user_id, u.vr_app_id, u.task_id, u.vr_task_uuid, u.vr_app_status, u.task_score, u.task_type,
            u.task_duration, u.task_step_num, u.task_step_user, u.task_total_value,
            u.task_observation, u.task_platform, u.task_quality, u.stream_port, u.http_port, u.ip_addr, u.create_by,
           s.task_step_name, s.step_score_value, s.step_score_user, s.step_oper_duration, s.step_begin
        from vr_user_oper_info u
        left join vr_user_oper_step_info s on u.vr_task_uuid = s.vr_task_uuid
    </sql>
    <!--根据vrTaskUuid查询用户任务和步骤信息-->
    <select id="selectUserOperAndStepByUUID" parameterType="String" resultMap="VrUserOperAndStepResult">
        <include refid="selectUserOperAndStep"/>
        where u.vr_task_uuid = #{vrTaskUuid}
    </select>
</mapper>


测试异常:关联显示了一条步骤信息,实际用户有4条步骤信息

问题分析:

写mapper映射文件时,在写到一对一关联,一对多关联时,由于两个javabean的属性相同,导致在select时外部属性覆盖了内部属性


问题解决1:别名

适用场景

相同属性的字段都需要返回

处理办法

在sql语句中给相同的属性起别名:s.id as s_id

同时设置 resultMap中VrUserTaskScoreResult的id column为s_id:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ruoyi.vr.mapper.VrUserOperAndStepMapper">
<!--任务信息-->
    <resultMap id="VrUserOperAndStepResult" type="com.ruoyi.vr.domain.VrUserOperAndStepDO">
        <id column="id" jdbcType="BIGINT" property="id" />
        <result column="user_id" jdbcType="BIGINT" property="userId" />
        <result column="vr_app_id" jdbcType="BIGINT" property="vrAppId" />
        <result column="task_id" jdbcType="BIGINT" property="taskId" />
        <result column="vr_task_uuid" jdbcType="VARCHAR" property="vrTaskUuid" />
        <result column="vr_app_status" jdbcType="INTEGER" property="vrAppStatus" />
        <result column="task_type" jdbcType="VARCHAR" property="taskType" />
        <result column="task_score" jdbcType="INTEGER" property="taskScore" />
        <result column="task_duration" jdbcType="INTEGER" property="taskDuration" />
        <result column="task_step_num" jdbcType="INTEGER" property="taskStepNum" />
        <result column="task_step_user" jdbcType="INTEGER" property="taskStepUser" />
        <result column="task_total_value" jdbcType="INTEGER" property="taskTotalValue" />
        <result column="task_observation" jdbcType="VARCHAR" property="taskObservation" />
        <result column="task_platform" jdbcType="VARCHAR" property="taskPlatform" />
        <result column="task_quality" jdbcType="VARCHAR" property="taskQuality" />
        <result column="stream_port" jdbcType="INTEGER" property="streamPort" />
        <result column="http_port" jdbcType="INTEGER" property="httpPort" />
        <result column="ip_addr" jdbcType="VARCHAR" property="ipAddr" />
        <result column="create_by" jdbcType="VARCHAR" property="createBy" />
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
        <collection property="stepInfos" javaType="java.util.List" resultMap="VrUserOperStepInfoResult"/>
    </resultMap>
<!--任务步骤信息-->
    <resultMap id="VrUserOperStepInfoResult" type="com.ruoyi.vr.domain.VrUserOperStepInfoDO">
        <id column="s_id" jdbcType="BIGINT" property="id"/>
        <result column="vr_task_uuid" jdbcType="VARCHAR" property="vrTaskUuid"/>
        <result column="task_step_name" jdbcType="BIGINT" property="taskStepName"/>
        <result column="step_score_value" jdbcType="INTEGER" property="stepScoreValue"/>
        <result column="step_score_user" jdbcType="INTEGER" property="stepScoreUser" />
        <result column="step_oper_duration" jdbcType="BIGINT" property="stepOperDuration"/>
        <result column="step_begin" jdbcType="TIMESTAMP" property="stepBegin"/>
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
    </resultMap>
<!--查询用户任务和步骤-->
    <resultMap id="VrUserTaskScoreResult" type="com.ruoyi.vr.BO.VrUserTaskScoreBO">
        <result column="user_id" jdbcType="BIGINT" property="userId"/>
        <result column="vr_app_id" jdbcType="BIGINT" property="vrAppId"/>
        <result column="task_id" jdbcType="BIGINT" property="taskId"/>
        <result column="vr_task_uuid" jdbcType="VARCHAR" property="vrTaskUuid"/>
        <result column="task_score" jdbcType="INTEGER" property="taskScore"/>
        <result column="task_type" jdbcType="VARCHAR" property="taskType"/>
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="app_name" jdbcType="VARCHAR" property="appName" />
        <result column="task_name" jdbcType="VARCHAR" property="taskName" />
    </resultMap>
    <!--根据vrTaskUuid查询用户任务和步骤信息-->
    <sql id="selectUserOperAndStep">
        select
            u.id, u.user_id, u.vr_app_id, u.task_id, u.vr_task_uuid, u.vr_app_status, u.task_score, u.task_type,
            u.task_duration, u.task_step_num, u.task_step_user, u.task_total_value,
            u.task_observation, u.task_platform, u.task_quality, u.stream_port, u.http_port, u.ip_addr, u.create_by,
            s.id as s_id,s.task_step_name, s.step_score_value, s.step_score_user, s.step_oper_duration, s.step_begin
        from vr_user_oper_info u
        left join vr_user_oper_step_info s on u.vr_task_uuid = s.vr_task_uuid
    </sql>
    <!--根据UUID查询用户操作和步骤信息-->
    <select id="selectUserOperAndStepByUUID" parameterType="String" resultMap="VrUserOperAndStepResult">
        <include refid="selectUserOperAndStep"/>
        where u.vr_task_uuid = #{vrTaskUuid}
    </select>
</mapper>


问题解决2:去掉相同属性的字段

适用场景

相同属性的字段都不需要返回

处理办法

在sql语句中给相同的属性起别名:s.id as s_id

同时设置 resultMap中VrUserTaskScoreResult的id column为s_id:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ruoyi.vr.mapper.VrUserOperAndStepMapper">
<!--任务信息-->
    <resultMap id="VrUserOperAndStepResult" type="com.ruoyi.vr.domain.VrUserOperAndStepDO">
        <result column="user_id" jdbcType="BIGINT" property="userId" />
        <result column="vr_app_id" jdbcType="BIGINT" property="vrAppId" />
        <result column="task_id" jdbcType="BIGINT" property="taskId" />
        <result column="vr_task_uuid" jdbcType="VARCHAR" property="vrTaskUuid" />
        <result column="vr_app_status" jdbcType="INTEGER" property="vrAppStatus" />
        <result column="task_type" jdbcType="VARCHAR" property="taskType" />
        <result column="task_score" jdbcType="INTEGER" property="taskScore" />
        <result column="task_duration" jdbcType="INTEGER" property="taskDuration" />
        <result column="task_step_num" jdbcType="INTEGER" property="taskStepNum" />
        <result column="task_step_user" jdbcType="INTEGER" property="taskStepUser" />
        <result column="task_total_value" jdbcType="INTEGER" property="taskTotalValue" />
        <result column="task_observation" jdbcType="VARCHAR" property="taskObservation" />
        <result column="task_platform" jdbcType="VARCHAR" property="taskPlatform" />
        <result column="task_quality" jdbcType="VARCHAR" property="taskQuality" />
        <result column="stream_port" jdbcType="INTEGER" property="streamPort" />
        <result column="http_port" jdbcType="INTEGER" property="httpPort" />
        <result column="ip_addr" jdbcType="VARCHAR" property="ipAddr" />
        <result column="create_by" jdbcType="VARCHAR" property="createBy" />
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
        <collection property="stepInfos" javaType="java.util.List" resultMap="VrUserOperStepInfoResult"/>
    </resultMap>
<!--任务步骤信息-->
    <resultMap id="VrUserOperStepInfoResult" type="com.ruoyi.vr.domain.VrUserOperStepInfoDO">
        <result column="vr_task_uuid" jdbcType="VARCHAR" property="vrTaskUuid"/>
        <result column="task_step_name" jdbcType="BIGINT" property="taskStepName"/>
        <result column="step_score_value" jdbcType="INTEGER" property="stepScoreValue"/>
        <result column="step_score_user" jdbcType="INTEGER" property="stepScoreUser" />
        <result column="step_oper_duration" jdbcType="BIGINT" property="stepOperDuration"/>
        <result column="step_begin" jdbcType="TIMESTAMP" property="stepBegin"/>
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
    </resultMap>
<!--查询用户任务和步骤-->
    <resultMap id="VrUserTaskScoreResult" type="com.ruoyi.vr.BO.VrUserTaskScoreBO">
        <result column="user_id" jdbcType="BIGINT" property="userId"/>
        <result column="vr_app_id" jdbcType="BIGINT" property="vrAppId"/>
        <result column="task_id" jdbcType="BIGINT" property="taskId"/>
        <result column="vr_task_uuid" jdbcType="VARCHAR" property="vrTaskUuid"/>
        <result column="task_score" jdbcType="INTEGER" property="taskScore"/>
        <result column="task_type" jdbcType="VARCHAR" property="taskType"/>
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="app_name" jdbcType="VARCHAR" property="appName" />
        <result column="task_name" jdbcType="VARCHAR" property="taskName" />
    </resultMap>
    <!--根据vrTaskUuid查询用户任务和步骤信息-->
    <sql id="selectUserOperAndStep">
        select
            u.user_id, u.vr_app_id, u.task_id, u.vr_task_uuid, u.vr_app_status, u.task_score, u.task_type,
            u.task_duration, u.task_step_num, u.task_step_user, u.task_total_value,
            u.task_observation, u.task_platform, u.task_quality, u.stream_port, u.http_port, u.ip_addr, u.create_by,
            s.task_step_name, s.step_score_value, s.step_score_user, s.step_oper_duration, s.step_begin
        from vr_user_oper_info u
        left join vr_user_oper_step_info s on u.vr_task_uuid = s.vr_task_uuid
    </sql>
    <!--根据UUID查询用户操作和步骤信息-->
    <select id="selectUserOperAndStepByUUID" parameterType="String" resultMap="VrUserOperAndStepResult">
        <include refid="selectUserOperAndStep"/>
        where u.vr_task_uuid = #{vrTaskUuid}
    </select>
</mapper>

 


目录
相关文章
|
3月前
|
Java 数据库连接 数据库
mybatis查询数据,返回的对象少了一个字段
mybatis查询数据,返回的对象少了一个字段
243 8
|
18天前
|
SQL Java 数据库连接
spring和Mybatis的各种查询
Spring 和 MyBatis 的结合使得数据访问层的开发变得更加简洁和高效。通过以上各种查询操作的详细讲解,我们可以看到 MyBatis 在处理简单查询、条件查询、分页查询、联合查询和动态 SQL 查询方面的强大功能。熟练掌握这些操作,可以极大提升开发效率和代码质量。
31 3
|
1月前
|
SQL 安全 Java
MyBatis-Plus条件构造器:构建安全、高效的数据库查询
MyBatis-Plus 提供了一套强大的条件构造器(Wrapper),用于构建复杂的数据库查询条件。Wrapper 类允许开发者以链式调用的方式构造查询条件,无需编写繁琐的 SQL 语句,从而提高开发效率并减少 SQL 注入的风险。
27 1
MyBatis-Plus条件构造器:构建安全、高效的数据库查询
|
2月前
|
SQL Java 数据库连接
mybatis如何仅仅查询某个表的几个字段
【10月更文挑战第19天】mybatis如何仅仅查询某个表的几个字段
75 1
|
3月前
|
SQL XML Java
mybatis复习04高级查询 一对多,多对一的映射处理,collection和association标签的使用
文章介绍了MyBatis中高级查询的一对多和多对一映射处理,包括创建数据库表、抽象对应的实体类、使用resultMap中的association和collection标签进行映射处理,以及如何实现级联查询和分步查询。此外,还补充了延迟加载的设置和用法。
mybatis复习04高级查询 一对多,多对一的映射处理,collection和association标签的使用
|
7月前
|
SQL
MyBatis-Plus-Join关联查询
MyBatis-Plus-Join关联查询
329 2
|
7月前
|
SQL Java 关系型数据库
Mybatis多表关联查询与动态SQL(下)
Mybatis多表关联查询与动态SQL
137 0
|
7月前
|
SQL Java 数据库连接
Mybatis多表关联查询与动态SQL(上)
Mybatis多表关联查询与动态SQL
222 0
|
7月前
|
SQL 缓存 Java
mybatis 一对多查询
mybatis 一对多查询
130 0
|
7月前
|
SQL XML Java
MyBatis-Plus多表关联查询
MyBatis-Plus多表关联查询
653 0