首页 > 其他分享 >MyBatis的关联映射

MyBatis的关联映射

时间:2024-03-21 15:01:37浏览次数:16  
标签:mapper 映射 tb sw private 关联 MyBatis com id

资料下载链接

<iframe allowfullscreen="true" data-mediaembed="bilibili" frameborder="0" id="EUZVhLYo-1710997471055" src="https://player.bilibili.com/player.html?aid=1952108328"></iframe>

MyBatis的关联映射- 1V1

<iframe allowfullscreen="true" data-mediaembed="bilibili" frameborder="0" id="8r8auOuv-1710998307413" src="https://player.bilibili.com/player.html?aid=1602218834"></iframe>

MyBatis的关联映射-1Vn

<iframe allowfullscreen="true" data-mediaembed="bilibili" frameborder="0" id="WtqnVGEU-1710999259153" src="https://player.bilibili.com/player.html?aid=1752042231"></iframe>

MyBatis的关联映射-nVn

一、需求分析

        掌握一对一关联映射,掌握一对多关联映射, 掌握多对多关联映射 。

二、搭建环境

1)数据库环境

        mybatis数据库,运行mybatistest04.sql

2)引入依赖

        pom.xml文件

3)数据库连接的配置文件

        src/main/resources,数据库连接的配置文件db.properties。

4)MyBatis核心配置文件

        src/main/resources,MyBatis的核心配置文件mybatis-config.xml

5)新建包和目录

        src/main/java,新建com.sw.pojo包

        src/main/java,新建com.sw.mapper包

        src/main/java,新建com.sw.util包,工具类MyBatisUtils

        src/main/resources,新建com/sw/mapper目录

三、一对一关联映射

        一个人只能有一个身份证,同时一个身份证也只会对应一个人。

1)数据封装类

        com.sw.pojo包,新建IdCard类

public class IdCard {
    private Integer id;
    private String code;
    //get、set
    //tostring
}

        com.sw.pojo包,新建Person类

public class Person {
    private Integer id;             
    private String name;         
    private Integer age;          
    private String sex;            
    private IdCard idCard;
    //get、set
    //tostring
}
2)关联映射--嵌套查询方式

        com.sw.mapper包,新建IdCardMapper接口

    IdCard getOne(int id);

        com/sw/mapper目录,IdCardMapper.xml

    <select id="getOneById" parameterType="int" resultType="IdCard">
        select * from tb_idcard where id = #{id};
    </select>

        com.sw.mapper包,新建PersonMapper接口

    Person getOneByQuery(int id);

        com/sw/mapper目录,PersonMapper.xml

    <select id="getOneByQuery" parameterType="int" resultMap="PersonWithIdCardByQuery">
        select * from tb_person where id = #{id};
    </select>
    <resultMap id="PersonWithIdCardByQuery" type="Person">
        <id property="id" column="id" />
        <result property="name" column="name" />
        <result property="age" column="age" />
        <result property="sex" column="sex" />
        <association property="idCard" column="card_id" javaType="IdCard"
                     select="com.sw.mapper.IdCardMapper.getOne"/>
    </resultMap>
3)关联映射--嵌套结果方式

        com.sw.mapper包,新建PersonMapper接口

    Person getOneByResult(int id);

        com/sw/mapper目录,PersonMapper.xml

    <select id="getOneByResult" parameterType="int" resultMap="PersonWithIdCardByResult">
        select * from tb_person,tb_idcard
        where tb_person.card_id=tb_idcard.id
        and tb_person.id = #{id}
    </select>
    <resultMap id="PersonWithIdCardByResult" type="Person">
        <id property="id" column="id"/>
        <result property="name" column="name" />
        <result property="age" column="age" />
        <result property="sex" column="sex" />
        <association property="idCard" javaType="IdCard">
            <id property="id" column="card_id"/>
            <result property="code" column="code"/>
        </association>
    </resultMap>

四、一对多关联映射

        一个用户可以有多个订单,多个订单也可以归一个用户所有。

1)数据封装类

        com.sw.pojo包,新建Order类

public class Order{
    private Integer id;
    private String number;
    //get、set
    //tostring
}

        com.sw.pojo包,新建User类

public class User{
    private Integer id;
    private String username;
    private String address;
    List<Order> orderList;
    //get、set
    //tostring
}
2)关联映射--嵌套查询方式

        com.sw.mapper包,新建OrderMapper接口。

    List<Order> getList(int userId);

        com/sw/mapper目录,OrderMapper.xml

    <select id="getList" parameterType="int" resultType="Order">
        select * from tb_orders where user_id = #{userId};
    </select>

        com.sw.mapper包,新建UserMapper接口。

    User getOneByQuery(int id);

        com/sw/mapper目录,UserMapper.xml

    <select id="getOneByQuery" parameterType="int" resultMap="UserWithOrderListByQuery">
        SELECT * FROM tb_user WHERE id = #{id}
    </select>
    <resultMap id="UserWithOrderListByQuery" type="User">
        <id property="id" column="id"/>
        <result property="username" column="username"/>
        <result property="address" column="address"/>
        <collection property="orderList" ofType="Order" column="id"
                    select="com.sw.mapper.OrderMapper.getList"/>
    </resultMap>
3)关联映射--嵌套结果方式

        com.sw.mapper包,新建UserMapper接口。

    User getOneByResult(int id);

        com/sw/mapper目录,UserMapper.xml

    <select id="getOneByResult" parameterType="int" resultMap="UserWithOrderListByResult">
        SELECT * FROM tb_user WHERE id = #{id}
    </select>
    <resultMap id="UserWithOrderListByResult" type="User">
        <id property="id" column="id"/>
        <result property="username" column="username"/>
        <result property="address" column="address"/>
        <collection property="orderList" ofType="Order" column="id"
                    select="com.sw.mapper.OrderMapper.getList"/>
    </resultMap>

五、多对多关联映射

        以订单和商品为例,一个订单可以包含多种商品,而一种商品又可以属于多个订单,订单和商品属于多对多关联关系。

1)数据封装类

        com.sw.pojo包,新建Product类

public class Product{
    private Integer id;
    private String name;
    private Double price;
    private List<Order> orderList;
    //get、set
    //tostring
}

        com.sw.pojo包,Order类

public class Order{
    private Integer id;
    private String number;
    private List<Product> productList;
    //get、set
    //tostring
}
2)关联映射--嵌套查询方式

        com.sw.mapper包,新建ProductMapper接口。

    List<Product> getList(int orderId);

        com/sw/mapper目录,ProductMapper.xml

    <select id="getList" parameterType="int" resultType="Product">
        select * from tb_product where id in(
        select product_id from tb_ordersitem where orders_id = #{orderId}
        )
    </select>

        com.sw.mapper包,新建OrderMapper接口。

    Order getOneByQuery(int id);

        com/sw/mapper目录,OrderMapper.xml

    <select id="getOneByResult" parameterType="int" resultMap="UserWithOrderListByResult">
        SELECT *,tb_orders.id as oid FROM tb_user,tb_orders
        WHERE tb_user.id=tb_orders.user_id AND tb_user.id=#{id}
    </select>
    <resultMap id="UserWithOrderListByResult" type="User">
        <id property="id" column="id"/>
        <result property="username" column="username"/>
        <result property="address" column="address"/>
        <collection property="orderList" ofType="Order" >
            <id property="id" column="oid"/>
            <result property="number" column="number"/>
        </collection>
    </resultMap>
3)关联映射--嵌套结果方式

        com.sw.mapper包,新建OrderMapper接口。

    Order getOneByResult(int id);

        com/sw/mapper目录,OrderMapper.xml

    <select id="getOneByResult" parameterType="int" resultMap="OrderWithProductListByResult">
        SELECT * FROM tb_orders,tb_ordersitem,tb_product
        WHERE tb_orders.id = tb_ordersitem.orders_id 
        AND tb_ordersitem.product_id = tb_product.id 
        AND tb_orders.id = #{id}
    </select>
    <resultMap id="OrderWithProductListByResult" type="Order">
        <id property="id" column="id"/>
        <result property="number" column="number"/>
        <collection property="productList" ofType="Product">
            <id property="id" column="product_id"/>
            <result property="name" column="name"/>
            <result property="price" column="price"/>
        </collection>
    </resultMap>

标签:mapper,映射,tb,sw,private,关联,MyBatis,com,id
From: https://blog.csdn.net/u010288901/article/details/136904691

相关文章

  • MyBatis
    缓存一级缓存SqlSession级别,同一会话中多次重复的查询会使用以及缓存,默认开启二级缓存Mappr级别,需要在Mappr显性的开启,不同会话使用同样的Sql会查询二级缓存Mapper.XML文件中常用标签sql脚本相关select、insert、update、delete动态sql相关指定sql的基本数据类型:<result......
  • 一文搞懂idea中的根目录和路径(以Mybatis为例)
    1.根目录概念:1.1项目根目录(ProjectRoot)项目根目录是你在文件系统中为整个项目选择的顶层目录。它通常包含了项目的所有内容,包括源代码、构建配置文件、文档、测试文件等。在版本控制系统中(如Git),项目根目录通常是仓库的根目录。1.2内容根目录(ContentRoot)在IntelliJ......
  • Mybatis批量插入——踩坑点
    最近在进行批量插入,并且返回id的时候,遇到了几个比较抽象的点,首先,是mybatis版本不对,3.3.1之后才支持批量插入返回主键,所以在主键为null的时候,先检查mybatis版本。之后就是检查数据库中的id是否是自增的,如果不是,是否是雪花算法,当然雪花算法就不在本文中详述了。其次,在insert返回主......
  • 2、URL和视图的映射
    fromflaskimportFlask,requestapp=Flask(__name__)#url的组成部分:http[80]/https[443]://www.qq.com:443/path#默认的http协议使用的是80端口,https协议使用的是443端口.#当我们输入www.qq.com时,实际浏览器会处理加上443端口#url与视图:path与视图@app.route("/")d......
  • SpringBoot整合Mybatis(SpringBoot3)
    依赖pom.xml:pom.xml<?xmlversion="1.0"encoding="UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://......
  • Elasticsearch-Mapping映射
    Mapping映射自动或手动为index中的_doc建立一种数据结构和相关配置动态映射:dynamicmapping,自动为我们建立index,以及对应的mapping,mapping中包含了每个field对应的数据类型,以及如何分词等设置。PUT/web_site/_doc/1{"post_date":"2023-01-01","title":"Thelonger",......
  • mybatis一二级缓存简介
    一、前言1,代码和准备工作见:mybatis工作原理简介-seeAll-博客园(cnblogs.com); 二、一级缓存1,效果展示1.1,测试代码代码中,使用SqlSession查询过一次数据;本例在此之后,继续添加一段代码,使用同样的SqlSession再次查询,观察结果数据来自于缓存还是数据库。 1.2,观察第一次......
  • 关联(包含具体实例)
    一、关联是什么?是一种结构关系,说明一个事物的对象与另一个事物的对象相联系。给定有关联的两个类,可以从一个类的对象得到另一个类的对象。关联有两元关系和多元关系。两元关系是指一种一对一的关系,多元关系是一对多或多对一的关系。两个类之间的简单关联表示了两个同等地位......
  • Mybatis设置默认值
    在MyBatis中,可以通过在<resultMap>中使用<result>标签的column属性来设置默认值。但是,MyBatis本身不直接支持在<select>查询中设置默认值。如果需要为查询结果中的某个字段设置默认值,可以在结果映射中处理,或者在应用层面进行处理。以下是一个使用<resultMap>设置默认值的例......
  • SpringBoot整合Mybatis-Plus(SpringBoot3)
    依赖pom.xml:pom.xml<?xmlversion="1.0"encoding="UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://......