Mabatis的嵌套查询(多表查询)

 

需求:实际:

查询setmeal 的全部信息

实体类中不仅有基本数据还有checkGroup的集合

其中checkGroup中的属性又有checkItem的集合

查询的setmeal对象中 需要包含一个或多个checkGroup 其中一个checkGroup又有多个checkItem

根据setmeal的id查询 

就进行了嵌套查询

具体逻辑:

在查询setmeal返回时 重新给返回的数据进行命名赋值 原本的resultType是让它自己内部进行封装成你指定的类 resultMap是可以进行指定类型和进行内部数据的调整

其他的名字赋值不用变还是原来的就行

在checkGroup集合那个属性上  进行数据改变 column是发送携带的参数 在引用的执行select时可以使用 然后就将checkgroup集合的值指定为引用sql的返回值

     <!--property是实体类名 oftype 是集合中属性的类 column里面是传递的数据 select是执行的语句这里直接指向了一个select方法,去执行那个dao中的那个方法-->
//集合进行分离
        <collection
                property="checkGroups"
                ofType="com.itheima.pojo.CheckGroup"
                column="id"
                select="com.itheima.dao.CheckGroupDao.findCheckGroupById"
        >

 进入checkgroupdao执行被引用的方法

执行的是

 select *
        from t_checkgroup where id in (select checkgroup_id from t_setmeal_checkgroup where setmeal_id=#{id});

 根据传来的setmeal的属性id查询setmeal和checkgroup的中间表 查询符合该id的checkgroup的ids

它Id的集合 然后在查询checkgroupid进行匹配ids的chectGroup集合

在返回时再进行一个resultMap

将checkItem集合进行一下包装

将这个属性赋值为该sql的返回值

 select * from t_checkitem where id in (select checkitem_id from t_checkgroup_checkitem where checkgroup_id =#{id})

返回一个checkItem的集合 子查询为 跟携带的checkgroup的属性Id 查询中间表 得到checkitem的Id集合 然后在进行id匹配查询(in) 得到checkitem集合进行返回

 总结:

倒叙

查询出checkitem集合返回给一个checkgroup

查询出checkgroup集合返回给setmeal 

1:SetMealDao:

mapper

/*
* 嵌套多表
* */
    Setmeal findById(Integer id);

xml

 <!--嵌套多表查询-->
    <resultMap id="setmealResultMap" type="com.itheima.pojo.Setmeal">
        <id property="id" column="id"/>
        <result column="name" property="name"/>
        <result column="code" property="code"/>
        <result column="helpCode" property="helpCode"/>
        <result column="sex" property="sex"/>
        <result column="age" property="age"/>
        <result column="price" property="price"/>
        <result column="remark" property="remark"/>
        <result column="attention" property="attention"/>
        <result column="img" property="img"/>
        <!--property是实体类名 oftype 是集合中属性的类 column里面是传递的数据 select是执行的语句这里直接指向了一个select方法,去执行那个dao中的那个方法-->
//集合进行分离
        <collection
                property="checkGroups"
                ofType="com.itheima.pojo.CheckGroup"
                column="id"
                select="com.itheima.dao.CheckGroupDao.findCheckGroupById"
        >
        </collection>
    </resultMap>
    <select id="findById" resultMap="setmealResultMap">
        select *
        from t_setmeal where id=#{id};
    </select>

 2:checkGroupDao:

不用写mapper

直接xml

 <resultMap id="findByIdResultMap" type="com.itheima.pojo.CheckGroup">
        <id column="id" property="id"/>
        <result column="name" property="name"/>
        <result column="code" property="code"/>
        <result column="helpCode" property="helpCode"/>
        <result column="sex" property="sex"/>
        <result column="remark" property="remark"/>
        <result column="attention" property="attention"/>
        <collection property="checkItems"
                    ofType="com.itheima.pojo.CheckItem"
                    column="id"
                    select="com.itheima.dao.CheckItemDao.findCheckItemById"
        ></collection>

    </resultMap>
<!--多表联合查询-->
    <select id="findCheckGroupById" resultMap="findByIdResultMap">
        select *
        from t_checkgroup where id in (select checkgroup_id from t_setmeal_checkgroup where setmeal_id=#{id});
    </select>

 3:checkItemDao:

不用mapper直接xml

 <!--多表查询 -->
    <select id="findCheckItemById" resultType="com.itheima.pojo.CheckItem">
        select * from t_checkitem where id in (select checkitem_id from t_checkgroup_checkitem where checkgroup_id =#{id})
    </select>

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值