MyBatis——表的关联关系,事务,ORM,缓存机制

发布于:2023-01-16 ⋅ 阅读:(144) ⋅ 点赞:(0)

目录

一,一对多的关联关系

二,多对一

 三,一对一

四,多对多关联关系

五,事务

六,缓存机制

使用缓存访问数据库的流程:

缓存的作用域:

七,ORM:对象关系映射


表的关联关系

关联关系是存在方向的

1,一对一

2,一对多

3,多对一

4,多对多

一,一对多的关联关系

创建表:

use ssm;

Create table customer(
id int primary key auto_increment,
name varchar(32),
age int
);

insert into customer values(1,'张三',22);
insert into customer values(2,'李四',23);
insert into customer values(3,'王五',24);

create table orders(
id int primary key auto_increment,
orderNumber varchar(16),
orderPrice double,
customer_id int ,
foreign key (customer_id) references customer(id)
);

insert into orders values(11,20,22.22,1);
insert into orders values(12,60,16.66,1);
insert into orders values(13,90,19.99,2);

select * from customer;
select * from orders;

创建实体类:

public class Customer {
    private Integer id;
    private String name;
    private Integer age;
    //用户订单信息
    private List<Orders> ordersList;
    ...
}

public class Orders {
    private Integer id;
    private String orderNumber;
    private Double orderPrice;

    //因为customer_id是一个外键,本不属于order这个类的属性,所以,不需要创建该属性
    ...
}

创建CustomerMapper接口和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.CustomerMapper">

    <!--
    //根据用户的ID查询用户的ID,和ID下的所有订单信息,返回的是Customer这个对象
    Customer getById(Integer id);

    实体类:
    private Integer id;
    private String name;
    private Integer age;

    //用户订单信息
    private List<Orders> ordersList;


    -->
    <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>
            <!--非主键绑定
                private Integer id;
                private String orderNumber;
                private Double orderPrice;
            -->
            <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>
package com.mybatis.mapper;

import com.mybatis.pojo.Customer;

public interface CustomerMapper {

    //根据用户的ID查询用户的ID,和ID下的所有订单信息,返回的是Customer这个对象
    Customer getById(Integer id);

}

测试:

   @Test
   //一对多关联表查询
    public void testGetCustomerById(){
       Customer customer = customerMapper.getById(1);
       System.out.println(customer);
   }

查询结果:

分析:

 

二,多对一

订单与客户是多对一的关系,站在订单查用户

package com.mybatis.mapper;

import com.mybatis.pojo.Orders;

public interface OrdersMapper {

    //根据订单的编号查询订单信息及下单用户的信息
    Orders getById(Integer id);
}

 

 <!--
    //根据订单的编号查询订单信息及下单用户的信息
    Orders getById(Integer id);

    private Integer id;
    private String orderNumber;
    private Double orderPrice;

    private Customer customer;
    -->
    <resultMap id="ordersMap" type="orders">
        <id property="id" column="oid"></id>

        <result property="orderNumber" column="orderNumber"></result>
        <result property="orderPrice" column="orderPrice"></result>

        <association property="customer" javaType="customer">
            <!--
                private Integer id;
                private String name;
                private Integer age;
            -->
            <id property="id" column="cid"></id>

            <result property="name" column="name"></result>
            <result property="age" column="age"></result>
        </association>
    </resultMap>
    <select id="getById" parameterType="int" resultMap="ordersMap">
        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>
 //多对一关联表查询
    @Test
    public void testGetOrdersById(){
        Orders orders = ordersMapper.getById(11);
        System.out.println(orders);
    }

 三,一对一

一个实体中包含另一个实体的对象既可通过一次查询,得到两个实体的关系

 

四,多对多关联关系

添加一个中间表来化简

 

 

五,事务

一组逻辑操作的最小单元
一致性,持久性,原子性,隔离性

MyBatis中的事务处理

通过transactionManager来管理事务是否是程序自己提交

<transactionManager type="JDBC"></transactionManager>
        sqlSession = factory.openSession();

factory.openSeesion()里面如果是空或者false,表示手动提交,如果true是自动提交。

六,缓存机制

MyBatis框架提供两级缓存,一级缓存和二级缓存,默认开启一级缓存,提高查询数据访问的效率。

使用缓存访问数据库的流程:

 如果数据库中发生了数据变化,会将缓存中的数据全部清空,保证数据的一致性。

缓存的作用域:

一级缓存:使用的SqlSession的作用域,同一个SqlSession共享一级缓存作用域

二级缓存:使用的mapper的缓存作用域,不同的SqlSession只要访问的同一个mapper.xml文件,则共享二级缓存作用域。

七,ORM:对象关系映射

Java中以对象关系操作数据,将数据库中的数据以对象的形式存储,在对象中的数据交换到数据库存储。叫对象关系映射。

本文含有隐藏内容,请 开通VIP 后查看