码云MybatisDemo: 用来学习springboot整合mybatis (gitee.com)
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; //对应的所有的医院
}
首先我们要在学校和医院接口对应的xml中写出按照市的编号
来查询出所有数据的xml
xml:医院
DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.yh.mybatis.dao.mapper.HospitalMapper">
<select id="findAllByUId" resultType="com.yh.mybatis.dao.pojo.Hospital">
select * from hospital where urban_id = #{urbanId}
select>
mapper>
xml:学校
DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.yh.mybatis.dao.mapper.SchoolMapper">
<select id="urbanSchool" resultType="com.yh.mybatis.dao.pojo.School">
select * from school where urban_id = #{urbanId}
select>
mapper>
接下来就是在市
的xml中对学校和医院的xml进行一个调用(用collection中select)
DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.yh.mybatis.dao.mapper.UrbanMapper">
<resultMap id="findAllUrbanSandH" type="com.yh.mybatis.dao.pojo.Urban">
<collection property="schools" javaType="java.util.List" ofType="com.yh.mybatis.dao.pojo.School"
select="com.yh.mybatis.dao.mapper.SchoolMapper.urbanSchool"
column="{urbanId=id}">
collection>
<collection property="hospitals" javaType="java.util.List" ofType="com.yh.mybatis.dao.pojo.Hospital"
select="com.yh.mybatis.dao.mapper.HospitalMapper.findAllByUId"
column="{urbanId=id}">
collection>
resultMap>
<select id="findAllUrbanSandH" resultMap="findAllUrbanSandH">
select * from urban
select>
mapper>
DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.yh.mybatis.dao.mapper.UrbanMapper">
<resultMap id="findAllUrbanSandH2" type="com.yh.mybatis.dao.pojo.Urban">
<id property="id" column="id"/>
<result property="cityId" column="city_id"/>
<result property="urbanName" column="urban_name"/>
<collection property="schools" javaType="java.util.List" ofType="com.yh.mybatis.dao.pojo.School">
<id property="id" column="sid"/>
<result property="urbanId" column="surban_id"/>
<result property="schoolName" column="school_name"/>
<result property="people" column="speople"/>
collection>
<collection property="hospitals" javaType="java.util.List" ofType="com.yh.mybatis.dao.pojo.Hospital">
<id property="id" column="hid"/>
<result property="urbanId" column="hurban_id"/>
<result property="hospitalName" column="hospital_name"/>
<result property="people" column="hpeople"/>
collection>
resultMap>
<select id="findAllUrbanSandH2" resultMap="findAllUrbanSandH2">
select urban.city_id
,urban.id
,urban.urban_name
,school.id sid
,school.urban_id surban_id
,school.school_name
,school.people speople
,hospital.id hid
,hospital.urban_id hurban_id
,hospital.hospital_name
,hospital.people hpeople
from urban
inner join school on urban.id = school.urban_id
inner join hospital on urban.id = hospital.urban_id
select>
mapper>
接下来就可以写两个接口来测试这两个xml配置是否正确,具体的代码在最上面的码云地址里,大家可以配合swagger进行测试。提供一个springboot整合swagger3的一个小教程springboot整合swagger3