MyBatis的关联映射

目录

​编辑

文章目录

前言

一、表与表之间的关系

一对一:

一对多:

多对一:

多对多:

二、多对一

1.创建接口MoreMapper

在MoreMapper里面写入方法,通过员工ID来查询员工的信息以及部门的信息,员工表代表多的一方。

2.创建 MoreMapper.xml文件来写Sql语句

在文件中,namespace要写接口的唯一标识,即在包下的位置,然后将方法名复制过来,对应查询的id即可。

3、多对一查询实现的三种方法 

方法一、用表名.属性名

方法一测试类MoreMapperTest

方法二、用association标签

方法三、用分步查询

三、一对多的使用

1.创建接口LessMapper

2、多对一查询实现的两种方法 

方法一、用collection标签

方法二、用分步查询

四、总结


文章目录

  • 一、表与表之间的关系
  • 二、多对一的使用
  • 三、一对多的使用
  • 四、总结

前言

        在实际开发中,根据业务需要,数据表之间往往会存在某种关联关系,例如:一对一,一对多,多对一,多对多等,当程序操作数据库时,如果被操作的表与其他表相关联,那么处理这些表中的数据时必须要考虑它们之间的关联关系。

        为此,MyBatis提供了映射表之间关联关系的功能,如此一来,使用MyBatis能够以简洁的方式操作多张表。


一、表与表之间的关系

一对一:

        在一对一关系中,一方数据表中的一条记录最多可以和另一方数据表中的一条记录相关。例如:人的角度:一个人只能有一张身份证,身份证角度:一张身份证只能属于一个人。

一对多:

        在一对多关系中,主键数据表中的一条记录可以和另外一张数据表的多条记录相关。例如:班级角度:一个班级可以有多名学生。学生角度:一个学生只能属于一个班级。

多对一:

        在多对一关系中,外键数据表的每一个数据可以和另外一张表的一条数据相对应。例如:

球员角度:多名球员属于一个球队。球队角度:一个球队里面的球员是唯一的(与一对多相反)

多对多:

        在多对多关系中,两个数据表里的每一条记录都可以和另外一张表里的每一条记录相关。例如:学生角度:一名学生由多名老师授课,老师角度:一名老师为多个学生授课。

二、多对一

现有emp员工表和dept部门表如下:

1.创建接口MoreMapper

在MoreMapper里面写入方法,通过员工ID来查询员工的信息以及部门的信息,员工表代表多的一方。

package com.mybatis.mapper;

import com.mybatis.pojo.Emp;
import org.apache.ibatis.annotations.Param;

import java.util.List;

public interface MoreMapper {
    
    //通过员工Id来查询员工信息和部门信息
    List<Emp> selectEmpAndDeptByEmpId(@Param("empId") Integer empId);
}

2.创建 MoreMapper.xml文件来写Sql语句

在文件中,namespace要写接口的唯一标识,即在包下的位置,然后将方法名复制过来,对应查询的id即可。

<?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.mybatis.mapper.MoreMapper">
<!--    List<Emp> selectEmpAndDeptByEmpId(@Param("empId") Integer empId);-->
    <select id="selectEmpAndDeptByEmpId" >
        
    </select>

</mapper>

3、多对一查询实现的三种方法 

方法一、用表名.属性名

<?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.mybatis.mapper.MoreMapper">
<!--    List<Emp> selectEmpAndDeptByEmpId(@Param("empId") Integer empId);-->
<!--    方法一-->
    <resultMap id="selectOne" 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>

    <select id="selectEmpAndDeptByEmpId" resultMap="selectOne">
        select *from emp join dept on emp.emp_id = dept.dept_id
        where emp_id = #{empId}
    </select>

</mapper>

运用resultMap来查询多个表,id标签代表查询的主键 ,

id里面的column写数据库的字段名,property写实体类对象的属性名。

方法一使用数据库里面的字段外键,属性名用点表示实体类里面的名字

方法一测试类MoreMapperTest

import com.mybatis.SqlSessionUtil;
import com.mybatis.mapper.MoreMapper;
import com.mybatis.pojo.Emp;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.List;

public class MoreMapperText {
    @Test
    public void textSelectEmpAndDeptByEmpId(){
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        MoreMapper mapper = sqlSession.getMapper(MoreMapper.class);
        List<Emp> emps = mapper.selectEmpAndDeptByEmpId(1);
        System.out.println(emps);
    }
}

测试运行:

方法一测试成功!!!

方法二、用association标签

<?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.mybatis.mapper.MoreMapper">
<!--    List<Emp> selectEmpAndDeptByEmpId(@Param("empId") Integer empId);-->
<!--    方法一-->
    <resultMap id="selectOne" 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>

<!--   方法二  association:处理多对一的映射关系-->
    <resultMap id="selectTwo" 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="dept" javaType="Dept">
            <id column="dept_id" property="deptId"></id>
            <result column="dept_name" property="deptName"></result>
        </association>
    </resultMap>

    <select id="selectEmpAndDeptByEmpId" resultMap="selectTwo">
        select *from emp join dept on emp.emp_id = dept.dept_id
        where emp_id = #{empId}
    </select>


</mapper>

测试运行:

方法二测试成功!!!

方法三、用分步查询

分步查询第一步:

只查询Emp表里面的全部内容

在MoreMapper里面写入分步查询第一步方法

package com.mybatis.mapper;

import com.mybatis.pojo.Emp;
import org.apache.ibatis.annotations.Param;

import java.util.List;

public interface MoreMapper {

    //通过员工Id来查询员工信息和部门信息
    List<Emp> selectEmpAndDeptByEmpId(@Param("empId") Integer empId);

    //分步查询第一步
    List<Emp> selectEmpAndDeptByStepOne(@Param("empId") Integer empId);
}

在MoreMapper.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.mybatis.mapper.MoreMapper">
<!--    List<Emp> selectEmpAndDeptByEmpId(@Param("empId") Integer empId);-->
<!--    方法一-->
    <resultMap id="selectOne" 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>

<!--   方法二  association:处理多对一的映射关系-->
    <resultMap id="selectTwo" 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="dept" javaType="Dept">
            <id column="dept_id" property="deptId"></id>
            <result column="dept_name" property="deptName"></result>
        </association>
    </resultMap>

    <select id="selectEmpAndDeptByEmpId" resultMap="selectTwo">
        select *from emp join dept on emp.emp_id = dept.dept_id
        where emp_id = #{empId}
    </select>

<!--第一步    List<Emp> selectEmpAndDeptByStepOne(@Param("empId") Integer empId);-->
    <resultMap id="MoreMapperOne" 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="dept" 
                     select=""
                     column=""></association>
    </resultMap>
    <select id="selectEmpAndDeptByStepOne" resultMap="">
        select *from emp where emp_id = #{empId}
    </select>



</mapper>

resultMap里面的关键字select表示第二步查询方法的唯一标识,column为查询的条件

开始准备分步查询第二步,创建LessMapper接口写入方法名

package com.mybatis.mapper;

import com.mybatis.pojo.Dept;
import org.apache.ibatis.annotations.Param;

import java.util.List;

public interface LessMapper {
    //多对一分步查询第二步
    List<Dept> selectEmpAndDeptByStepTwo(@Param("deptId") Integer deptId);
    
}

 在LessMapper.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.mybatis.mapper.LessMapper">
<!--    List<Dept> selectEmpAndDeptByStepTwo(@Param("deptId") Integer deptId);-->
    <select id="selectEmpAndDeptByStepTwo" resultType="Dept">
        select *from dept where dept_id = #{dept}
    </select>
</mapper>

完善MoreMapper里面的select和column

<?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.mybatis.mapper.MoreMapper">
<!--    List<Emp> selectEmpAndDeptByEmpId(@Param("empId") Integer empId);-->
<!--    方法一-->
    <resultMap id="selectOne" 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>

<!--   方法二  association:处理多对一的映射关系-->
    <resultMap id="selectTwo" 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="dept" javaType="Dept">
            <id column="dept_id" property="deptId"></id>
            <result column="dept_name" property="deptName"></result>
        </association>
    </resultMap>

    <select id="selectEmpAndDeptByEmpId" resultMap="selectTwo">
        select *from emp join dept on emp.emp_id = dept.dept_id
        where emp_id = #{empId}
    </select>

<!--第一步    List<Emp> selectEmpAndDeptByStepOne(@Param("empId") Integer empId);-->
    <resultMap id="MoreMapperOne" 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="dept"
                     select="com.mybatis.mapper.LessMapper.selectEmpAndDeptByStepTwo"
                     column="dept_id"></association>
    </resultMap>
    <select id="selectEmpAndDeptByStepOne" resultMap="MoreMapperOne">
        select *from emp where emp_id = #{empId}
    </select>



</mapper>

最后开始测试:

 分步查询成功!!!


三、一对多的使用

现在我们把Emp表当成多,Dept表当成一,在LessMapper里面写则可以实现一对多

1.创建接口LessMapper

package com.mybatis.mapper;

import com.mybatis.pojo.Dept;
import org.apache.ibatis.annotations.Param;

import java.util.List;

public interface LessMapper {
    //多对一分步查询第二步
    List<Dept> selectEmpAndDeptByStepTwo(@Param("deptId") Integer deptId);
    
    //一对多查询
    List<Dept> selectEmpAndDeptByDeptId(@Param("deptId") Integer deptId);

}

2、多对一查询实现的两种方法 

方法一、用collection标签

<?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.mybatis.mapper.LessMapper">
<!--    List<Dept> selectEmpAndDeptByStepTwo(@Param("deptId") Integer deptId);-->
    <select id="selectEmpAndDeptByStepTwo" resultType="Dept">
        select *from dept where dept_id = #{dept}
    </select>

<!--  List<Dept> selectEmpAndDeptByDeptId(@Param("deptId") Integer deptId);-->
<!--    一对多查询方法一-->
    <resultMap id="selectOne" type="Dept">
        <id column="dept_id" property="deptId"></id>
        <result column="dept_name" property="deptName"></result>
        <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>
    <select id="selectEmpAndDeptByDeptId" resultMap="selectOne">
        select *from dept join emp on dept.dept_id = emp.dept_id where dept.dept_id = #{deptId}
    </select>
</mapper>

测试结果如下:

方法一测试成功!!!

方法二、用分步查询

分步查询第一步:

只查询Dept表里面的全部内容

在LessMapper里面写入分步查询第一步方法

package com.mybatis.mapper;

import com.mybatis.pojo.Dept;
import org.apache.ibatis.annotations.Param;

import java.util.List;

public interface LessMapper {
    //多对一分步查询第二步
    List<Dept> selectEmpAndDeptByStepTwo(@Param("deptId") Integer deptId);

    //一对多查询
    List<Dept> selectEmpAndDeptByDeptId(@Param("deptId") Integer deptId);
    
    //一对多分步查询第一步
    List<Dept> selectDeptAndEmpByStepOne(@Param("deptId") Integer deptId);

}

在LessMapper.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.mybatis.mapper.LessMapper">
<!--    List<Dept> selectEmpAndDeptByStepTwo(@Param("deptId") Integer deptId);-->
    <select id="selectEmpAndDeptByStepTwo" resultType="Dept">
        select *from dept where dept_id = #{dept}
    </select>

<!--  List<Dept> selectEmpAndDeptByDeptId(@Param("deptId") Integer deptId);-->
<!--    一对多查询方法一-->
    <resultMap id="selectOne" type="Dept">
        <id column="dept_id" property="deptId"></id>
        <result column="dept_name" property="deptName"></result>
        <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>
    <select id="selectEmpAndDeptByDeptId" resultMap="selectOne">
        select *from dept join emp on dept.dept_id = emp.dept_id where dept.dept_id = #{deptId}
    </select>

<!--    List<Dept> selectDeptAndEmpByStepOne(@Param("deptId") Integer deptId);-->
<!--    一对多方法二 第一步-->
    <resultMap id="LessMapperOne" type="Dept">
        <id column="dept_id" property="deptId"></id>
        <result column="dept_name" property="deptName"></result>
        <collection property="emps" 
                    select=""
                    column=""></collection>
    </resultMap>
    <select id="selectDeptAndEmpByStepOne" resultMap="LessMapperOne">
        select *from dept where dept_id = #{deptId}
    </select>
</mapper>

resultMap里面的关键字select表示第二步查询方法的唯一标识,column为查询的条件

开始准备分步查询第二步,创建MoreMapper接口写入方法名

package com.mybatis.mapper;

import com.mybatis.pojo.Emp;
import org.apache.ibatis.annotations.Param;

import java.util.List;

public interface MoreMapper {

    //通过员工Id来查询员工信息和部门信息
    List<Emp> selectEmpAndDeptByEmpId(@Param("empId") Integer empId);

    //多对一分步查询第一步
    List<Emp> selectEmpAndDeptByStepOne(@Param("empId") Integer empId);
    
    //一对多分步查询第二步
    List<Emp> selectDeptAndEmpByStepTwo(@Param("deptId") Integer deptId);
}

 在LessMapper.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.mybatis.mapper.MoreMapper">
<!--    List<Emp> selectEmpAndDeptByEmpId(@Param("empId") Integer empId);-->
<!--    方法一-->
    <resultMap id="selectOne" 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>

<!--   方法二  association:处理多对一的映射关系-->
    <resultMap id="selectTwo" 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="dept" javaType="Dept">
            <id column="dept_id" property="deptId"></id>
            <result column="dept_name" property="deptName"></result>
        </association>
    </resultMap>

    <select id="selectEmpAndDeptByEmpId" resultMap="selectTwo">
        select *from emp join dept on emp.emp_id = dept.dept_id
        where emp_id = #{empId}
    </select>

<!--第一步    List<Emp> selectEmpAndDeptByStepOne(@Param("empId") Integer empId);-->
    <resultMap id="MoreMapperOne" 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="dept"
                     select="com.mybatis.mapper.LessMapper.selectEmpAndDeptByStepTwo"
                     column="dept_id"></association>
    </resultMap>
    <select id="selectEmpAndDeptByStepOne" resultMap="MoreMapperOne">
        select *from emp where emp_id = #{empId}
    </select>

<!--    List<Emp> selectDeptAndEmpByStepTwo(@Param("deptId") Integer deptId);-->
<!--    一对多分步查询第二步-->
    <select id="selectDeptAndEmpByStepTwo" resultType="Emp">
        select * from emp where dept_id = #{deptId}
    </select>

</mapper>

完善LessMapper里面的select和column

<?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.mybatis.mapper.LessMapper">
<!--    List<Dept> selectEmpAndDeptByStepTwo(@Param("deptId") Integer deptId);-->
    <select id="selectEmpAndDeptByStepTwo" resultType="Dept">
        select *from dept where dept_id = #{dept}
    </select>

<!--  List<Dept> selectEmpAndDeptByDeptId(@Param("deptId") Integer deptId);-->
<!--    一对多查询方法一-->
    <resultMap id="selectOne" type="Dept">
        <id column="dept_id" property="deptId"></id>
        <result column="dept_name" property="deptName"></result>
        <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>
    <select id="selectEmpAndDeptByDeptId" resultMap="selectOne">
        select *from dept join emp on dept.dept_id = emp.dept_id where dept.dept_id = #{deptId}
    </select>

<!--    List<Dept> selectDeptAndEmpByStepOne(@Param("deptId") Integer deptId);-->
<!--    一对多方法二 第一步-->
    <resultMap id="LessMapperOne" type="Dept">
        <id column="dept_id" property="deptId"></id>
        <result column="dept_name" property="deptName"></result>
        <collection property="emps"
                    select="com.mybatis.mapper.MoreMapper.selectDeptAndEmpByStepTwo"
                    column="dept_id"></collection>
    </resultMap>
    <select id="selectDeptAndEmpByStepOne" resultMap="LessMapperOne">
        select *from dept where dept_id = #{deptId}
    </select>
</mapper>

开始测试:

分步查询成功!!!


四、总结

创作不易,请大家多多点赞多多支持,努力创造出更好的作品!!!

  • 6
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值