mybatis里多层一对多集合 对collection的嵌套使用

mybatis里多层一对多集合 对collection的嵌套使用

每一个父级都要有子级的集合以及相应的collection语句

模型:

image-20211016161041786

一个仓库下多个区域,区域下有多个货柜......等等

实体类:

warehouse里要有area的集合以及自身需要的属性和set,get方法

public class Warehouse {
    //因为不是数据库里有的字段,加上注解
    @TableField(exist = false)
    private List<Area> areas;

    public List<Area> getAreas() {
        return areas;
    }
    public void setAreas(List<Area> areas) {
        this.areas = areas;
    }
}

area里要有container的集合以及自身需要的属性和set,get方法

public class Area {
    
    private String id;
    private String name;
    @TableField(exist = false)
    private List<Container> containers;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<Container> getContainers() {
        return containers;
    }

    public void setContainers(List<Container> containers) {
        this.containers = containers;
    }
}

container里要有cell的集合以及需要的属性和set,get方法

public class Container {

    @TableField(exist = false)
    private List<Cell> cells;

    public List<Cell> getCells() {
        return cells;
    }

    public void setCells(List<Cell> cells) {
        this.cells = cells;
    }
}

mapper.xml

sql语句

<select id="getChildrenCell" resultMap="Warehouse">
        SELECT
        warehouse.id,
        warehouse.`name`,
        area.id as areaId,
        area.`name` as areaName,
        container.id as containerId,
        container.name as containerName,
        cell.id as cellId,
        cell.name as cellName
        FROM
        warehouse warehouse
        LEFT JOIN area area ON warehouse.id = area.houseId
        LEFT JOIN container container on container.areaId =area.id
        left join cell cell on cell.containerId = container.id
        <where>
            cell.id is not null
            ${ew.sqlSegment}
        </where>
    </select>

resultMap

重点collection的嵌套使用部分

<resultMap id="Warehouse" extends="BaseResultMap" type="com.mingxing.entity.Warehouse">
    <!--        property= 的参数是实体类里集合的属性名  areas对应warehouse里的areas集合-->
        <collection property="areas" ofType="com.mingxing.entity.Area" >
            <id column="areaId" property="id"/>
            <result column="areaName" property="name"/>
            <collection property="containers" ofType="com.mingxing.entity.Container">
                <id column="containerId" property="id"/>
                <result column="containerName" property="name"/>
                <collection property="cells" ofType="com.mingxing.entity.Cell">
                    <id column="cellId" property="id"/>
                    <result column="cellName" property="name"/>
                </collection>
            </collection>
        </collection>
    </resultMap>

结果

会自动映射到实体类

直接取出使用

List<Warehouse> warehouses = warehouseService.getChildrenCell(wrapper);
        for (Warehouse warehouse:warehouses){
            System.out.println("warehouse.getName() = " + warehouse.getName());
            List<Area> areas = warehouse.getAreas();
            for (Area area:areas){
                System.out.println("area.getName() = " + area.getName());
                List<Container> containers = area.getContainers();
                for (Container container:containers){
                    System.out.println("container.getName() = " + container.getName());
                    List<Cell> cells = container.getCells();
                    for (Cell cell:cells){
                        System.out.println("cell.getName() = " + cell.getName());
                    }
                }
            }
        }

输出

warehouse.getName() = HOUSE1
area.getName() = AREA1
container.getName() = CON1
cell.getName() = CELL1
cell.getName() = CELL2
cell.getName() = CELL3
container.getName() = CON2
cell.getName() = CELL4
cell.getName() = CELL5
container.getName() = CON3
cell.getName() = cell6
cell.getName() = CELL7
posted @ 2021-10-16 16:43  YuChun_9293  阅读(771)  评论(1编辑  收藏  举报