mybatis多层嵌套查询(多对多)

依赖:

			<dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>mybatis-plus-boot-starter</artifactId>
                <version>3.4.3</version>
            </dependency>

实体类Setmeal:

@Data
@TableName("t_setmeal")
public class Setmeal implements Serializable {
    @TableId(type = IdType.AUTO)
    private Integer id;
    private String name;
    private String code;
    private String helpCode;
    private String sex;//套餐适用性别:0不限 1男 2女
    private String age;//套餐适用年龄
    private Float price;//套餐价格
    private String remark;
    private String attention;
    private String img;//套餐对应图片存储路径
    @TableField(exist = false)
    private List<CheckGroup> checkGroups;//体检套餐对应的检查组,多对多关系
}

实体类CheckGroup:

@Data
@TableName("t_checkgroup")
public class CheckGroup {
    @TableId(type = IdType.AUTO)
    private Integer id;//主键
    private String code;//编码
    private String name;//名称
    private String helpCode;//助记
    private String sex;//适用性别
    private String remark;//介绍
    private String attention;//注意事项
    @TableField(exist = false)
    private List<CheckItem> checkItems;//一个检查组合包含多个检查项
}

实体类CheckItem:

@Data
@TableName("t_checkitem")
public class CheckItem {
    @TableId(type = IdType.AUTO)
    private Integer id;//主键
    private String code;//项目编码
    private String name;//项目名称
    private String sex;//适用性别
    private String age;//适用年龄(范围),例如:20-50
    private Float price;//价格
    private String type;//检查项类型,分为检查和检验两种类型
    private String remark;//项目说明
    private String attention;//注意事项
}

中间表t_setmeal_checkgroup
在这里插入图片描述
中间表t_checkgroup_checkitem
在这里插入图片描述

可以看出Setmeal里面包含多个CheckGroup,而CheckGroup包括多个CheckItem

mapper层

  • CheckItemMapper
/**
     * 根据检查组得到检查项
     * @param checkgroupId
     * @return
     */
    List<CheckItem> findCheckItemById(@Param("checkgroupId") Integer checkgroupId);
CheckItemMapper.xml
<!--根据检查组id查询检查项信息-->
    <select id="findCheckItemById" resultType="com.zhubayi.common.pojo.CheckItem">
        select * from t_checkitem
        where id
        in (select checkitem_id from t_checkgroup_checkitem where checkgroup_id=#{checkgroupId})
    </select>
  • CheckGroupMapper
/**
     * 根据体验套餐的id得到检查项的分组
     * @param setmealId
     * @return
     */
    List<CheckGroup> findCheckGroupBySetmealId(@Param("setmealId") Integer setmealId);
CheckGroupMapper.xml
    <resultMap type="com.zhubayi.common.pojo.CheckGroup" id="baseResultMap">
        <id column="id" property="id"/>
        <result column="name" property="name"/>
        <result column="code" property="code"/>
        <result column="help_code" property="helpCode"/>
        <result column="sex" property="sex"/>
        <result column="remark" property="remark"/>
        <result column="attention" property="attention"/>
    </resultMap>
    
	<resultMap type="com.zhubayi.common.pojo.CheckGroup"
               id="findByIdResultMap"
               extends="baseResultMap">
        <collection property="checkItems"
                    javaType="ArrayList"
                    ofType="com.zhubayi.common.pojo.CheckItem"
                    column="id"
                    select="com.zhubayi.provider.mapper.CheckItemMapper.findCheckItemById">
        </collection>
    </resultMap>
    <!--根据套餐id查询检查项信息-->
    <select id="findCheckGroupBySetmealId" resultMap="findByIdResultMap">
        select * from t_checkgroup
        where id
                  in (select checkgroup_id from t_setmeal_checkgroup where setmeal_id=#{id})
    </select>

column="id"应该是把CheckGroupid当作参数传给findCheckGroupBySetmealId

  • SetmealMapper
/**
     * 根据id查询套餐信息
     * @param id
     * @return
     */
    Setmeal findById(@Param("id") int id);
SetmealMapper.xml
    <resultMap type="com.zhubayi.common.pojo.Setmeal" id="baseResultMap">
        <id column="id" property="id"/>
        <result column="name" property="name"/>
        <result column="code" property="code"/>
        <result column="help_code" 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"/>
    </resultMap>
    <!--column="id"应该就是t_setmeal的id,然后传过去-->
    <resultMap type="com.zhubayi.common.pojo.Setmeal"
               id="findByIdResultMap"
               extends="baseResultMap">
        <collection property="checkGroups"
                    javaType="ArrayList"
                    ofType="com.zhubayi.common.pojo.CheckGroup"
                    column="id"
                    select="com.zhubayi.provider.mapper.CheckGroupMapper.findCheckGroupBySetmealId">
        </collection>
    </resultMap>
    <select id="findById" resultMap="findByIdResultMap">
        select * from t_setmeal  where id=#{id}
    </select>

测试
在这里插入图片描述
一个setmeal里面有多个checkGroupcheckGroup里面有多个checkItems

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果你需要在 MyBatis 中进行多层嵌套的插入操作,可以使用 MyBatis 的一些特性,例如嵌套查询和级联插入。下面是一个示例: 首先,定义好插入的实体类和对应的映射文件。假设我们要插入一个 `Order` 对象,它包含多个 `Item` 对象,每个 `Item` 对象又包含多个 `Product` 对象。 Order 实体类: ```java public class Order { private Long id; private List<Item> items; // getters and setters } public class Item { private Long id; private List<Product> products; // getters and setters } public class Product { private Long id; private String name; // getters and setters } ``` Order 映射文件: ```xml <insert id="insertOrder" parameterType="Order"> INSERT INTO orders (id) VALUES (#{id}) <foreach collection="items" item="item" index="index" separator=";"> INSERT INTO items (id, order_id) VALUES (#{item.id}, #{id}) <foreach collection="item.products" item="product" index="index" separator=";"> INSERT INTO products (id, item_id, name) VALUES (#{product.id}, #{item.id}, #{product.name}) </foreach> </foreach> </insert> ``` 在这个映射文件中,我们使用了 `<foreach>` 标签来遍历 Order 对象中的 items 和每个 item 中的 products。注意在嵌套的 `<foreach>` 标签中,需要使用 item 对象的属性来作为参数,例如 `#{item.id}` 和 `#{item.products}`。 然后,在代码中调用这个插入操作: ```java Order order = new Order(); order.setId(1L); Item item1 = new Item(); item1.setId(1L); Product product1 = new Product(); product1.setId(1L); product1.setName("Product 1"); Product product2 = new Product(); product2.setId(2L); product2.setName("Product 2"); item1.setProducts(Arrays.asList(product1, product2)); Item item2 = new Item(); item2.setId(2L); Product product3 = new Product(); product3.setId(3L); product3.setName("Product 3"); item2.setProducts(Collections.singletonList(product3)); order.setItems(Arrays.asList(item1, item2)); orderMapper.insertOrder(order); ``` 在这个示例中,我们创建了一个包含两个 Item 对象的 Order 对象,并且每个 Item 对象包含一个或多个 Product 对象。然后,我们调用了 `orderMapper.insertOrder(order)` 方法来插入这个 Order 对象到数据库中。 在实际使用中,你需要根据自己的数据结构和需求来修改上面的示例。希望对你有所帮助!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值