MyBatis collection定义关联的集合类型的属性封装规则

mac2026-08-20  2

实体类对象

@Data public class Employee { private Integer id; private String lastName; private Integer gender; private String email; private Department department; } @Data public class Department { private Integer id; private String deptName; /** * 一个部门下有很多个员工 */ private List<Employee> emps; }

查询部门的时候将部门下的所有员工一并查出来

接口中的方法:

Department `selectDeptByIdPlus(Integer id);`

实现方式一:

<!--场景2:查询部门的时候将部门下的所有员工一并查出来--> <!--collection定义关联的集合类型的属性封装规则,ofType:指定集合里边的元素类型--> <!--和association一样,这样的话不管会不会用到部门下的员工信息都会把员工信息查询出来 也可以通过分步的方式,员工集合信息按需加载出来--> <resultMap id="SimpleResultMap" type="com.gf.selfdemo.mybatis.bean.Department"> <id column="d_id" property="id"/> <result column="dept_name" property="deptName"/> <collection property="emps" ofType="com.gf.selfdemo.mybatis.bean.Employee"> <result column="e_id" property="id"/> <result column="last_name" property="lastName"/> <result column="email" property="email"/> <result column="gender" property="gender"/> </collection> </resultMap> <select id="selectDeptByIdPlus" resultMap="SimpleResultMap"> select d.id d_id,d.dept_name,e.id e_id,e.last_name,e.email,e.gender from department d left join employee e on e.dept_id=d.id where d.id=#{id} </select>

实现方式二:

<!--分步查询部门信息和部门下的员工集合信息--> <resultMap id="SimpleResultMap2" type="com.gf.selfdemo.mybatis.bean.Department"> <id column="id" property="id"/> <result column="dept_name" property="deptName"/> <collection property="emps" select="com.gf.selfdemo.mybatis.mapper.EmployeeMapperPlus.getEmpByDeptId" column="id" fetchType="lazy"> </collection> </resultMap> <select id="selectDeptByIdStep" resultMap="SimpleResultMap2"> select * from department where id=#{id} </select>

其中关联的分步sql为:

<select id="getEmpByDeptId" resultType="com.gf.selfdemo.mybatis.bean.Employee"> select * from employee where dept_id=#{deptId} </select>

扩展知识

<!--扩展:分布查询的时候不管是association还是collection, 都可以将步骤一的查询结果作为步骤二的条件,如果步骤二需要传入多个条件,则需要变一下column的写法,封装为map传过去 如:column="{key1=value1,key2=value2,key3=value3....}" 另外,虽然全局开启了延迟加载,也可以在每个sql映射中改变,如:使用fetchType属性 lazy:延迟加载,eager:立即加载-->

discriminator 鉴别器的简单使用

<!--discriminator:鉴别器:mybatis可以使用discriminator判断某列的值,然后根据某列的值改变封装行为 封装Employee: 如果查出来的是女生,就把部门信息查询出来,否则不查询 如果是男生,把last_name这一列的值赋给email--> <resultMap id="ResultMapDis" type="com.gf.selfdemo.mybatis.bean.Employee"> <id column="id" property="id"/> <result column="last_name" property="lastName"/> <result column="email" property="email"/> <result column="gender" property="gender"/> <!--column:需要判定的列名 javaType:列值对应的java类型--> <discriminator javaType="string" column="gender"> <!--女生,部门信息查出来--> <case value="0" resultType="com.gf.selfdemo.mybatis.bean.Employee"> <!--分步查询--> <association property="department" javaType="com.gf.selfdemo.mybatis.bean.Department" column="dept_id"> </association> </case> <!--男生,步查部门信息,把last_name赋值给email--> <case value="1" resultType="com.gf.selfdemo.mybatis.bean.Employee"> <result column="last_name" property="lastName"/> <result column="last_name" property="email"/> <result column="gender" property="gender"/> </case> </discriminator> </resultMap> <select id="selectEmpDis" resultMap="ResultMapDis"> select id,last_name,email,gender dpet_id from employee where id=#{id} </select>
最新回复(0)