MyBatis中表的映射关系

简介: MyBatis中表的映射关系

MyBatis 中表的映射关系



多对多 和 一对一方法相同 ,这里不展开讲 ,主要讲解 一对多 和 多对一


resultMap的作用 : 处理属性和字段之间的映射关系 (设置自定义映射)


属性:

id:表示自定义映射的唯一标识 与select标签中的resultMap内容一致
type:查询的数据要映射的实体类的类型


子标签:

id:设置主键的映射关系
result:设置普通字段的映射关系
association:设置多对一的映射关系
collection:设置一对多的映射关系


属性:

property:设置映射关系中实体类中的属性名
column:设置映射关系中表中的字段名


First : 多对一的映射关系


查询员工以及员工所对应的部门信息 或者查询学生以及学生所对应的班级信息


本文讲解主要以员工类Emp以及部门类Dept为主,对应的数据库表名[ Emp—>t_emp ] [ Dept—> t_dept]


方法一 : 使用级联的方式处理映射关系


<resultMap id="getEmpAndDept" type="Emp">
<!--      这里是处理当前表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>
<!--    这里是处理Dept中的属性-->
        <result column="dept_id" property="dept.deptId"></result>
        <result column="dept_name" property="dept.deptName"></result>
</resultMap>
<!--  接口 :  Emp getEmpAndDeptById(@Param("empId") Integer empId); -->
<select id="getEmpAndDeptById" resultMap="getEmpAndDept">
    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>


注意点: 这里的查询用到了左外连接, 多表查询的一种。


方法二 : 使用association标签: 处理映射关系


<resultMap id="getEmpAndDeptOne" type="Emp">
<!--      这里是处理表t_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 中处理的一定是实体类中的属性 所以Emp中的属性名有一个Dept 类型的dept
            javaType===>是该属性的来源
        -->
<!--      这里是处理t_dept表-->
        <association property="dept" javaType="Dept">
            <id column="dept_id" property="deptId"></id>
            <result column="dept_name" property="deptName"></result>
        </association>
    </resultMap>
<!--    Emp getEmpAndDeptById(@Param("empId") Integer empId);-->
    <select id="getEmpAndDeptById" resultMap="getEmpAndDeptOne">
        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>


方法三 : 最常用的 分布查询


步骤:


首先查询相关的员工信息

根据员工信息中的部门id 从而查询出对应的部门信息


<resultMap id="getEmpOne" type="Emp">
    <!--      这里是处理当前表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>
    <!--这里的dept从哪里来,就是stepTwo(DeptMapper中的)中的查询出来的dept
        property :  设置需要处理映射关系的属性的属性名
        select : 设置分布查询的 sql语句的唯一表示 通过namespace.id  :  获取sql语句的唯一标识
        格式: 模块名.包名.接口名.方法名
        column : 设置分布查询的sql的条件
           也就是作为下一步StepTwo 的sql的条件where XXX = ? 的字段
    -->
    <association property="dept"
                 select="com_Ray.mapper.DeptMapper.getEmpStepTwo"
                 column="dept_id">
    </association>
</resultMap>
<!-- 分布查询的第一步   Emp getEmpByStepOne(@Param("empId") Integer empId);-->
    <select id="getEmpByStepOne" resultMap="getEmpOne">
        select * from t_emp where emp_id = #{empId}
    </select>


此时已经根据员工的id 查询出了相关的员工信息 , 接下来就是根据员工信息中的dept_id字段来查询出对应的部门的信息


再次解释assciation标签中的select属性: 设置分布查询的 sql语句的唯一表示 通过namespace.id获取


namespace 就是mapper标签中的属性 , 而对应的id 就是我们所要执行的接口中的方法名(因为方法名与id的内容要一致)


接下来进行第二步:

<mapper namespace="com_Ray.mapper.DeptMapper">
<!-- 分布查询的第二步   Dept getEmpStepTwo(@Param("deptId") Integer deptId);-->
    <select id="getEmpStepTwo" resultType="Dept">
        select * from t_dept where dept_id = #{deptId}
    </select>


分布查询的优点


可以实现延迟加载,但是必须在核心配置文件中设置全局配置信息

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


Second : 一对多的映射关系


查询部门中所有的员工信息 或者查询班级中所有的学生信息


步骤:


首先在Dept部门类中创建Emp集合


然后再进行查询需要查询的部门id ,再通过部门id查询出部门中所有的员工信息


方法一 : collection


<!--
    collection : 处理多对一的属性
    property : 需要处理的多对一的属性的属性名
    ofType :  需要处理的属性的类型
  注意点  :里面的emps是Emp的集合类型
-->
    <resultMap id="deptResultMap" 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>
    <!--方法名:    Dept getDeptById(@Param("deptId") Integer deptId);-->
    <select id="getDeptById" resultMap="deptResultMap">
        select
            t_dept.* , t_emp.*
        from t_emp right join t_dept
                             on t_emp.dept_id = t_dept.dept_id
        where t_dept.dept_id = #{deptId}
    </select>


方法二 : 分布查询


第一步:查询出需要查询的部门信息


<resultMap id="DeptStepOne" type="Dept">
        <id column="dept_id" property="deptId"></id>
        <result column="dept_name" property="deptName"></result>
<!--
        property :  设置需要处理映射关系的属性的属性名
        select : 设置分布查询的 sql语句的唯一表示
        通过namespace.id  :  获取sql语句的唯一标识
        column : 设置分布查询的sql的条件 作为下一步StepTwo 的sql的条件where XXX = ? 的字段
-->
        <collection property="emps"
                    select="com_Ray.mapper.EmpMapper.getDeptByStepTwo"
                    column="dept_id">
        </collection>
    </resultMap>
<!--    分布查询的第一步 : Emp getDeptByStepOne(@Param("deptId") Integer deptId);-->
    <select id="getDeptByStepOne" resultMap="DeptStepOne">
        select * from t_dept where dept_id = #{deptId}
    </select>


第二步: 根据部门id 查询员工信息

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


注意点!!!

虽然这里我们用到的表中的字段名和所对应的实体类中的属性名不一致 ,但是表设置的字段名使用’_’符合数据库的规则, 而实体类中的属性也同样满足java驼峰命名规范,所以这里可以在核心控制文件中加上下面这段代码 ,这样就可以不用设置resultMap

<settings>
<!--  mapUnderscoreToCamelCase: 将下划线映射为驼峰 例如: user_name==>userName -->
       <setting name="mapUnderscoreToCamelCase" value="true"/>
   </settings>


目录
相关文章
|
3月前
|
XML Oracle Java
mybatis反向生成实体类、dao层以及映射文件
mybatis反向生成实体类、dao层以及映射文件
|
3月前
|
SQL Java 数据库连接
|
3月前
|
Java 数据库连接 Maven
使用mybatis插件generator生成实体类,dao层和mapper映射
使用mybatis插件generator生成实体类,dao层和mapper映射
197 0
|
3月前
|
SQL Java 数据库连接
MyBatis映射文件深入
MyBatis映射文件深入
72 0
|
23天前
|
SQL Java 数据库连接
idea中配置mybatis 映射文件模版及 mybatis plus 自定义sql
idea中配置mybatis 映射文件模版及 mybatis plus 自定义sql
40 3
|
1月前
|
Java 数据库连接 mybatis
mybatis映射关系(1-1 1-n n-n)
Mybatis 中的关系映射包括一对一、一对多和多对多。一对一映射可以通过ResultMap定义属性与字段的对应。一对多示例中,User类包含List&lt;Role&gt;,在User的mapper.xml中使用&lt;collection&gt;标签。多对一的情况,如Author与Blog,Blog的ResultMap中使用&lt;association&gt;来映射Author属性。多对多时,如部门与用户,通常需要中间表,并在双方实体中通过&lt;collection&gt;引用中间类,如UserForDept,来维护关系。
|
2月前
|
SQL XML Java
后端数据库开发JDBC编程Mybatis之用基于XML文件的方式映射SQL语句实操
后端数据库开发JDBC编程Mybatis之用基于XML文件的方式映射SQL语句实操
45 3
|
3月前
|
XML Java 数据库连接
【MyBatis】1、MyBatis 核心配置文件、多表查询、实体映射文件 ......
【MyBatis】1、MyBatis 核心配置文件、多表查询、实体映射文件 ......
110 0
若依修改,集成mybatisplus报错,若依集成mybatisplus,总是找不到映射是怎么回事只要是用mp的方法就找报,改成mybatisPlus配置一定要改
若依修改,集成mybatisplus报错,若依集成mybatisplus,总是找不到映射是怎么回事只要是用mp的方法就找报,改成mybatisPlus配置一定要改
|
3月前
|
算法 BI 数据库
MyBatisPlus查询条件设置、映射匹配兼容性、id生成策略、多数据操作
MyBatisPlus查询条件设置、映射匹配兼容性、id生成策略、多数据操作
101 3