mybatis resultMap标签应用

16 篇文章 0 订阅

一对一

一对一的映射配置association标签
    association属性:
    property:表示要映射的pojo类的属性名
    javaType:表示要映射的属性的类型
子查询
    select:根据select的id执行指定的select语句,如果是别的映射文件那么就全限定名+id(例如:com.zhang.dao.MemberDao.getMemberById)
    column:关联的id字段名
    <!--
        一对一的映射配置association标签
        association属性:
        property:表示要映射的pojo类的属性名
        javaType:表示要映射的属性的类型

    -->
    <resultMap id="infoResultMap" type="Info">
        <id property="id" column="id" />
        <result property="title" column="title"></result>
        <association property="member" javaType="Member">
            <id property="id" column="id"/>
            <result property="username" column="username"></result>
        </association>
    </resultMap>
    <!--
    select * from dlq_info as info
            left join dlq_member as member
                on info.member_id = member.id
            where info.id=#{id}
    -->
    <select id="findById" parameterType="integer" resultMap="infoResultMap">
        select * from dlq_info info,dlq_member member
        where info.member_id = member.id
          and info.id=#{id}
    </select>

一对多

<!--
        一对多使用collection标签
        collection标签属性:
            property:pojo类中的属性名
            ofType:对应的属性的类型
    -->

    <!--
        resultMap属性:
        id:与select标签中的resultMap对应即可,唯一标识
        type:表示对哪个pojo类进行映射
        resultMap子标签:
        id标签:表示对主键进行映射
            property:pojo类中的属性名
            column:数据库中的字段名
        result标签:表示对其他非主键进行映射
    -->
    <!--
        一对多使用collection标签
        collection标签属性:
            property:pojo类中的属性名
            ofType:对应的属性的类型
    -->
    <resultMap id="infos" type="Member">
        <id property="id" column="id" />
        <result property="username" column="username"></result>
        <collection property="infos" ofType="com.angen.pojo.Info">
            <result column="id" property="id"></result>
            <result column="title" property="title"></result>
        </collection>
    </resultMap>

    <select id="findInfoById" parameterType="integer" resultMap="infos">
        select * from dlq_member as member
        left join dlq_info as info
            on member.id=info.member_id
            where member.id=#{id}
    </select>

多对多跟一对多差不多


一对一映射,嵌套查询,会产生n+1,也就是多执行一次sql语句

Mybatis(十二)一对一关系处理 - 知乎

association标签中的select属性和column属性

场景:两条sql语句,一条sql语句需要另一条sql语句的查询结果的某个值来查询

<select id="findById" parameterType="integer" resultType="com.angen.pojo.Member">
        select * from dlq_member where id=#{id}
    </select>
<resultMap id="infoTwo" type="com.angen.pojo.Info">
        <id property="id" column="id"/>
        <result property="title" column="title"></result>
        <association property="member" javaType="Member"
                     column="member_id"
                select="com.angen.dao.MemberDao.findById"
        >

        </association>
    </resultMap>
    <select id="findByIdTwo" parameterType="integer" resultMap="infoTwo">
        select * from dlq_info where id=#{id}
    </select>

以上,这样就可以不用在association标签中自己写映射了。

column属性:传入哪个字段提供给select="com.angen.dao.MemberDao.findById"对应的那个sql语句来查询

select属性:映射配置文件的命名空间+对应的id.

上面是分步骤查询

延迟加载(懒加载)fetchType="lazy"局部延迟加载

<resultMap id="infoTwo" type="com.angen.pojo.Info">
        <id property="id" column="id"/>
        <result property="title" column="title"></result>
        <association property="member" javaType="Member"
                     column="member_id"
                select="com.angen.dao.MemberDao.findById"
                     fetchType="lazy"
        >

        </association>
    </resultMap>

加上这个属性后,如果我们不用member里面的数据就不会查询member的sql语句。

全局延迟加载,在核心配置文件中配置

<settings>
        <setting name="lazyLoadingEnabled" value="ture"/>
    </settings>

一对多的分步执行,延时加载跟一对一差不多,只不过是collection标签

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值