mybatis一对一,一对多映射

mybatis在XML中编写sql语句,虽然编码挺多的,但是对于一多一,多对多查询,数据量多的话还是感觉用mybatis比较吃香。遇到循环查询的话,感觉流(stream)都没mybatis好用;

要是单表单条件的话使用mybatis-plus比较好些,因为plus没太多的沉余代码,plus一遇到循环的话查询就不是很快了。以上观点大家可以试试哈!

 

下面直接上代码了哈

一对一映射

 <association property="memberInfo" column="info_id" javaType="com.cainaer.member.domain.MemberInfo"  resultMap="MemMemberInfoResult"/>
这里是会员登录信息跟会员信息一对一映射
property:对应会员信息实体
column:会员信息主键id
javaType:映射类型
resultMap:映射结果

一对多映射

<collection property="subCategoryList" javaType="java.util.List" resultMap="SubCategoryMap"/>

 

<!-- 接待室映射结果集 -->
    <resultMap id="receptionRoomMap" type="com.zerody.customer.vo.ReceptionRoomVo">
        <id property="id" column="id"/>
        <result property="companyId" column="company_id"/>
        <result property="name" column="room_name"/>
        <result property="status" column="room_status"/>
        <result property="sort" column="room_sort"/>
        <result property="createTime" column="room_create_time"/>
        <result property="updateTime" column="room_update_time"/>
        <result property="deleted" column="room_deleted"/>
        <collection property="receptionLocationList" ofType="com.zerody.customer.vo.ReceptionLocationVo">
            <id property="id" column="location_id"/>
            <result property="receptionId" column="reception_id"/>
            <result property="companyId" column="location_company_id"/>
            <result property="name" column="location_name"/>
            <result property="status" column="location_status"/>
            <result property="sort" column="location_sort"/>
            <result property="createTime" column="location_create_time"/>
            <result property="updateTime" column="location_update_time"/>
            <result property="deleted" column="location_deleted"/>
        </collection>
    </resultMap>

 

 

<select id="getAppPageRoom" resultMap="receptionRoomMap">
        SELECT
        r.id,
        r.company_id AS company_id,
        r.name AS room_name,
        r.status AS room_status,
        r.sort AS room_sort,
        r.create_time AS room_create_time,
        r.update_time AS room_update_time,
        r.deleted AS room_deleted,
        l.id as location_id,
        l.reception_id,
        l.company_id AS location_company_id,
        l.name AS location_name,
        l.status AS location_status,
        l.sort AS location_sort,
        l.create_time AS location_create_time,
        l.update_time AS location_update_time,
        l.deleted AS location_deleted
        FROM reception_room r
        LEFT JOIN reception_location l ON r.id = l.reception_id
    </select>

 

注意点:reception_location表的id 和 reception_room id需要其中一个表的id取别名,不然就去除id,否则会影响一对多映射效果!

 

posted @ 2021-05-17 22:16  安详的苦丁茶  阅读(231)  评论(0编辑  收藏  举报