Mybatis关联查询

本文详细介绍了在Java中使用MyBatis进行SQL嵌套查询和结果映射的操作,包括如何通过<sqlid>定义通用字段,<resultMap>设置属性映射,以及在<collection>中使用子查询来获取复杂关联数据。示例展示了从Setmeal到CheckGroup再到CheckItem的多级数据获取过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近发现sql的嵌套查询比较好,就记一下

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

解释:
	通过 collection 对 List 进行映射
	property : 想要添加到类的那个属性上 
    javaType : 这个属性类型
    ofType : 查询到属性具体类型
    column : 根据那个表字段查询
# SetmealDao中

<sql id="AllParam">id, name, code, helpCode, sex, age, price, remark, attention, img</sql>

<resultMap id="setmeal" type="com.pojo.Setmeal">
    <id property="id" column="id"/>
    <result property="name" column="name"/>
    <result property="code" column="code"/>
    <result property="helpCode" column="helpCode"/>
    <result property="sex" column="sex"/>
    <result property="age" column="age"/>
    <result property="price" column="price"/>
    <result property="remark" column="remark"/>
    <result property="attention" column="attention"/>
    <result property="img" column="img"/>
</resultMap>

<resultMap id="findSetMealIsAll" type="com.pojo.Setmeal" extends="setmeal">
    <collection property="checkGroups"
                javaType="ArrayList"
                ofType="com.itheima.pojo.CheckGroup"
                column="id"
                select="com.dao.CheckGroupDao.findCheckGroupById">

    </collection>
</resultMap>

# 查询语句
<select id="findSetMealIsAll" parameterType="integer" resultMap="findSetMealIsAll">
    SELECT <include refid="AllParam"/>
    FROM t_setmeal where id=#{id}
</select>

第二个Dao

# CheckGroupDao中

<sql id="AllParam">id, code, name, helpCode, sex, remark, attention</sql>

<resultMap id="CheckGroup" type="com.pojo.CheckGroup">
   <id property="id" column="id"/>
   <result property="name" column="name"/>
   <result property="code" column="code"/>
   <result property="helpCode" column="helpCode"/>
   <result property="sex" column="sex"/>
   <result property="remark" column="remark"/>
   <result property="attention" column="attention"/>
</resultMap>

<resultMap id="findCheckGroupById" type="com.pojo.CheckGroup" extends="CheckGroup">
   <collection property="checkItems"
               ofType="com.itheima.pojo.CheckItem"
               column="id"
               javaType="ArrayList"
               select="com.dao.CheckitemDao.findCheckItemById">
   </collection>
</resultMap>

<select id="findCheckGroupById" resultMap="findCheckGroupById">
   SELECT <include refid="AllParam"/>
   FROM `t_checkgroup` tcg INNER JOIN `t_setmeal_checkgroup` tsc ON tcg.`id` = tsc.`checkgroup_id` WHERE tsc.`setmeal_id`=#{id}
</select>

第三个Dao

# CheckitemDao中

<sql id="allParam">id,code,name,sex,age,price,type,attention,remark</sql>

<resultMap id="CheckItems" type="com.pojo.CheckItem">
    <id property="id" column="id"/>
    <result property="name" column="name"/>
    <result property="code" column="code"/>
    <result property="age" column="age"/>
    <result property="sex" column="sex"/>
    <result property="remark" column="remark"/>
    <result property="attention" column="attention"/>
    <result property="price" column="price"/>
    <result property="type" column="type"/>
</resultMap>

<select id="findCheckItemById" resultMap="CheckItems">
    SELECT <include refid="allParam"/>
    FROM `t_checkitem` tc INNER JOIN `t_checkgroup_checkitem` tcc ON tc.`id` = tcc.`checkitem_id` WHERE tcc.`checkgroup_id`= #{id}
</select>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值