【MyBatis】三、ResultMap与多表查询的处理

ResultMap与多表查询的处理

当字段名与实类名不一致时

使用别名进行处理

字段名:emp_name

实体类名:empName

映射文件中写法:

    <select id="getAllEmp" resultType="Emp">
        select eid, emp_name empName, age, sex, email, did from t_emp
    </select>

使用全局配置将下划线命名映射为驼峰

在mybatis-config.xml文件的properties标签和typeAlias标签之间添加settings标签如下,可以将下划线式命名映射为驼峰:

    <settings>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>

使用resultMap创建自定义的映射关系

在mapper.xml文件中进行定义:

  • 定义resultMap:

        <!-- 就算是自定义映射关系,也需要相对应的实体类 -->
        <resultMap id="empResultMap" type="Emp">
            <!-- id用来声明主键,property用来表示实体类中的属性名、column用来标识数据库表中的字段名 -->
            <id property="eid" column="eid"></id>
            <!-- result用来声明普通字段 -->
            <result property="empName" column="emp_name"></result>
            <result property="age" column="age"></result>
            <result property="sex" column="sex"></result>
            <result property="email" column="email"></result>
        </resultMap>
    
  • 传入resultMap的id以用来使用自定义映射

        <select id="getAllEmp" resultMap="empResultMap">
            select * from t_emp
        </select>
    

注意:resultMap一般用于处理多对一、一对多的关系

一对多情况的处理

对于多对一的情况(一个员工只会在一个部门中)

只需要在员工实体类中添加一个部门属性:

    private Integer eid;

    private String empName;

    private Integer age;

    private String sex;

    private String email;

    private Integer did;

    private Dept dept;

通过级联属性赋值resultMap解决多对一问题

在mapper.xml文件中创建resultMap:

    <resultMap id="empAndDeptResultMap" type="Emp">
        <id property="eid" column="eid"></id>
        <result property="empName" column="emp_name"></result>
        <result property="age" column="age"></result>
        <result property="sex" column="sex"></result>
        <result property="email" column="email"></result>
        <result property="dept.did" column="did"></result>
        <result property="dept.deptName" column="dept_name"></result>
    </resultMap>

再将这个传入语句进行调用

<!--    Emp getEmpAndDept(@Param("eid") Integer eid);-->
    <select id="getEmpAndDept" resultMap="empAndDeptResultMap">
        select *
        from t_emp left join t_dept on t_emp.did = t_dept.did
        where eid=#{eid}
    </select>

级联属性赋值的方式一般不使用

使用association解决多对一问题

在mapper.xml文件中创建resultMap:

    <resultMap id="empAndDeptResultMapAsscoiation" type="Emp">
        <id property="eid" column="eid"></id>
        <result property="empName" column="emp_name"></result>
        <result property="age" column="age"></result>
        <result property="sex" column="sex"></result>
        <result property="email" column="email"></result>
        <association property="dept" javaType="Dept">
            <id property="did" column="did"></id>
            <result property="deptName" column="dept_name"></result>
        </association>
    </resultMap>

再将之传入即可

通过分步查询解决多对一问题

在mapper.xml建立如下:

<!--    注意在分步查询的过程中,association中的column代表传入的条件-->
    <resultMap id="empAndDeptByStepResultMap" type="emp">
        <id property="eid" column="eid"></id>
        <result property="empName" column="emp_name"></result>
        <result property="age" column="age"></result>
        <result property="sex" column="sex"></result>
        <result property="email" column="email"></result>
        <association property="dept"
                     select="com.qinghe.mybatis.mapper.DeptMapper.getEmpAndDeptByStepTwo"
                     column="did"></association>
    </resultMap>

Dept的mapper如下:

<!--    Dept getEmpAndDeptByStepTwo(@Param("did") Integer did);-->
    <select id="getEmpAndDeptByStepTwo" resultType="Dept">
        select * from t_dept where did = #{did}
    </select>

调用:

    <select id="getEmpAndDeptByStepOne" resultMap="empAndDeptByStepResultMap">
        select * from t_emp where eid = #{eid}
    </select>

分布查询的优点:延迟加载

当mybatis的查询语句由多步查询构成时,我们可以开启mybatis的懒加载,此时若我们只需要第一步的某个属性,mybatis就不会去调用第二步的sql语句,这样在多种场景下最大限度的保证了性能。

  • 在全局配置(mybatis-config.xml)中添加如下配置

    <!--    全局配置:自动将下划线转为驼峰,懒加载-->
        <settings>
            <setting name="mapUnderscoreToCamelCase" value="true"/>
            <setting name="lazyLoadingEnabled" value="true"></setting>
        </settings>
    

同时,我们也可以在association中使用fetchType标签来使延迟加载变得可控,eager代表立即加载、lazy代表延迟加载

<!--    注意在分步查询的过程中,association中的column代表传入的条件-->
    <resultMap id="empAndDeptByStepResultMap" type="emp">
        <id property="eid" column="eid"></id>
        <result property="empName" column="emp_name"></result>
        <result property="age" column="age"></result>
        <result property="sex" column="sex"></result>
        <result property="email" column="email"></result>
        <association property="dept"
                     select="com.qinghe.mybatis.mapper.DeptMapper.getEmpAndDeptByStepTwo"
                     column="did"
                     fetchType="eager">
        </association>
    </resultMap>

多对一情况的处理

在dept实体类中添加多的的那个List:

    private List<Emp> empList;

使用collection标签进行一口气的处理

在deptMapper下进行如下操作:

    <resultMap id="deptAndEmpResultMap" type="Dept">
        <id property="did" column="did"></id>
        <result property="deptName" column="dept_name"></result>
<!--        这里的ofType代表传入的List的泛型-->
        <collection property="empList" ofType="Emp">
            <id property="eid" column="eid"></id>
            <result property="empName" column="emp_name"></result>
            <result property="age" column="age"></result>
            <result property="sex" column="sex"></result>
            <result property="email" column="email"></result>
        </collection>
    </resultMap>

    <select id="getDeptAndEmp" resultMap="deptAndEmpResultMap">
        select *
        from t_emp left join t_dept on t_emp.did = t_dept.did
        where t_dept.did=#{did}
    </select>

通过分步查询进行处理

在DeptMapper中建立第一步的接口:

    /**
     * 通过分步查询部门以及部门中的员工信息
     */
    Dept getDeptAndEmpByStepOne(@Param("did") Integer did);

在EmpMapper中建立第二步的接口:

    /**
     * 分步查询第二步,根据did查询员工信息
     */
    List<Emp> getDeptAndEmpByStepTwo(@Param("did") Integer did);

在EmpMapper.xml文件中定义xml:

<!--    List<Emp> getDeptAndEmpByStepTwo(@Param("did") Integer did);-->
    <select id="getDeptAndEmpByStepTwo" resultType="Emp">
        select * from t_emp
    </select>

在DeptMapper.xml文件中定义xml:

    <resultMap id="deptAndEmpByStepResultMap" type="Dept">
        <id property="did" column="did"></id>
        <result property="deptName" column="dept_name"></result>
        <collection property="empList"
                    select="com.qinghe.mybatis.mapper.EmpMapper.getDeptAndEmpByStepTwo"
                    column="did"
        ></collection>
    </resultMap>

    <select id="getDeptAndEmpByStepOne" resultMap="deptAndEmpByStepResultMap">
        select * from t_dept where did = #{did}
    </select>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值