举一个例子:员工与部门(员工-->部门==多对一)
相关实体类
员工(Emp)
public class Emp {
private Integer id;
private String eName;
private Integer age;
private Dept dept;
}
部门(Dept)
public class Dept {
private Integer dId;
private String deptName;
}
Emp.xml文件编写自定义SQL
方式一:级联属性
<!--处理多对一映射关系方式一:级联属性赋值-->
<!--Emp;员工表 Dept:部门表-->
<!--员工对应部门:多对一 -->
<resultMap id="empAndDeptResultMap" type="Emp">
<!--id为查询的实体类主键id column为数据表id-->
<id property="eid" column="eid"></id>
<!--result为:除主键外,其他属性。-->
<result property="eName" column="e_name"></result>
<result property="age" column="age"></result>
<result property="dept.id" column="did"></result>
<result property="dept.deptName" column="dept_name"></result>
</resultMap>
<select id="getEmpAndDept" resultMap="empAndDeptResultMap">
select * from tbl_employee e inner join tbl_deparment d on e.eid =d.did where e.eid=#{eid}
</select>
方式二: 通过association标签实现
<resultMap id="empAndDeptResultMapTwo" type="com.cc.entity.Emp">
<!--id为查询的实体类主键id column为数据表id-->
<id property="eid" column="eid"></id>
<!--result为:除主键外,其他属性。-->
<result property="eName" column="e_name"></result>
<result property="age" column="age"></result>
<!--association:处理多对一的映射关系
property:需要处理多对一的映射关系的属性
javaType:该属性的类型-->
<association property="dept" javaType="com.cc.entity.Dept">
<id property="dId" column="did"></id>
<result property="deptName" column="dept_name"></result>
</association>
</resultMap>
<select id="getEmpAndDept" resultMap="empAndDeptResultMapTwo">
select * from tbl_employee e inner join tbl_deparment d on e.eid =d.did where e.eid=#{eid}
</select>
方式三:分步查询,先查询员工信息,再根据查询出的员工信息所对应的部门id进行部门信息查询
Emp.xml
<!-- 分步查询-->
<!--1.查询员工信息-->
<resultMap id="empAndDeptByStepResultMap" type="com.cc.entity.Emp">
<id property="eid" column="eid"></id>
<!--result为:除主键外,其他属性。-->
<result property="eName" column="e_name"></result>
<result property="age" column="age"></result>
<!--
association:处理多对一的映射关系
property:需要处理多对一的映射关系的属性
select:namespace+id唯一标识
column="did":由前一个查询结果提供的员工的部门id进行查询
-->
<association property="dept" select="namespace+id" column="did">
<id property="dId" column="did"></id>
<result property="deptName" column="dept_name"></result>
</association>
</resultMap>
<select id="getEmpAndDeptByStep" resultMap="empAndDeptResultMapTwo">
select * from tbl_employee where eid=#{eid}
</select>
Dept.xml
<select id="getEmpAndDeptByStepTow" resultMap="com.cc.entity.Dept">
select * from tbl_dept where did=#{did}
</select>
一对多(部门对应员工)
相关实体类
员工(Emp)
public class Emp {
private Integer id;
private String eName;
private Integer age;
}
部门(Dept)
public class Dept {
private Integer dId;
private String deptName;
private List<Emp> emps;
public List<Emp> getEmps() {
return emps;
}
}
collection
Dept.xml
<!-- 一对多 -->
<!-- collection:用来处理一对多的映射关系 -->
<!-- ofType:表示该属性所对应的集合中存储数据的类型 -->
<resultMap id="deptAndEmpResultMap" type="com.cc.entity.Dept">
<id property="dId" column="did"></id>
<result property="deptName" column="dept_name"></result>
<collection property="emps" ofType="com.cc.entity.Emp">
<id property="id" column="eid"></id>
<result property="eName" column="emp_name"></result>
<result property="age" column="age"></result>
</collection>
</resultMap>
<select id="getDeptAndEmp" resultMap="deptAndEmpResultMap">
select *
from tbl_dept d left join tbl_emp e on d.did=e.did where d.did=#{did};
</select>