Mybatis级联操作的实现

背景

Mybatis的级联操作主要是针对一对多、多对一和多对多的情况而设定的。级联是在resultMap标签中配置的。级联并不是必须的,好处就是获取关联数据便捷,但如果级联过多会增加系统的复杂度,同时降低系统的性能。

一对多

一个人对应多件衣服为例

只查询一次

(1)先来看一下 标签中的属性:
property:对象属性的名称,对应一对多中一的字段名
ofType:指定的是映射到集合属性中bean的类型
column:所对应的外键字段名称
select:使用另一个查询封装的结果
xml文件

 <!--一对多级联查询 resultMap-->
    <resultMap id="oneToMoreResultMap" type="com.lks.bean.User">
        <id column="id" property="id"></id>
        <result property="name" column="name"/>
        <result property="age" column="age"/>
        <result property="county" column="county"/>
        <result property="date" column="date"/>
        <!--对应一对多中一的字段名-->
        <collection property="clothes" ofType="com.lks.bean.Clothe">
            <id column="clothe_id" property="clotheId"></id>
            <result column="clothe_color" property="clotheColor"></result>
            <result column="user_id" property="userId"></result>
        </collection>
    </resultMap>

    <select id="oneToMoreQuery" parameterType="int" resultMap="oneToMoreResultMap">
        select id, name, age, county, date, clothe_id, clothe_color
        FROM users join clothes on users.id = clothes.user_id
        where users.id = #{id}
    </select>

主要

  • 5
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值