Java学习 --- mybatis解决多对一关系查询

本文详细介绍了MyBatis在解决多对一关系映射时的三种方法:级联属性赋值、使用association标签以及分步查询。通过实体类、mapper接口和映射文件的配置,展示了如何在查询员工信息时关联部门信息。
摘要由CSDN通过智能技术生成

目录

一、mybatis解决多对一关系

1.1、方式一:级联属性赋值

 方式二、使用mybatis的association标签

方式三、分步查询

一、mybatis解决多对一关系

1.1、方式一:级联属性赋值

对应的实体类,这里的构造方法,get,set方法,toString方法省略:

public class Emp {
    private Integer id;
    private String empName;
    private Integer age;
    private String sex;
    private String email;
    private Dept dept;
}
public class Dept {
    private Integer did;
    private String deptName;
}

在mapper接口定义方法:

/**
     * 查询员工的信息和对应的方法
     * @param id
     * @return
     */
    Emp selectEmp(@Param("id") Integer id);

在mybatis的mapper映射文件

<!--级联属性赋值-->
    <resultMap id="empResultMap" type="Emp">
        <id property="id" column="id"></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>
    <select id="selectEmp" resultMap="empResultMap">
      select * from t_emp left join t_dept on t_emp.did = t_dept.did where t_emp.id = #{id}
    </select>

测试类:

@Test
    public void test02(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
        Emp emps = mapper.selectEmp(1);
        System.out.println("emps = " + emps);
    }

测试结果:

 方式二、使用mybatis的association标签

其他不变,修改mybatis的mapper映射文件

 <!--
     association:专门处理多对一的关系
     property:处理多对的映射关系的属性名
     javaType:该属性的类型
    -->
    <resultMap id="empResultMap" type="Emp">
        <id property="id" column="id"></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>

方式三、分步查询

第一步:

Emp selectEmpAndDept(@Param("id") Integer id);

修改 Emp的配置mapper映射文件

    <resultMap id="empResultMap" type="Emp">
        <id property="id" column="id"></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.cjc.mybatis.mapper.DeptMapper.selectEmpAndDept"
                     column="did">
        </association>
    </resultMap>
    <select id="selectEmpAndDept" resultMap="empResultMap">
        select * from t_emp where id = #{id}
    </select>

第二步:

Dept selectEmpAndDept(@Param("did") Integer did);

 修改 Dept的配置mapper映射文件

 <resultMap id="resultDept" type="dept">
        <id property="did" column="did"></id>
        <result property="deptName" column="dept_name"></result>
    </resultMap>
    <select id="selectEmpAndDept" resultMap="resultDept">
        select * from t_dept where did = #{did}
    </select>

测试代码:

@Test
    public void test03(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
        Emp emp = mapper.selectEmpAndDept(3);
        System.out.println("emp = " + emp);
    }

测试结果:

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

鸭鸭老板

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值