分段查询:
问题:查出员工表的信息,以及员工所对应的部门信息
分两段
1.查出员工表的信息
2.查出员工表里部门id所对应具体部门的信息。
Emp getEmpInfo(@Param(“eid”) Integer eid);
下面的dept属性是一个对象,我们需要对这个对象在进行一次查询,查询的内容就是它这张表里的所有信息,根据我们员工表的部门id去查询。
Dept getDeptInfo(@Param("did") Integer did);
测试
开启延迟加载就意味着,你具体查哪个属性,他就会执行哪个属性的具体sql,而不会全部都运行,如果你不开延迟加载,就意味着,你写的sql都会被执行。
一对多如何查询?
一对多,分布查询:
package com.zhang.mapper;
import com.zhang.pojo.Dept;
import org.apache.ibatis.annotations.Param;
public interface DeptMapper {
Dept getDeptInfo(@Param("did") Integer did);
Dept getDeptAndEmp(@Param("did") Integer did);
Dept getDeptANDAllEmp(@Param("did") Integer did);
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface EmpMapper {
List<Emp> getAllEmp();
Emp getEmpInfo(@Param("eid") Integer eid);
List<Emp> getEmpAll(@Param("did") Integer did);
<select id="getDeptANDAllEmp" resultMap="DD">
select * from t_dept where d_id=#{did};
</select>
<resultMap id="DD" type="Dept">
<id property="did" column="d_id"/>
<result property="deptName" column="dept_name"/>
<collection property="emps" select="com.zhang.mapper.EmpMapper.getEmpAll" column="d_id"></collection>
</resultMap>
</mapper>
<resultMap id="AA" type="Emp">
<id property="eid" column="eid"/>
<result property="empName" column="emp_name"/>
<result property="age" column="age"/>
<result property="sex" column="sex"/>
<result property="email" column="email"/>
<association property="dept" select="com.zhang.mapper.EmpMapper.getEmpAll" column="did"/>
</resultMap>
<select id="getEmpAll" resultType="Emp">
select * from t_emp where did = #{did};
</select>
版权声明:本文为weixin_50968313原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。