MyBatis中resultMap解决映射关系(多对一、一对多)

一、多对一映射处理

查询员工信息以及员工所对应的部门信息

public class Emp {  
	private Integer eid;  
	private String empName;  
	private Integer age;  
	private String sex;  
	private String email;  
	private Dept dept;
	//...构造器、get、set方法等
}

 1.1  级联方式处理映射关系

<resultMap id="empAndDeptResultMapOne" type="Emp">
	<id property="eid" column="eid"></id>
	<result property="empName" column="emp_name"></result>
	<result property="age" column="age"></result>
	<result property="sex" column="sex"></result>
	<result property="email" column="email"></result>
	<result property="dept.did" column="did"></result>
	<result property="dept.deptName" column="dept_name"></result>
</resultMap>
<!--Emp getEmpAndDept(@Param("eid")Integer eid);-->
<select id="getEmpAndDept" resultMap="empAndDeptResultMapOne">
	select * from t_emp left join t_dept on t_emp.did = t_dept.did where t_emp.eid = #{eid}
</select>

 1.2  使用association处理映射关系

  • association:处理多对一的映射关系
  • property:需要处理多对的映射关系的属性名
  • javaType:该属性的类型(实体类)
<resultMap id="empAndDeptResultMapTwo" type="Emp">
	<id property="eid" column="eid"></id>
	<result property="empName" column="emp_name"></result>
	<result property="age" column="age"></result>
	<result property="sex" column="sex"></result>
	<result property="email" column="email"></result>
	<association property="dept" javaType="Dept">
		<id property="did" column="did"></id>
		<result property="deptName" column="dept_name"></result>
	</association>
</resultMap>
<!--Emp getEmpAndDept(@Param("eid")Integer eid);-->
<select id="getEmpAndDept" resultMap="empAndDeptResultMapTwo">
	select * from t_emp left join t_dept on t_emp.did = t_dept.did where t_emp.eid = #{eid}
</select>

1.3  分步查询

(1) 查询员工信息
  • select:设置分布查询的sql的唯一标识(namespace.SQLId或mapper接口的全类名.方法名)
  • column:设置分步查询的条件
//EmpMapper里的方法
/**
 * 通过分步查询,员工及所对应的部门信息
 * 分步查询第一步:查询员工信息
 * @param  
 * @return com.atguigu.mybatis.pojo.Emp
 */
Emp getEmpAndDeptByStepOne(@Param("eid") Integer eid);
<resultMap id="empAndDeptByStepResultMap" type="Emp">
	<id property="eid" column="eid"></id>
	<result property="empName" column="emp_name"></result>
	<result property="age" column="age"></result>
	<result property="sex" column="sex"></result>
	<result property="email" column="email"></result>
	<association property="dept"
				 select="com.atguigu.mybatis.mapper.DeptMapper.getEmpAndDeptByStepTwo"
				 column="did"></association>
</resultMap>
<!--Emp getEmpAndDeptByStepOne(@Param("eid") Integer eid);-->
<select id="getEmpAndDeptByStepOne" resultMap="empAndDeptByStepResultMap">
	select * from t_emp where eid = #{eid}
</select>
(2)查询部门信息
//DeptMapper里的方法
/**
 * 通过分步查询,员工及所对应的部门信息
 * 分步查询第二步:通过did查询员工对应的部门信息
 * @param
 * @return com.atguigu.mybatis.pojo.Emp
 */
Dept getEmpAndDeptByStepTwo(@Param("did") Integer did);
<!--此处的resultMap仅是处理字段和属性的映射关系-->
<resultMap id="EmpAndDeptByStepTwoResultMap" type="Dept">
	<id property="did" column="did"></id>
	<result property="deptName" column="dept_name"></result>
</resultMap>
<!--Dept getEmpAndDeptByStepTwo(@Param("did") Integer did);-->
<select id="getEmpAndDeptByStepTwo" resultMap="EmpAndDeptByStepTwoResultMap">
	select * from t_dept where did = #{did}
</select>

二、一对多映射处理

查询部门信息以及部门所对应的员工

public class Dept {
    private Integer did;
    private string deptName;
    private List<Emp> emps;
}

2.1  使用collection处理映射关系

  • collection:用来处理一对多的映射关系
  • ofType:表示该属性对应的集合中存储的数据的类型
<resultMap id="DeptAndEmpResultMap" type="Dept">
	<id property="did" column="did"></id>
	<result property="deptName" column="dept_name"></result>
	<collection property="emps" ofType="Emp">
		<id property="eid" column="eid"></id>
		<result property="empName" column="emp_name"></result>
		<result property="age" column="age"></result>
		<result property="sex" column="sex"></result>
		<result property="email" column="email"></result>
	</collection>
</resultMap>
<!--Dept getDeptAndEmp(@Param("did") Integer did);-->
<select id="getDeptAndEmp" resultMap="DeptAndEmpResultMap">
	select * from t_dept left join t_emp on t_dept.did = t_emp.did where t_dept.did = #{did}
</select>

2.2  分步查询

1. 查询部门信息
/**
 * 通过分步查询,查询部门及对应的所有员工信息
 * 分步查询第一步:查询部门信息
 * @param did 
 * @return com.atguigu.mybatis.pojo.Dept
 */
Dept getDeptAndEmpByStepOne(@Param("did") Integer did);
<resultMap id="DeptAndEmpByStepOneResultMap" type="Dept">
	<id property="did" column="did"></id>
	<result property="deptName" column="dept_name"></result>
	<collection property="emps"
				select="com.atguigu.mybatis.mapper.EmpMapper.getDeptAndEmpByStepTwo"
				column="did"></collection>
</resultMap>
<!--Dept getDeptAndEmpByStepOne(@Param("did") Integer did);-->
<select id="getDeptAndEmpByStepOne" resultMap="DeptAndEmpByStepOneResultMap">
	select * from t_dept where did = #{did}
</select>

2. 根据部门id查询部门中的所有员工
/**
 * 通过分步查询,查询部门及对应的所有员工信息
 * 分步查询第二步:根据部门id查询部门中的所有员工
 * @param did
 * @return java.util.List<com.atguigu.mybatis.pojo.Emp>
 */
List<Emp> getDeptAndEmpByStepTwo(@Param("did") Integer did);
<!--List<Emp> getDeptAndEmpByStepTwo(@Param("did") Integer did);-->
<select id="getDeptAndEmpByStepTwo" resultType="Emp">
	select * from t_emp where did = #{did}
</select>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: MybatisResultMap可以用来映射一对多关系。在ResultMap,可以使用collection标签来定义一个集合属性,用来表示一对多关系。在集合属性,可以使用result标签来定义子对象的映射规则。例如: <resultMap id="userMap" type="User"> <id property="id" column="id"/> <result property="name" column="name"/> <collection property="orders" ofType="Order"> <id property="id" column="order_id"/> <result property="name" column="order_name"/> </collection> </resultMap> 在上面的例子,User对象包含一个orders属性,用来表示一个用户可以有多个订单。使用collection标签来定义orders属性,并使用ofType属性来指定子对象的类型为Order。在集合属性,使用id和result标签来定义子对象的映射规则。这样,在查询结果,就可以将多个订单映射到一个User对象的orders属性。 ### 回答2: 在MyBatisResultMap是一种用于将查询到的结果集映射Java对象的机制。当查询结果集包含多个对象的属性时,就需要使用ResultMap一对多映射一对多映射的实现,需要在ResultMap定义一个collection元素来表示将多个对象映射成一个集合。collection元素需要设置property、ofType和select等属性。 property属性表示映射到结果集的查询条件,也就是查询多个对象时需要根据哪个属性进行关联。 ofType属性表示集合元素的类型,这里表示集合元素的类型为哪个Java类。 select属性表示查询的语句,对应于查询结果集的collection列。 例如,以下是一段使用ResultMap实现一对多映射的示例代码: <resultMap id="UserMap" type="User"> <id column="id" property="id"/> <result column="username" property="username"/> <result column="password" property="password"/> <collection property="books" ofType="Book" select="selectBooksByUser" column="id"/> </resultMap> 在上面的代码,UserMap是一个ResultMap配置,books是User类的一个List<Book>类型属性。通过collection元素的配置,表示User对象与多个Book对象之间存在一对多关系。 需要注意的是,以上的示例代码,还需要在mapper文件定义selectBooksByUser语句,用于查询对应的Book对象。同时,需要配置查询语句的查询条件,也就是column属性,与User对象的id属性关联起来。 通过以上的配置,如果查询结果集包含了User对象与多个Book对象,MyBatis就会根据配置进行自动映射,将查询结果集的数据转化为Java对象的形式,方便我们进行业务逻辑的处理。 ### 回答3: MyBatis是一种轻量级的ORM框架,支持复杂的SQL查询。其ResultMapMyBatis非常重要的一种映射规则,可以将查询结果映射Java对象。 在MyBatis,使用ResultMap进行一对多映射时,可以通过以下步骤完成: 1. 定义主实体类和从实体类 在一对多映射,主实体类代表一端,从实体类代表多端。例如,在一个学校的管理系统,Student代表主实体类,Grade代表从实体类。一名学生可以拥有多个成绩单。 2. 在主实体类增加从实体类的集合 在主实体类增加从实体类的集合属性,如下所示: public class Student { private int id; private String name; private List<Grade> grades; } 3. 定义ResultMap 定义ResultMap时,需要使用collection标签来映射从实体类的集合属性。注意,collection标签的属性property应该指向主实体类的集合属性,同时需要指定从实体类的ResultMap,如下所示: <resultMap id="studentResultMap" type="Student"> <id property="id" column="id"/> <result property="name" column="name"/> <collection property="grades" ofType="Grade" resultMap="gradeResultMap"/> </resultMap> 4. 定义从实体类的ResultMap 在从实体类的ResultMap,需要定义每一列的映射关系。同样地,需要定义id标签,指向主实体类的外键列,如下所示: <resultMap id="gradeResultMap" type="Grade"> <id property="id" column="id"/> <result property="subject" column="subject"/> <result property="score" column="score"/> <association property="student" javaType="Student"> <id property="id" column="student_id"/> <result property="name" column="student_name"/> </association> </resultMap> 5. 编写查询语句 最后,编写查询语句时,需要使用select标签,并在其指定ResultMap,如下所示: <select id="getStudent" resultMap="studentResultMap"> SELECT s.id, s.name, g.id, g.subject, g.score, g.student_id, s2.name as student_name FROM student s LEFT JOIN grade g on s.id = g.student_id LEFT JOIN student s2 on g.student_id = s2.id </select> 这样,就完成了从数据库查询学生及其所拥有的成绩单,并将结果映射为Student的对象。其,每个Student对象的grades属性是一个List,其包含多个Grade的对象。 以上就是使用ResultMap实现MyBatis一对多映射的步骤。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值