mybatis一对多关系多表查询去重,不定条件查询,duplicated column "id"

首先这是一个多表查询,一对多关系,君悦酒店及既可能存在北京,也可能存在上海,在mysql里面的结果如下:

 一开始我的查询语句,以及映射情况如下

<resultMap id="BaseResultMap2" type="cn.edu.bcu.longtermroom.hotel.pc.entity.HotelExtend2">
        <id property="id" column="id"/>
        <result property="gmtCreat" column="gmt_creat" />
        <result property="gmtModified" column="gmt_modified" />
        <result property="hotelName" column="hotel_name" />
        <result property="brandId" column="brand_id" />
        <result property="saleStatus" column="sale_status" />
        <result property="lastOrder" column="last_order" />
        <result property="lastConfirm" column="last_confirm"/>
        <result property="star" column="star" />
        <result property="address" column="address" />
        <result property="telephone" column="telephone" />
        <result property="introduce" column="introduce" />
        <result property="lastOrder" column="last_order" />
        <result property="remain" column="remain" />
        <association property="brand" javaType="cn.edu.bcu.longtermroom.hotel.pc.entity.Brand">
            <id property="id" column="id"></id>
            <result property="brandName" column="brand_name"/>
            <result property="icon" column="icon"/>
            <result property="brandDetail" column="brand_detail"/>
            <result property="brandRemark" column="brand_remark"/>
        </association>
        <association property="city" javaType="cn.edu.bcu.longtermroom.hotel.pc.entity.City">
            <id property="id" column="id"></id>
            <result property="cityName" column="city_name"></result>
        </association>
    </resultMap>


<select id="selectHotelByMany" resultMap="BaseResultMap2">
        select distinct  h.id,h.gmt_creat,h.gmt_modified,h.hotel_name,h.brand_id,h.sale_status,h.last_order,h.last_confirm,h.star,h.address,
        b.brand_name,b.icon,b.brand_detail,b.brand_remark,
        city.id,city.city_name
        from hotel h
        left join brand b on h.brand_id=b.id
        left join hotel_area on h.id=hotel_area.hotel_id
        left join city_area on hotel_area.area_id=city_area.area_id
        left join city on city_area.city_id=city.id
        <where>
            <if test="cityId!=null">
                city.id = #{cityId}
            </if>
            <if test="hotelName!=null">
                and h.hotel_name like  '%${hotelName}%'
            </if>
            <if test="hotelId!=null">
                and h.id = #{hotelId}
            </if>
            <if test="saleStatus!=null">
                and h.sale_status= #{saleStatus}
            </if>
        </where>
    </select>

我的查询里面查了两个id,一个酒店id一个城市id,映射直接没做处理一直报错duplicated column “id”,还是映射id重了呗,因为查询语句是没有问题的在navcat里面试过,那就是映射的问题。 重了id,那我就取别名

首先这个城市id列取别名为cid ,对应的映射cid的如下,那么这个报错解决了,注意因为品牌id我没有进行select出来,是自动映射进去的,不会报错,但是数据其实是错误的,数据是酒店id,因为这里写的直接是id,数据中id是酒店id所以是错误的,这里我试过select加上b.id就会报错还是duplicate column 'id'这个错误

 

 但是这样又出现了问题,数据不全,应该有四条数据最终只有三条,原因是mybatis自动根据id去重,也就是主键id,需要加上另外一个主键id城市id,那么mybatis会根据这两个进行去重,数据就是完整的了

同时封装类Hotelextend类再加一个属性cid

另外我发现一个问题就是查询的时候不是从酒店id为1开始,而是从6开始,乱序的,这应该是由于mysql引擎优化器的原因,mysql没有义务帮你按照id排序,惯性思维是根据id从上到下执行,这是正常的现象,想要排序直接用orderby吧

 mybatis的坑啊,一个一个踩,写出来希望同时在采坑的人能节约点时间

发布了30 篇原创文章 · 获赞 18 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_40898368/article/details/90711675