mybatis 一对多

蚂蚁渣 发布于 01/19 11:24
阅读 255
收藏 0

使用 mybatis 的collection标签来实现一对多,当多的一方为nul时,一的也要显示。这个要这么实现啊。。。。。。。。。。。。。。。。

<resultMap id="RoomTypeResultMap" type="com.sucat.model.RoomType" >
  <!--
    WARNING - @mbg.generated
  -->
  <result column="room_type_id" property="roomTypeId" jdbcType="INTEGER" />
  <result column="room_type_name" property="roomTypeName" jdbcType="VARCHAR" />
  <result column="hotel_id" property="hotelId" jdbcType="VARCHAR" />
  <result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
  <result column="modify_time" property="modifyTime" jdbcType="TIMESTAMP" />
  <result column="remark" property="remark" jdbcType="VARCHAR" />
  <result column="data_status" property="dataStatus" jdbcType="INTEGER" />
  <result column="creater" property="creater" jdbcType="VARCHAR" />

  <collection property="rooms" ofType="com.sucat.model.Room">
    <id column="rm_room_id"  property="roomId"/>
    <result column="rm_room_code"  property="roomCode" />
    <result column="rm_room_type_id"  property="roomTypeId" />
    <result column="rm_hotel_id" property="hotelId" />
    <result column="rm_floor" property="floor" />
    <result column="rm_roomTypeName" property="roomTypeName" />
    <result column="rm_rs_remark" property="remark" />
  </collection>

</resultMap>
<select id="queryRoomTypeList" parameterType="com.sucat.model.RoomType" resultMap="RoomTypeResultMap">
    select rt.`*`,rm.room_id as rm_room_id,rm.room_code as rm_room_code,rm.room_type_id as rm_room_type_id,rm.hotel_id as rm_hotel_id,rm.floor as rm_floor,rt.room_type_name as rm_roomTypeName,rm.remark as rm_rs_remark
    from room_type rt join room rm on  rt.hotel_id = #{hotelId} and rm.room_type_id = rt.room_type_id
    order by rm.create_time  desc
</select>
public class RoomType {
 
    private Integer roomTypeId;

    private String roomTypeName;

    private List<Room> rooms;
}

public class Room {
    private String roomId;
    private String roomCode;
    private Integer roomTypeId;
    private String floor;
    private String roomTypeName;
}

查询结果是:

{ "roomTypeList": [ { "roomTypeId": 6, "roomTypeName": "双人房", "hotelId": "190118B66PG2WY3C", "createTime": "2019-01-19T16:47:20.000+0000", "modifyTime": "2019-01-19T16:47:58.000+0000", "remark": null, "dataStatus": 1, "creater": "1901118Z0H618TT4", "rooms": [ { "roomId": "ads0151", "roomCode": "B001", "roomTypeId": 6, "hotelId": "190118B66PG2WY3C", "remark": null, "createTime": null, "modifyTime": null, "floor": "1", "roomStatus": null, "roomDynamic": null, "dataStatus": null, "creater": null, "roomTypeName": "双人房" } ] }, { "roomTypeId": 5, "roomTypeName": "单人房", "hotelId": "190118B66PG2WY3C", "createTime": "2019-01-18T21:29:45.000+0000", "modifyTime": "2019-01-19T16:47:56.000+0000", "remark": null, "dataStatus": 1, "creater": "1901118Z0H618TT4", "rooms": [ { "roomId": "20aad11", "roomCode": "a002", "roomTypeId": 5, "hotelId": "190118B66PG2WY3C", "remark": null, "createTime": null, "modifyTime": null, "floor": "1", "roomStatus": null, "roomDynamic": null, "dataStatus": null, "creater": null, "roomTypeName": "单人房" }, { "roomId": "adad101", "roomCode": "a001", "roomTypeId": 5, "hotelId": "190118B66PG2WY3C", "remark": null, "createTime": null, "modifyTime": null, "floor": "1", "roomStatus": null, "roomDynamic": null, "dataStatus": null, "creater": null, "roomTypeName": "单人房" } ] } ] }

但是我数据库里面有三个房型

我想要的结果是{单人房{001,002},双人房{003},三人间};

这个要怎么实现啊

 

 

 

加载中
0
蓝水晶飞机
蓝水晶飞机

你要么就不用 JOIN 查询,要么就 LEFT JOIN !试试 LEFT JOIN 先吧!

0
海豚之外

左连接右连接的问题吧?

返回顶部
顶部