mybatis一对一关系 association的使用

本文介绍MyBatis中Employee对象的关联查询方法,包括单表查询及与Department表的关联查询,采用不同的resultMap实现方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

废话不多说直接上代码

Employee.java

public class Employee {
    private Integer id;
    private String lastName;
    private String gender;
    private String email;    
    private Department dept;
    
}


接口文件EmployeeMapperPlus.java

public interface EmployeeMapperPlus {
    
    /**
     * 根据id查询Employee对象,使用resultMap
     * @param id
     * @return
     */
    public Employee getEmpById(Integer id);
    /**
     * 根据id查询Employee对象,同时要查询出Deptment对象信息 方式一
     * @param id
     * @return
     */
    public Employee getEmpAndDeptById(Integer id);
    /**
     * 根据id查询Employee对象,同时要查询出Deptment对象信息 方式二
     * @param id
     * @return
     */
    public Employee getEmpAndDeptById2(Integer id);
    
}
Mapper配置文件EmployeeMapperPlus.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.du.mybatis.dao.EmployeeMapperPlus">
    <!-- 
        type:自定义规则的java类型
        id:唯一标识,以便引用
    -->
    <resultMap type="com.du.mybatis.bean.Employee" id="MySimpleEmp">
        <!-- 
                                主键列使用id,其他列使用result
           property java属性字段名
           column 数据库字段名
         -->
        <id property="id" column="id"/>
        <result property="lastName" column="last_name"/>
        <result column="email" property="email"/>
        <result column="gender" property="gender"/>
    </resultMap>
    
    
    
    <!-- public Employee getEmpById(Integer id); -->
    <select id="getEmpById" resultMap="MySimpleEmp">
        select id, last_name, email, gender from tbl_employee where id= #{id}
    </select>
    <!-- 一对一关系写法1 级联属性封装结果集-->
    <resultMap type="com.du.mybatis.bean.Employee" id="MyDifEmp1">
        <id column="id" property="id"/>
        <result property="lastName" column="last_name"/>
        <result column="email" property="email"/>
        <result column="gender" property="gender"/>
        <result property="dept.id" column="dept_id"/>
        <result property="dept.departmentName" column="dept_name"/>
    </resultMap>
    <!-- 一对一关系写法2 使用association -->
    <resultMap type="com.du.mybatis.bean.Employee" id="MyDifEmp2">
        <id column="id" property="id"/>
        <result property="lastName" column="last_name"/>
        <result column="email" property="email"/>
        <result column="gender" property="gender"/>
        <!-- 
            association指定级联的Javabean对象
            property指定该对象的字段名称
            javaType指定对象类型(必填)
         -->
        <association property="dept" javaType="com.du.mybatis.bean.Department">
            <id column="id" property="id"/>
            <result column="dept_name" property="departmentName"/>
        </association>
    </resultMap>
    
    <!-- 
                    查询Employee同时查出所属部门Department
       resultMap:可以选择MyDifEmp1和MyDifEmp2其中一种实现方式
     -->
    <!-- public Employee getEmpAndDeptById(Integer id); -->
    <select id="getEmpAndDeptById" resultMap="MyDifEmp1">
        <!-- 注意查询字段如果不写*,则要把所有需要的字段列出 -->
        SELECT e.id,e.last_name,e.email,e.gender,e.dept_id,d.dept_name FROM tbl_employee e, tbl_dept d
        WHERE e.dept_id=d.id and e.id=#{id}
    </select>
    
    <!-- 一对一关系,实现方式三 -->
    <resultMap type="com.du.mybatis.bean.Employee" id="MyDifEmp3">
        <id column="id" property="id"/>
        <result property="lastName" column="last_name"/>
        <result column="email" property="email"/>
        <result column="gender" property="gender"/>
        <!-- 
                                定义关联对象 
          column:将该字段作为参数传递给select作为入参
        -->
        <association property="dept" column="dept_id" select="com.du.mybatis.dao.DepartmentMapper.getDeptById"></association>
    </resultMap>
    
    <!-- 一对一关系,实现方式四 -->
    <resultMap type="com.du.mybatis.bean.Employee" id="MyDifEmp4">
        <id column="id" property="id"/>
        <result property="lastName" column="last_name"/>
        <result column="email" property="email"/>
        <result column="gender" property="gender"/>
        <!-- 
                                定义关联对象 
          column:将该字段作为参数传递给select作为入参
        -->
        <association property="dept" column="dept_id" select="queryDeptById"></association>
    </resultMap>
    <!-- 
                        分步骤查询:
        1、根据员工号查询员工信息
        2、根据员工信息的dept_id信息,在部门表里查询部门信息
        3、将部门信息放入到员工属性的Department里
                        
     -->
    <!-- public Employee getEmpAndDeptById2(Integer id); -->
    <select id="getEmpAndDeptById2" resultMap="MyDifEmp4">
        select id, last_name, email, gender, dept_id from tbl_employee where id=#{id}
    </select>
    
    <select id="queryDeptById" resultType="com.du.mybatis.bean.Department">
        select id,dept_name departmentName from tbl_dept where id=#{id}
    </select>
    
    <!-- ================================ -->
    <!-- 
        resultMap中有一个discriminator标签,鉴别器:可以使用discriminator判断某列的值,然后根据某列的值改变封装行为
        <discriminator javaType=""></discriminator>
                        例如:Employee
                        如果查出是女生,查出部门信息,
                        如果是男生,不查询部门信息,同时把last_Name这一列赋值给email;
                        参见MyDifEmp5配置
        
     -->
     <resultMap type="com.du.mybatis.bean.Employee" id="MyDifEmp5">
        <id column="id" property="id"/>
        <result property="lastName" column="last_name"/>
        <result column="email" property="email"/>
        <result column="gender" property="gender"/>
        <!-- 
            column:需要判断的列
            javaType:列值对应的java类型
         -->
        <discriminator javaType="string" column="gender">
            <!-- 女生 resultType:指定封装的结果类型 -->
            <case value="0" resultType="com.du.mybatis.bean.Employee">
		        <association property="dept" column="dept_id" select="com.du.mybatis.dao.DepartmentMapper.getDeptById"></association>
            </case>
            <!-- 男生  resultType:指定封装的结果类型 -->
            <case value="1" resultType="com.du.mybatis.bean.Employee">
                <!-- 等于说,重新配置规则 -->
                <id column="id" property="id"/>
                <result column="last_name" property="lastName"/>
                <result column="last_name" property="email"/>
                <result column="gender" property="gender"/>
            </case>
        </discriminator>
        
     </resultMap>
    
    
</mapper>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值