由于resultMap配置中对每个关联对象的name属性都进行了映射,这可能导致解析时的混乱。

admin2024-07-04  9

代码

<select id="selectTeachList" parameterType="Teach" resultMap="TeachResult">
    select  *
    from teach t
        join coach c on c.id = t.cid
        join course c2 on c2.id = t.courseid
        join member m on m.m_id = t.mid

    <where></where>

</select>  <resultMap type="Teach" id="TeachResult">
    <result property="id"    column="id"    />
    <result property="mid"    column="mid"    />
    <result property="cid"    column="cid"    />
    <result property="courseid"    column="courseid"    />
    <result property="count"    column="count"    />
    <association property="member"    column="mid" javaType="Member" resultMap="MemberResult" />
    <association property="coach"    column="cid" javaType="Coach" resultMap="CoachResult" />
    <association property="course"    column="courseid" javaType="Course" resultMap="CourseResult" />

</resultMap>
<resultMap type="Coach" id="CoachResult">
    <result property="id"    column="id"    />
    <result property="name"    column="name"    />
    <result property="phone"    column="phone"    />
    <result property="sex"    column="sex"    />
    <result property="address"    column="address"    />
    <result property="workDate"    column="work_date"    />
    <result property="age"    column="age"    />
    <result property="salary"    column="salary"    />
</resultMap>


<resultMap type="Course" id="CourseResult">
    <result property="id"    column="id"    />
    <result property="name"    column="name"    />
    <result property="price"    column="price"    />
</resultMap>
<resultMap type="Member" id="MemberResult">
    <result property="mId"    column="m_id"    />
    <result property="name"    column="name"    />
    <result property="age"    column="age"    />
    <result property="cardType"    column="card_type"    />
    <result property="insDate"    column="ins_date"    />
    <result property="expirationDate"    column="expiration_date"    />
    <result property="userId"    column="user_id"    />
    <result property="userId"    column="user_id"    />
    <result property="balance"    column="balance"    />
</resultMap>package com.ruoyi.coach.domain;

问题可能出在SQL查询的JOIN操作以及结果映射的细节上。你的查询使用了SELECT *来获取所有列,但在多表联查的情况下,这种做法可能会导致列名冲突。具体来说,如果membercoachcourse表中都有名为name的列,那么在结果集中,name列将会只显示来自最后一个JOIN的表(在这个案例中通常是course表的name列),但由于resultMap配置中对每个关联对象的name属性都进行了映射,这可能导致解析时的混乱。

为了解决这个问题,你需要确保在多表联查时,所选列是唯一的,可以通过指定表别名来区分同名列,例如:

 

Sql

1SELECT 
2    t.id, 
3    t.mid, t.cid, t.courseid, t.count,
4    m.m_id AS member_id, m.name AS member_name, m.age, m.card_type, m.ins_date, m.expiration_date, m.user_id, m.balance,
5    c.id AS coach_id, c.name AS coach_name, c.phone, c.sex, c.address, c.work_date, c.age AS coach_age, c.salary,
6    c2.id AS course_id, c2.name AS course_name, c2.price
7FROM teach t
8JOIN coach c ON c.id = t.cid
9JOIN course c2 ON c2.id = t.courseid
10JOIN member m ON m.m_id = t.mid

并且,确保在相应的resultMap中使用更新后的列名(带有别名的列名),例如:

 

Xml

1<resultMap type="Teach" id="TeachResult">
2    <!-- ... -->
3    <association property="member" column="member_id" javaType="Member" resultMap="MemberResult"/>
4    <association property="coach" column="coach_id" javaType="Coach" resultMap="CoachResult"/>
5    <association property="course" column="course_id" javaType="Course" resultMap="CourseResult"/>
6    <!-- ... -->
7</resultMap>
8
9<resultMap type="Member" id="MemberResult">
10    <result property="mId" column="member_id"/>
11    <result property="name" column="member_name"/>
12    <!-- 其他属性映射... -->
13</resultMap>
14
15<resultMap type="Coach" id="CoachResult">
16    <result property="id" column="coach_id"/>
17    <result property="name" column="coach_name"/>
18    <!-- 其他属性映射... -->
19</resultMap>
20
21<resultMap type="Course" id="CourseResult">
22    <result property="id" column="course_id"/>
23    <result property="name" column="course_name"/>
24    <!-- 其他属性映射... -->
25</resultMap>

通过这种方式,可以避免列名冲突导致的映射错误,确保CoachCourseMember对象的属性正确填充。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明原文出处。如若内容造成侵权/违法违规/事实不符,请联系SD编程学习网:675289112@qq.com进行投诉反馈,一经查实,立即删除!