mybatis中多对一与一对多关系查询

这篇博客详细介绍了MyBatis中处理一对一和一对多关系映射的几种方法,包括级联方式、association标签、分步查询等,并探讨了延迟加载的配置和优势。同时,提供了具体的XML配置示例,帮助理解不同处理方式的实现细节。
摘要由CSDN通过智能技术生成

首先记住一个理念对一就是对应一个对象,对多就是对应一个集合。

1 多对一关系的处理(三种方法):

1.1 级联方式处理映射关系

<resultMap id="empAndDeptResultMap" type="Emp">
        <id column="emp_id" property="empId"></id>
        <result column="emp_name" property="empName"></result>
        <result column="age" property="age"></result>
        <result column="gender" property="gender"></result>
        <result column="dept_id" property="dept.deptId"></result>
        <result column="dept_name" property="dept.deptName"></result>
    </resultMap>

    <!--Emp getEmpAndDeptByEmpId(@Param("empId") Integer empId);-->
    <select id="getEmpAndDeptByEmpId" resultMap="empAndDeptResultMap">
        select
        t_emp.*,t_dept.*
        from t_emp
        left join t_dept
        on t_emp.dept_id = t_dept.dept_id
        where t_emp.emp_id = #{empId}
    </select>

这里注意resultmap标签最后两行是用来处理emp中没有的两个字段,但是emp中有dept字段,dept中有dept_name和dept_id两个字段,所以用这种方法。

1.2 使用association处理映射关系

<resultMap id="empAndDeptResultMap" type="Emp">
        <id column="emp_id" property="empId"></id>
        <result column="emp_name" property="empName"></result>
        <result column="age" property="age"></result>
        <result column="gender" property="gender"></result>
        <!--
            association:处理多对一的映射关系(处理实体类类型的属性)
            property:设置需要处理映射关系的属性的属性名
            javaType:设置要处理的属性的类型
        -->
        <association property="dept" javaType="Dept">
            <id column="dept_id" property="deptId"></id>
            <result column="dept_name" property="deptName"></result>
        </association>
    </resultMap>

    <!--Emp getEmpAndDeptByEmpId(@Param("empId") Integer empId);-->
    <select id="getEmpAndDeptByEmpId" resultMap="empAndDeptResultMap">
        select
        t_emp.*,t_dept.*
        from t_emp
        left join t_dept
        on t_emp.dept_id = t_dept.dept_id
        where t_emp.emp_id = #{empId}
    </select>

1.3 分步查询

首先查询员工以及对应的部门信息。然后再根据部门信息查询部门名字以及id。

    <resultMap id="empAndDeptByStepResultMap" type="Emp">
        <id column="emp_id" property="empId"></id>
        <result column="emp_name" property="empName"></result>
        <result column="age" property="age"></result>
        <result column="gender" property="gender"></result>
        <!--
            property:设置需要处理映射关系的属性的属性名
            select:设置分步查询的sql的唯一标识
            column:将查询出的某个字段作为分步查询的sql的条件
            fetchType:在开启了延迟加载的环境中,通过该属性设置当前的分步查询是否使用延迟加载
            fetchType="eager(立即加载)|lazy(延迟加载)"
        -->
        <association property="dept" fetchType="eager"
                     select="com.atguigu.mybatis.mapper.DeptMapper.getEmpAndDeptByStepTwo"
                     column="dept_id"></association>
    </resultMap>

    <!--Emp getEmpAndDeptByStepOne(@Param("empId") Integer empId);-->
    <select id="getEmpAndDeptByStepOne" resultMap="empAndDeptByStepResultMap">
        select * from t_emp where emp_id = #{empId}
    </select>

deptmapper.xml中的代码:

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

这个方法在使用时要注意,为了符合规范,dept的方法要重新建立mapper接口,不能在emp中混用。

分步查询的优势:可以实现延迟加载(懒加载)。

使用延迟加载时要进行Mybatis全局配置:

<settings>
        <!--将下划线映射为驼峰-->
        <setting name="mapUnderscoreToCamelCase" value="true"/>
        <!--开启延迟加载-->
        <setting name="lazyLoadingEnabled" value="true"/>
        <!--按需加载-->
        <setting name="aggressiveLazyLoading" value="false"/>
    </settings>

lazyLoadingEnabled:延迟加载的全局开关。当开启时,所有关联对象都会延迟加载
aggressiveLazyLoading:当开启时,任何方法的调用都会加载该对象的所有属性。否则,每个属
性会按需加载。此时就可以实现按需加载,获取的数据是什么,就只会执行相应的sql。此时可通过association和collection中的fetchType属性设置当前的分步查询是否使用延迟加载, fetchType="lazy(延迟加载)|eager(立即加载)"

2 一对多关系的处理(两种方法):

2.1 collection方法

这种方法是一次性全部查出来。

<resultMap id="deptAndEmpResultMap" type="Dept">
        <id column="dept_id" property="deptId"></id>
        <result column="dept_name" property="deptName"></result>
        <!--
            ofType:设置集合类型的属性中存储的数据的类型
            collection:处理一对多的映射关系(处理集合类型的属性)
        -->
        <collection property="emps" ofType="Emp">
            <id column="emp_id" property="empId"></id>
            <result column="emp_name" property="empName"></result>
            <result column="age" property="age"></result>
            <result column="gender" property="gender"></result>
        </collection>
    </resultMap>

    <!--Dept getDeptAndEmpByDeptId(@Param("deptId") Integer deptId);-->
    <select id="getDeptAndEmpByDeptId" resultMap="deptAndEmpResultMap">
        SELECT *
        FROM t_dept
        LEFT JOIN t_emp
        ON t_dept.dept_id = t_emp.dept_id
        WHERE t_dept.dept_id = #{deptId}
    </select>

2.2分步查询

<resultMap id="deptAndEmpResultMapByStep" type="Dept">
        <id column="dept_id" property="deptId"></id>
        <result column="dept_name" property="deptName"></result>
        <collection property="emps"
                    select="com.atguigu.mybatis.mapper.EmpMapper.getDeptAndEmpByStepTwo"
                    column="dept_id"></collection>
    </resultMap>

    <!--Dept getDeptAndEmpByStepOne(@Param("deptId") Integer deptId);-->
    <select id="getDeptAndEmpByStepOne" resultMap="deptAndEmpResultMapByStep">
        select * from t_dept where dept_id = #{deptId}
    </select>

empmapper.xml中的代码: 

<select id="getDeptAndEmpByStepTwo" resultType="Emp">
        select * from t_emp where dept_id = #{deptId}
    </select>

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis是一个Java持久层框架,它可以通过XML或注解将数据库操作映射到Java对象。在MyBatis中进行一对多关联查询有几种方式可以实现。 1. 嵌套查询(Nested Queries):这是一种常见的方式,可以在父查询中调用子查询来获取相关的多个子对象。具体步骤如下: - 在Mapper XML文件中定义两个查询语句,一个用于获取父对象,另一个用于获取子对象列表。 - 在父对象的查询语句中使用"resultMap"属性来指定子对象的映射关系,并在子对象的查询语句中使用"collection"属性来指定父对象的映射关系。 - 在Java代码中调用父对象的查询方法即可。 2. 延迟加载(Lazy Loading):当父查询中包含多个子对象时,可以使用延迟加载的方式来提高查询性能。具体步骤如下: - 在Mapper XML文件中定义两个查询语句,一个用于获取父对象,另一个用于获取子对象列表。 - 在父对象的查询语句中使用"resultMap"属性来指定子对象的映射关系,并在子对象的查询语句中使用"collection"属性来指定父对象的映射关系,并设置"fetchType"为"lazy"。 - 在Java代码中调用父对象的查询方法时,子对象列表将不会立即加载,只有在访问子对象列表时才会触发加载。 3. 嵌套结果映射(Nested Result Maps):在父对象中直接嵌套子对象的方式来进行一对多关联查询。具体步骤如下: - 在Mapper XML文件中定义一个查询语句,同时在"resultMap"中定义父对象的属性和子对象的映射关系。 - 在Java代码中调用查询方法即可,MyBatis会自动将结果映射到父对象的对应属性中,而子对象则会作为父对象属性的集合。 以上是一些常见的在MyBatis中进行一对多关联查询的方法,具体选择哪种方式取决于具体的业务需求和性能要求。希望对你有帮助!如有更多问题,请继续提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值