• Mybatis中表之间的关联关系


    1.关联关系是有方向的.

    1)一对多关联:一个老师可以教多个学生,多个学生只有一个老师来教,站在老师方,就是一对多关联.
    2)多对一关联:一个老师可以教多个学生,多个学生只有一个老师来教,站在学生方,就是多对一关联.
    3)一对一关联:一个老师辅导一个学生,一个学生只请教一个老师.学生和老师是一对一.
    4)多对多关联:园区划线的车位和园区的每一辆车.任意一个车位可以停任意一辆车.任意一车辆车可以停在任意一个车位上.

    2.一对多关联关系

    客户和订单就是典型的一对多关联关系.
    一个客户名下可以有多个订单.
    客户表是一方,订单表是多方.客户一中持有订单的集合.
    使用一对多的关联关系,可以满足查询客户的同时查询该客户名下的所有订单.

    客户表:
    在这里插入图片描述
    订单表:
    在这里插入图片描述
    Customer实体类:

    package com.xin.entity;
    
    import java.util.List;
    
    public class Customer {
        //customer表中的三个列
        private Integer id;
        private  String name;
        private Integer age;
        //该客户名下的所有订单集合
        private List<Orders> ordersList;
    
        public Customer() {
        }
    
        public Customer(Integer id, String name, Integer age, List<Orders> ordersList) {
            this.id = id;
            this.name = name;
            this.age = age;
            this.ordersList = ordersList;
        }
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public Integer getAge() {
            return age;
        }
    
        public void setAge(Integer age) {
            this.age = age;
        }
    
        public List<Orders> getOrdersList() {
            return ordersList;
        }
    
        public void setOrdersList(List<Orders> ordersList) {
            this.ordersList = ordersList;
        }
    
        @Override
        public String toString() {
            return "Customer{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    ", age=" + age +
                    ", ordersList=" + ordersList +
                    '}';
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65

    CustomerMapper接口:

    import com.xin.entity.Customer;
    
    public interface CustomerMapper {
        //根据客户的id查询所有用户信息并同时查询该客户下的所有订单
        Customer getById(Integer id);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    CustomerMapper.xml文件:

    
    DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="com.xin.mapper.CustomerMapper">
        
        <resultMap id="customermap" type="customer">
            
            <id property="id" column="cid">id>
            
            <result property="name" column="name">result>
            <result property="age" column="age">result>
            
            <collection property="ordersList" ofType="orders">
                
                <id property="id" column="oid">id>
                
                <result property="orderNumber" column="orderNumber">result>
                <result property="orderPrice" column="orderPrice">result>
            collection>
        resultMap>
        <select  id="getById" parameterType="int" resultMap="customermap">
            select c.id cid,name,age,o.id oid,orderNumber,orderPrice,customer_id
            from customer c left join orders o on c.id=o.customer_id
            where c.id=#{id}
        select>
    mapper>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43

    测试:

    @Test
        public void testGetById() {
            Customer customer=customerMapper.getById(1);
            System.out.println(customer);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    在这里插入图片描述

    3.多对一关联关系.

    订单和客户就是多对一关联.
    站在订单的方向查询订单的同时将客户信息查出.
    订单是多方,会持有一方的对象.客户是一方.

    Orders1实体类:

    package com.xin.entity;
    
    public class Orders1 {
        public Integer id;
        public String orderNumber;
        public String orderPrice;
        public Customer customer;
    
        public Orders1() {
        }
    
        public Orders1(Integer id, String orderNumber, String orderPrice, Customer customer) {
            this.id = id;
            this.orderNumber = orderNumber;
            this.orderPrice = orderPrice;
            this.customer = customer;
        }
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public String getOrderNumber() {
            return orderNumber;
        }
    
        public void setOrderNumber(String orderNumber) {
            this.orderNumber = orderNumber;
        }
    
        public String getOrderPrice() {
            return orderPrice;
        }
    
        public void setOrderPrice(String orderPrice) {
            this.orderPrice = orderPrice;
        }
    
        public Customer getCustomer() {
            return customer;
        }
    
        public void setCustomer(Customer customer) {
            this.customer = customer;
        }
    
        @Override
        public String toString() {
            return "Orders1{" +
                    "id=" + id +
                    ", orderNumber='" + orderNumber + '\'' +
                    ", orderPrice='" + orderPrice + '\'' +
                    ", customer=" + customer +
                    '}';
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61

    Orders1Mapper接口:

    package com.xin.mapper;
    
    import com.xin.entity.Orders1;
    
    public interface Orders1Mapper {
        Orders1 getById1(Integer id);
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    Orders1Mapper.xml文件

    
    DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="com.xin.mapper.Orders1Mapper">
        
        <resultMap id="orders1map" type="orders1">
            <id property="id" column="oid">id>
            <result property="orderNumber" column="orderNumber">result>
            <result property="orderPrice" column="orderPrice">result>
            <association property="customer" javaType="customer">
                <id property="id" column="cid">id>
                <result property="name" column="name">result>
                <result property="age" column="age">result>
            association>
        resultMap>
        <select id="getById1" parameterType="int" resultMap="orders1map">
            select o.id oid,orderNumber,orderPrice,customer_id,c.id cid,name,age
            from orders o inner join customer c on o.customer_id=c.id
            where o.id=#{id}
        select>
    mapper>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    测试:

    @Test
        public void testGetById1() {
            Orders1 orders1=orders1Mapper.getById1(11);
            System.out.println(orders1);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    在这里插入图片描述

    总结:无论是什么关联关系,如果某方持有另一方的集合,则使用< collection >标签完成映射,如果某方持有另一方的对象,则使用< association >标签完成映射。

  • 相关阅读:
    【C++代码】二叉树的最大深度,二叉树的最小深度,完全二叉树的节点个数--代码随想录
    面向对象继承:ES5继承和ES6继承:extends、super()
    jQuery实现黑暗模式
    RHCE之路网盘搭建
    如何看待unity新的收费模式
    每日一练 | 华为认证真题练习Day122
    Kubernetes 词条总结
    css 10-13
    CH34X linux驱动安装,参考代码例程
    使用百度云服务器申请ssl证书配置报错问题
  • 原文地址:https://blog.csdn.net/m0_56336875/article/details/125879879