MyBatis中collection两种常用使用方法
发表于:2022-09-22 11:18:28浏览:508次
MyBatis中的collection两种常用使用方法
collection主要是应对表关系是一对多的情况
查询的时候,用到联表去查询
接下来的小案例包括:市,学校,医院(随便写的),写一个最简单的demo
主要的功能就是查询出所有的市以及对应的市下面所有的学校和医院
实体类:医院
@Data @AllArgsConstructor @NoArgsConstructor public class Hospital { private int id; //医院编号 private int urbanId; //市的编号 private String hospitalName; //医院名称 private Long people; //医院人数 }
实体类:学校
@Data @AllArgsConstructor @NoArgsConstructor public class School { private int id; //学校编号 private int urbanId; //市的编号 private String schoolName; //学校名字 private Long people; //学校人数 }
实体类:市
@Data @AllArgsConstructor @NoArgsConstructor public class Urban { private int id; //市的编号 private String cityId; //省的编号(此博文没用到) private String urbanName; //城市名字 private List<School> schools; //对应的所有的学校 private List<Hospital> hospitals; //对应的所有的医院 }
第一种方式,采用select
首先我们要在学校和医院接口对应的xml中写出按照市的编号来查询出所有数据的xml
xml:医院