mybatis之collection实现递归查询级联数据

2022-10-25 15:30:37 浏览数 (2)

mybatis之collection实现递归查询级联数据

数据库结构:

mbatis mapper文件:

代码语言:javascript复制
<!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.xiepanpan.gmall.pms.entity.ProductCategory">
        <id column="id" property="id" />
        <result column="parent_id" property="parentId" />
        <result column="name" property="name" />
        <result column="level" property="level" />
        <result column="product_count" property="productCount" />
        <result column="product_unit" property="productUnit" />
        <result column="nav_status" property="navStatus" />
        <result column="show_status" property="showStatus" />
        <result column="sort" property="sort" />
        <result column="icon" property="icon" />
        <result column="keywords" property="keywords" />
        <result column="description" property="description" />
    </resultMap>

    <resultMap id="ExtendResultMap"
               type="com.xiepanpan.gmall.vo.product.PmsProductCategoryWithChildrenItem"
               extends="BaseResultMap">
        <collection property="children" select="listCategoryWithChildren"
                    column="id"></collection>
        <!--<association property=""-->

    </resultMap>

    <!--缓存-->
    <!--List<PmsProductCategoryWithChildrenItem> listCatelogWithChilder(Integer i);-->
    <select id="listCategoryWithChildren" resultMap="ExtendResultMap">
        select * from pms_product_category where parent_id=#{i}
    </select>

PmsProductCategoryWithChildrenItem 实体类 :

代码语言:javascript复制
import com.xiepanpan.gmall.pms.entity.ProductCategory;
import lombok.Data;

import java.io.Serializable;
import java.util.List;

/**
 */
@Data
public class PmsProductCategoryWithChildrenItem extends ProductCategory  implements Serializable {



    private List<ProductCategory> children;

}

ProductCategory实体类 :

代码语言:javascript复制
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableField;
import java.io.Serializable;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

/**
 * <p>
 * 产品分类
 * </p>
 *
 * @author xiepanpan
 * @since 2019-12-06
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("pms_product_category")
@ApiModel(value="ProductCategory对象", description="产品分类")
public class ProductCategory implements Serializable {

    private static final long serialVersionUID = 1L;

    @TableId(value = "id", type = IdType.AUTO)
    private Long id;

    @ApiModelProperty(value = "上机分类的编号:0表示一级分类")
    @TableField("parent_id")
    private Long parentId;

    @TableField("name")
    private String name;

    @ApiModelProperty(value = "分类级别:0->1级;1->2级")
    @TableField("level")
    private Integer level;

    @TableField("product_count")
    private Integer productCount;

    @TableField("product_unit")
    private String productUnit;

    @ApiModelProperty(value = "是否显示在导航栏:0->不显示;1->显示")
    @TableField("nav_status")
    private Integer navStatus;

    @ApiModelProperty(value = "显示状态:0->不显示;1->显示")
    @TableField("show_status")
    private Integer showStatus;

    @TableField("sort")
    private Integer sort;

    @ApiModelProperty(value = "图标")
    @TableField("icon")
    private String icon;

    @TableField("keywords")
    private String keywords;

    @ApiModelProperty(value = "描述")
    @TableField("description")
    private String description;


}

查询结果:

0 人点赞