Mybtis 一对多 关联查询 <collection>的简明例子

 <collection>用于两张一对一对应的表的查询,是<resultMap>的子标签。

即A表与B表对应为“一个牧羊人多只羊”,或者说“多只羊一个牧羊人”

collection:收集

表的字段

A表:D1,D2,D3,D4,D5

B表:D6,D7

类的属性

ccc1类:DD1,DD2,DD3,D4,List<ccc2> CCC2List;

ccc2类:DD6,DD7

<collection>两次查询的方法

关键代码

    <resultMap id="ResultMapper" type="ccc1">
        <id column="D1" property="DD1"/>
        <result column="D2" property="DD2"/>
        <result column="D3" property="DD3"/>
        <result column="D4" property="DD4"/>

        <collection column="D1" property="CCC2List" ofType="Orders"
                    select="com.xxx.mapper.xxxMapper.findB"/>

    </resultMap>
    <select id="findA" parameterType="Integer" resultMap="ResultMapper">
        select * from xxx where D1=#{D1}
    </select>

运行过程:

        1,首先执行 findA的SQL:  select * from xxx where D1=#{D1}  获取结果A表结果

        2,再通过<resultMap>里的<id><result>(二者是差不多的效果)把结果分给java类ccc1对应的属性。即D1字段对应DD1属性。

        3,再  执  行 findB的SQL: select="com.xxx.xxx.ccc2Mapper.findB"  获取B表结果。笔者这里没有给出findB的SQL语句内容。

这里  <collection column="D1"  中D1指的是A表中的字段,用作关联B表的依据,传值给findB,用作findB的SQL语句的执行参数。

        4,执行finB获取的结果直接给ccc1类中的ccc2类型List的属性CCC2List,成为CCC2List的元素。

        5,执行完毕

A表:D1,D2,D3,D4,D5

B表:D6,D7

ccc1类:DD1,DD2,DD3,D4,List<ccc2> CCC2List;

ccc2类:DD6,DD7

<association>一次查询的方法

关键代码

    <resultMap id="Result" type="ccc1">
        <id column="D1" property="DD1"/>
        <result column="D2" property="DD2"/>
        <result column="D3" property="DD3"/>
        <result column="D4" property="DD4"/>
        <collection property="CCC2List" ofType="ccc2">
            <id property="DD6" column="D6"/>
            <result property="DD7" column="D7"/>
        </collection>

    </resultMap>
    <select id="findA" parameterType="Integer"
            resultMap="Result">
        select A.*,B.* from A,B where A.D1 = B.D6
        and A.D1 = #{D1}
    </select>

ccc1类:DD1,DD2,DD3,D4,List<ccc2> CCC2List;

ccc2类:DD6,DD7

运行过程:

        1,首先执行findA: select A.*,B.* from A,B where A.D1 = B.D6 and A.D1 = #{D1}

        2,再通过<resultMap>里的<id>和<result>(二者是同样的效果)把结果分给java类ccc1对应的属性。即D1字段对应DD1属性。

        3,之后通过 <collection>把D6、D7字段合成ccc2类型的数据。即D7字段对应ccc2类中的属性DD7。D6对应DD6。因为是一对多,所以查到的D6、D7可能有多组。把这些组分别合成ccc2类型给List<ccc2> CCC2List

.        4,执行完毕

一般建议使用一次查询的方法,且这种方法不用写findB方法。

完整例子

<?xml version="1.0" encoding="UTF8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xxx.xxx.ccc1Mapper">


    <resultMap id="ResultMapper" type="ccc1">
        <id column="D1" property="DD1"/>
        <result column="D2" property="DD2"/>
        <result column="D3" property="DD3"/>
        <result column="D4" property="DD4"/>

        <collection column="D1" property="CCC2List" ofType="Orders"
                    select="com.xxx.mapper.xxxMapper.findB"/>

    </resultMap>
    <select id="findA" parameterType="Integer" resultMap="ResultMapper">
        select * from xxx where D1=#{D1}
    </select>



    <resultMap id="Result" type="ccc1">
        <id column="D1" property="DD1"/>
        <result column="D2" property="DD2"/>
        <result column="D3" property="DD3"/>
        <result column="D4" property="DD4"/>
        <collection property="CCC2List" ofType="ccc2">
            <id property="DD6" column="D6"/>
            <result property="DD7" column="D7"/>
        </collection>

    </resultMap>
    <select id="findA" parameterType="Integer"
            resultMap="Result">
        select A.*,B.* from A,B where A.D1 = B.D6
        and A.D1 = #{D1}
    </select>

</mapper>

调用

        SqlSession session = MybatisUtils.getSeesion();//自定义的工具类用来获取session
        xxx x= session.selectOne("com.xxx.mapper.xxxMapper.findA", 1);

        System.out.println(x);
        session.close();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值