一对多(关联查询):
<!--List<Dept> findAll();-->
<!--一对多查询方法-->
<select id="findAll" resultMap="DeptMap">
select d.*,e.id eid ,e.name ename From dept d join emp e ON d.id=e.dept.id
</select>
<resultMap id="DeptMap" type="Dept">
<!--主键-->
<id column="id" property="id"></id>
<result column="name" property="name" ></result>
<collection ofType="Emp" property="emps">
<id column="eid" property="id"></id>
<result column="ename" property="name" ></result>
</collection>
</resultMap>
一对多(嵌套查询):
(1)DeptMapper中:
<!--List<Dept> findAll();-->
<!--一对多查询方法-->
<select id="findAll" resultMap="DeptMap">
select * From dept
</select>
<resultMap id="DeptMap" type="Dept">
<!--主键-->
<id column="id" property="id"></id>
<result column="name" property="name" ></result>
<collection column="id" property="emps" select="cn.itsource.mapper.EmpMapper.findEmpById">
</collection>
</resultMap>
(2)EmpMapper中:
<!--List<Dept> findAll();-->
<select id="findEmpById" resultType="Emp">
select * From emp where dept_id=#{id}
</select>
多对一(关联查询):
<!--List<Emp> findAll();-->
<!--多对一查询方法-->
<select id="findAll" resultMap="EmpMap">
select e.*,d.name dname From emp e join dept d ON e.dept.id=d.id
</select>
<resultMap id="EmpMap" type="Emp">
<!--主键-->
<id column="id" property="id"></id>
<result column="name" property="name" ></result>
<result column="dept_id" property="dept_id" ></result>
<association javaType="Dept" property="dept">
<result column="dname" property="name" ></result>
</association>
</resultMap>
多对一(嵌套查询):
(1)EmpMapper中:
<!--List<Emp> findAll();-->
<!--多对一查询方法-->
<select id="findAll" resultMap="EmpMap">
select * From emp
</select>
<resultMap id="EmpMap" type="Emp">
<!--主键-->
<id column="id" property="id"></id>
<result column="name" property="name" ></result>
<result column="dept_id" property="dept_id" ></result>
<association column="dept_id" property="dept" select="cn.itsource.mapper.DeptMapper.findDeptById">
</association>
</resultMap>
(2)DeptMapper中:
<select id="findDeptById" resultType="Emp">
select * From emp where id=#{dept_id}
</select>