mybatis里多层一对多集合 对collection的嵌套使用
mybatis里多层一对多集合 对collection的嵌套使用
每一个父级都要有子级的集合以及相应的collection语句
模型:
一个仓库下多个区域,区域下有多个货柜......等等
实体类:
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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!