一对一的关联查询:
1.sql语句使用表连接查询,映射结果使用嵌套的结果集,使用级联操作来映射结果
2.sql语句使用表连接,映射结果使用association
使用多表联查的情况需要单独写一个Vo类,并添加另一个表所在的po类的类名属性,然后再生成设置器以及访问器
<resultMap type="Vo类的类名" id="empWithDept2">
<id column="emp_id" property="id"/>
<result column="emp_name" property="name"/>
<result column="age" property="age"/>
<result column="dept_id" property="deptId"/>
<association property="department" javaType="Department">
<id column="dept_id" property="deptId"/>
<result column="dept_name" property="deptName"/>
</association>
</resultMap>
<select id="selectEmployeeWithDepartment2" resultMap="empWithDept2">
select
e.*, d.dept_id, d.dept_name
FROM
emp_ e, dept_ d
where
e.dept_id = d.dept_id
and
e.emp_id = #{empId}
</select>
具体的还需要参考mybatis7
分步查询:
一对一关联
<resultMap type="Vo类的类名" id="empWithDept3">
<id column="emp_id" property="id"/>
<result column="emp_name" property="name"/>
<result column="emp_email" property="email"/>
<result column="dept_id" property="deptId"/>
<!-- 分步查询, 关联department对象 -->
<!-- 一对一: 使用association -->
<!-- select: 指向了另一条sql语句
column: 是带的参数
-->
<association
select="com.cdsxt.mapper.DepartmentMapper.selectDepartmentById"(具体到另一个mapper的单表查询)
column="{deptId=dept_id}"
property="department" 返回的类型,为vo类中的引用类型
javaType="Department"/>返回类型对应的javabean的类
<!-- column="{ename=emp_name, email=emp_email}" -->
注意:column的值必须与selectDepartmentById中的@param的值一致,否则会导致取不到值
</resultMap>
这一步是把查询出来的对象映射成一个对象,需要注意的时resultMap必须与上面的resultMap中的id相同
<select id="selectEmployeeWithDepartmentByStep" resultMap="empWithDept3">
select * from emp_ where emp_id = #{empId}
</select>
另一个表所在的po类的XXXMapper.xml的写法:
<resultMap type="Department" id="department1">
<id column="dept_id" property="deptId"/>
<result column="dept_name" property="deptName"/>
</resultMap>
<!-- 根据部门id 查询部门信息 -->
<select id="selectDepartmentById" resultMap="department1">
select * from dept_ where dept_id = #{deptId}
需要注意的时占位符中的属性名必须与上面的column传入的key相同
</select>
注意:需要两个联合起来一起看,参考mybatis7-relatedQueryByStep
一对多的关联
需要在vo表中加上一个list的属性
<resultMap type="DepartmentVo" id="departmentWithEmployees">
<id column="dept_id" property="deptId"/>
<result column="dept_name" property="deptName"/>
<!-- 一对多关联: collection -->
<collection property="employees"(vo中的级联类的属性)
column="{deptId = dept_id}"(代表的是需要传过去的表里面的属性)
select="com.cdsxt.mapper.EmployeeMapper.selectEmployeesByDeptId" (具体到另一个类中需要使用对应参数的方法的全限定名+方法名)
ofType="Employee" />
ofType:需要返回的po类型
</resultMap>
<select id="selectDepartmentByIdWithEmployees" resultMap="departmentWithEmployees">
select * from dept_ where dept_id = #{deptId}
</select>
需要得到的表:
<resultMap type="Employee" id="dept1">
<!-- 这里是员工表 -->
<id column="emp_id" property="id"/>
<result column="emp_name" property="name"/>
<result column="emp_email" property="email"/>
<result column="dept_id" property="deptId"/>
</resultMap>
<select id="selectEmployeesByDeptId" resultMap="dept1">
select * from emp_ where dept_id = #{deptId}
</select>
注意:这边获取的占位符中的名字必须与传过来的column中的key相同
1.sql语句使用表连接查询,映射结果使用嵌套的结果集,使用级联操作来映射结果
2.sql语句使用表连接,映射结果使用association
使用多表联查的情况需要单独写一个Vo类,并添加另一个表所在的po类的类名属性,然后再生成设置器以及访问器
<resultMap type="Vo类的类名" id="empWithDept2">
<id column="emp_id" property="id"/>
<result column="emp_name" property="name"/>
<result column="age" property="age"/>
<result column="dept_id" property="deptId"/>
<association property="department" javaType="Department">
<id column="dept_id" property="deptId"/>
<result column="dept_name" property="deptName"/>
</association>
</resultMap>
<select id="selectEmployeeWithDepartment2" resultMap="empWithDept2">
select
e.*, d.dept_id, d.dept_name
FROM
emp_ e, dept_ d
where
e.dept_id = d.dept_id
and
e.emp_id = #{empId}
</select>
具体的还需要参考mybatis7
分步查询:
一对一关联
<resultMap type="Vo类的类名" id="empWithDept3">
<id column="emp_id" property="id"/>
<result column="emp_name" property="name"/>
<result column="emp_email" property="email"/>
<result column="dept_id" property="deptId"/>
<!-- 分步查询, 关联department对象 -->
<!-- 一对一: 使用association -->
<!-- select: 指向了另一条sql语句
column: 是带的参数
-->
<association
select="com.cdsxt.mapper.DepartmentMapper.selectDepartmentById"(具体到另一个mapper的单表查询)
column="{deptId=dept_id}"
property="department" 返回的类型,为vo类中的引用类型
javaType="Department"/>返回类型对应的javabean的类
<!-- column="{ename=emp_name, email=emp_email}" -->
注意:column的值必须与selectDepartmentById中的@param的值一致,否则会导致取不到值
</resultMap>
这一步是把查询出来的对象映射成一个对象,需要注意的时resultMap必须与上面的resultMap中的id相同
<select id="selectEmployeeWithDepartmentByStep" resultMap="empWithDept3">
select * from emp_ where emp_id = #{empId}
</select>
另一个表所在的po类的XXXMapper.xml的写法:
<resultMap type="Department" id="department1">
<id column="dept_id" property="deptId"/>
<result column="dept_name" property="deptName"/>
</resultMap>
<!-- 根据部门id 查询部门信息 -->
<select id="selectDepartmentById" resultMap="department1">
select * from dept_ where dept_id = #{deptId}
需要注意的时占位符中的属性名必须与上面的column传入的key相同
</select>
注意:需要两个联合起来一起看,参考mybatis7-relatedQueryByStep
一对多的关联
需要在vo表中加上一个list的属性
<resultMap type="DepartmentVo" id="departmentWithEmployees">
<id column="dept_id" property="deptId"/>
<result column="dept_name" property="deptName"/>
<!-- 一对多关联: collection -->
<collection property="employees"(vo中的级联类的属性)
column="{deptId = dept_id}"(代表的是需要传过去的表里面的属性)
select="com.cdsxt.mapper.EmployeeMapper.selectEmployeesByDeptId" (具体到另一个类中需要使用对应参数的方法的全限定名+方法名)
ofType="Employee" />
ofType:需要返回的po类型
</resultMap>
<select id="selectDepartmentByIdWithEmployees" resultMap="departmentWithEmployees">
select * from dept_ where dept_id = #{deptId}
</select>
需要得到的表:
<resultMap type="Employee" id="dept1">
<!-- 这里是员工表 -->
<id column="emp_id" property="id"/>
<result column="emp_name" property="name"/>
<result column="emp_email" property="email"/>
<result column="dept_id" property="deptId"/>
</resultMap>
<select id="selectEmployeesByDeptId" resultMap="dept1">
select * from emp_ where dept_id = #{deptId}
</select>
注意:这边获取的占位符中的名字必须与传过来的column中的key相同