mybatis关联查询,分步查询和延迟加载

关联查询:

1.Employee表:

 id;lastName;email; gender;d_id(外键关联Department的ID)

2.Department表:

 id;deptName;

3。首先,为了关联,Employee(javaBean)如下:

private Integer id;
private String lastName;
private String email;
private String gender;
private Department dept;

Department(javaBean)如下:

private Integer id;
private String deptName;

4.关联三种方式:

场景:查询部员工所在部门

4.1:新建resultMap (resultMap的id是select标签的resultMap名字)

非Employee表的列通过<result column="数据库列名" property="javaBean中该类的名称.类属性"/>

<resultMap type="com.mybatis.bean.Employee" id="myDifEmployee">
<id column="id" property="id"/>
<result column="gender" property="gender"/>
<result column="last_name" property="lastName"/>
<result column="d_id" property="dept.id"/>
<result column="dept_name" property="dept.deptName"/>
</resultMap>

<select id="getEmpAndDept" resultMap="myDifEmployee">
select e.id id, e.last_name last_name, e.gender gender, e.d_id d_id, d.id did, d.dept_name dept_name from tbl_employee as e,tbl_department as d where e.d_id = d.id and e.id=#{id}
</select>

4.2:

<!-- association级联  <association property="javaBean中该类的名称" javaType="类类型地址">-->
<resultMap type="com.mybatis.bean.Employee" id="myDifEmployee2">
<id column="id" property="id"/>
<result column="gender" property="gender"/>
<result column="last_name" property="lastName"/>
<association property="dept" javaType="com.mybatis.bean.Department">
<id column="did" property="id"/>
<result column="dept_name" property="deptName"/>
</association>
</resultMap>

<select id="getEmpAndDept" resultMap="myDifEmployee2">
select e.id id, e.last_name last_name, e.gender gender, e.d_id d_id, d.id did, d.dept_name dept_name from tbl_employee as e,tbl_department as d where e.d_id = d.id and e.id=#{id}
</select>

4.3:分步查询

<!-- association分步
select:表明当前属性是调用select指定方法查出的结果 是XXXMapper.xml中namespace。方法名
column;指定将哪一列的值传给这个方法
-->
<resultMap type="com.mybatis.bean.Employee" id="myEmpByIdStep">
<id column="id" property="id"/>
<result column="gender" property="gender"/>
<result column="last_name" property="lastName"/>
<result column="email" property="email"/>
<association property="dept" select="com.mybatis.dao.DepartmentMapper.getDeptById" column="d_id">
</association>
</resultMap>
<select id="getEmpByIdStep" resultMap="myEmpByIdStep">
select * from tbl_employee where id = #{id}
</select>

5.延迟加载

延迟加载的以上是如果需要的仅仅是tbl_employee 表的值,那么sql语句就不查询tbl_department 。

如果需要tbl_department ,sql语句才加载。

延迟加载的解决方式是在配置文件中的<settings>下加如下两个配置:

<settings>
<!-- 延迟加载的全局开关。当开启时,所有关联对象都会延迟加载。 -->
<setting name="lazyLoadingEnabled" value="true"/>
<!-- 当开启时,任何方法的调用都会加载该对象的所有属性。否则,每个属性会按需加载 -->
<setting name="aggressiveLazyLoading" value="false"/>
</settings>

6.关联查询collection返回集合

场景:查询部门下的所有员工

Department(javaBeab):

private Integer id;
private String deptName;
private List<Employee> emps;

方法1:

<resultMap type="com.mybatis.bean.Department" id="myDept">
<id column="did" property="id"/>
<result column="dept_name" property="deptName"/>
<!--ofType: collection返回值类型 -->
<collection property="emps" ofType="com.mybatis.bean.Employee">
<id column="eId" property="id"/>
<result column="gender" property="gender"/>
<result column="last_name" property="lastName"/>
<result column="email" property="email"/>
</collection>
</resultMap>

<select id="getDeptByIdPlus" resultMap="myDept">
select d.id did, d.dept_name dept_name,e.id eId, e.last_name last_name, e.gender gender, e.email email,e.d_id d_id from tbl_department as d left join tbl_employee as e ON d.id= e.d_id WHERE d.id=#{id}
</select>

方法二:

<resultMap type="com.mybatis.bean.Department" id="myDeptStep">
<id column="id" property="id"/>
<result column="dept_name" property="deptName"/>

<collection property="emps" select="com.mybatis.dao.EmployeeMapper.getEmpByDeptID" column="{deptId=id}" fetchType="lazy">
</collection>


</resultMap>
<select id="getDeptByIdStep" resultMap="myDeptStep">
select id,dept_name deptName from tbl_department where id = #{id}
</select>

==========多列返回值,collection;单个返回值:association==========

collection 将多列的值传递给方法:将多列值封装成map传递;column=“{key1=column1,key2=column2}"

fetchType="lazy" 开启懒加载 fetchType="eager"禁用懒加载;优先级大于全局的懒加载;

resultMap中discriminator标签可以进行鉴别:

场景:如果是女员工把部门信息查出来,如果是男生将last_name这一列赋值给email

<resultMap type="com.mybatis.bean.Employee" id="myEmpByDis">
<id column="id" property="id"/>
<result column="last_name" property="lastName"/>
<result column="email" property="email"/>
<result column="gender" property="gender"/>
<!-- discriminator column:指定判断的列名,javaType:列对应的java类型 -->
<discriminator javaType="string" column="gender">
<case value="0" resultType="com.mybatis.bean.Employee">
<association property="dept" select="com.mybatis.dao.DepartmentMapper.getDeptById" column="d_id">
</association>
</case>
<case value="1" resultType="com.mybatis.bean.Employee">
<id column="id" property="id"/>
<result column="gender" property="gender"/>
<result column="last_name" property="lastName"/>
<result column="last_name" property="email"/>
</case>
</discriminator>
</resultMap>

<select id="getEmpByDiscriminator" resultMap="myEmpByDis">
select * from tbl_employee where id = #{id}
</select>

原文地址:https://www.cnblogs.com/maoyizhimi/p/7698743.html