mybatis XML映射器-多表关联映射

101 阅读1分钟
package com.kdy.pojo;

public class EmpDeptDTO extends Emp{
    private Dept dept;

    public Dept getDept() {
        return dept;
    }

    public void setDept(Dept dept) {
        this.dept = dept;
    }
    

    @Override
    public String toString() {
        return super.toString()+"EmpDeptDTO{" +
                "dept=" + dept +
                '}'+"\n";
    }
}
<resultMap id="EmpAndDeptMap" type="com.kdy.pojo.EmpDeptDTO">
    <id column="id" property="id"></id>
    <result column="username" property="username"></result>
    <result column="create_date" property="create_date"></result>
    <result column="dept_id" property="dept_id"></result>
    <result column="dept_name" property="dept.dept_name"></result>
    <result column="dept_id" property="dept.id"></result>


</resultMap>



<!--    -->
<select id="selectEmpAndDept"  resultMap="EmpAndDeptMap">
    select emp.*,dept.dept_name  from  emp left JOIN dept on  emp.dept_id = dept.id
</select>

一对多映射 association

<!--
    association 映射多对一点查询
    property 指定 “一”
    javaType 类型
    resultMap 调用已存在的映射 重用
    强行使 结果映射为多对一   底层会把ID  和  值 拿出来组成Map
    没有ID时  会全部映射

    columnPrefix="dept_"  会为每个字段去掉指定前缀

    当列名与指定不一致时  在里面重新定义 要加javaType


-->
<!--        <association property="dept" javaType="com.kdy.pojo.Dept">-->
<!--            <id property="id" column="dept_id"></id>-->
<!--            <result property="dept_name" column="dept_name"></result>-->
<!--        </association>-->

        <association property="dept"  resultMap="com.kdy.mapper.DeptMapper.BaseResultMap" >

        </association>

多对一映射

<resultMap id="BaseResultMap" type="com.kdy.pojo.Dept">
    <id property="id" column="dept_id"></id>
    <result property="dept_name" column="dept_name"></result>
</resultMap>
<resultMap id="DeptAndEmp" extends="BaseResultMap" type="com.kdy.pojo.DeptEmpDTO">
    <!--
       collection 指定多对一
        ofType  自定义映射
        和association一样 根据底层ID组织的数据


     -->
    <collection property="emps" ofType="com.kdy.pojo.Emp">
        <id property="id" column="id"></id>
        <result property="username" column="username"></result>
        <result property="create_date" column="create_date"></result>
    </collection>
</resultMap>
<select id="selectDeptAndEmp" resultMap="DeptAndEmp">
    select dept.dept_name,emp.*  from dept LEFT JOIN emp on dept.id = emp.dept_id
</select>

嵌套查询方式

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--致力于减少使用成本  让用户专注SQL文件-->
<!--
 mapper 根节点
 namespace  一般情况:一个mapper对应一个不同的命名空间
 如果使用接口绑定的方式  必须输入对应接口的完整限定名


嵌套查询(也叫分布查询)

-->
<mapper namespace="com.kdy.mapper.DeptMapper">
    <resultMap id="BaseResultMap" type="com.kdy.pojo.Dept">
        <id property="id" column="dept_id"></id>
        <result property="dept_name" column="dept_name"></result>
    </resultMap>
     
    <!--
    嵌套查询使用更麻烦

            部门分页   嵌套查询更合适
            用户分页  多对一更合适
            嵌套查询支持懒加载

         -->
    <resultMap id="DeptAndEmp2" extends="BaseResultMap" type="com.kdy.pojo.DeptEmpDTO">
        <collection property="emps" column="dept_id" select="com.kdy.mapper.EmpMapper.selectEmp" ofType="com.kdy.pojo.Emp">

        </collection>
    </resultMap>
     

    <select id="selectDeptAndEmp2" resultMap="DeptAndEmp2">
        select id as dept_id,dept_name  from dept
    </select>

</mapper>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
 



<mapper namespace="com.kdy.mapper.EmpMapper">
    
    
    <resultMap id="BaseResultMap" type="com.kdy.pojo.Emp">
        <id column="id" property="id"></id>
        <result column="username" property="username"></result>
        <result column="create_date" property="create_date"></result>
        <result column="dept_id" property="dept_id"></result>
    </resultMap>
    
   
    <select id="selectEmp" resultMap="BaseResultMap">
        select *  from emp where dept_id= #{dept_id}
    </select>
</mapper>
    @Test
    public void Test02(){
        DeptMapper mapper = sqlSession.getMapper(DeptMapper.class);
//        List<Dept> depts = mapper.selectDeptAndEmp();
//        System.out.println(depts.size());
//        System.out.println(depts);
        List<Dept> depts = mapper.selectDeptAndEmp2();
        System.out.println(depts.size());
        System.out.println(depts);
    }

嵌套查询的懒加载

一、mybatis config 文件settting 设置为 lazyLoadingEnabled true

<setting name="lazyLoadingEnabled" value="true"/>

如果不使用懒加载

<collection property="emps" fetchType="eager"

二、

直接将嵌套查询的 fetchType设置为懒加载

<collection property="emps" fetchType="lazy" column="dept_id" select="com.kdy.mapper.EmpMapper.selectEmp" ofType="com.kdy.pojo.Emp">

</collection>

方便演示将mybatis的配置文件lazyLoadTriggerMethod设置为空

<setting name="lazyLoadTriggerMethod" value=""/>