Mybatis学习笔记:多表关联,懒加载,缓存

人生呐......

文章目录

  • 一、多表关联查询
    • [1.1 定制类](#1.1 定制类)
    • [1.2 成员属性(利用association标签和collection标签](#1.2 成员属性(利用association标签和collection标签)
  • 二、懒加载
    • [2.1 配置](#2.1 配置)
    • [2.2 实现](#2.2 实现)
  • 三、缓存
    • [3.1 一级缓存(session中的缓存](#3.1 一级缓存(session中的缓存)
    • [3.2 二级缓存(Mapper中的缓存](#3.2 二级缓存(Mapper中的缓存)

一、多表关联查询

  1. 确定主表
  2. 确定关联表
  3. 确定链接方式

1.1 定制类

很简单,就是继承一下User类写的UserCustom,这样的话可以保存多表关联的其他类的信息。

sql 复制代码
select * from `user`,orders.`id`,orders.`name` where orders.`user_id` = id;

保存这一块儿的信息需要在UserCustom中额外写上orders的属性,然后用UserCustom去接收数据。

1.2 成员属性(利用association标签和collection标签

我们可以在User类中定义一Orders orders的成员属性。(下面的例子是反着的,即在Orders中定义了一个User user 的成员属性。

xml 复制代码
    <select id="findOrdersUser" resultMap="orderUserResultMap">
        SELECT orders.*,user.`sex`,user.`username`,user.`address`
        FROM orders,USER
        WHERE orders.`user_id` = user.`id`;
    </select>

    <resultMap id="orderUserResultMap" type="entity.Orders">
        <id column="id" property="id"/>

        <result column="user_id" property="userId"/>
        <result column="number" property="number"/>
        <result column="createtime" property="createTime"/>
        <result column="note" property="note"/>
		//property里写属性名 javaType写属性的类型
        <association property="user"  javaType="entity.User">
            <id column="user_id" property="id"/>
            <result column="username" property="username"/>
            <result column="sex" property="sex"/>
            <result column="address" property="address"/>
        </association>
    </resultMap>

类似的,我们可以在用户类下定义一个包含订单类的列表。这时候就要用collection 标签了。(例子给的是一个订单中有多个订单详情

xml 复制代码
	//映射关系也是可以继承的
    <resultMap id="orderUserOrderDetailResultMap" type="entity.Orders" extends="orderUserResultMap">
        <!--Order不用写了-->
        <!--User不用写了-->
        <!--只用写订单详情,然后查询会出现一个订单订了两种物品-->
        //同理,但是注意这里的javaType变成了ofType
        <collection property="orderDetails" ofType="entity.OrderDetail" >
            <id column="orderdetail_id" property="id"/>
            <result column="orders_id" property="ordersId"/>
            <result column="items_id" property="itemsId"/>
            <result column="items_num" property="itemsNum"/>
        </collection>
    </resultMap>
    <select id="findOrderUserOrderDetail" resultMap="orderUserOrderDetailResultMap">
        SELECT orders.*,user.`sex`,user.`username`,user.`address`,
               orderdetail.`id` AS orderdetail_id,
               orderdetail.`items_id`,
               orderdetail.`items_num`,
               orderdetail.`orders_id`
        FROM orders,USER,orderdetail
        WHERE orders.`user_id` = user.`id` AND orderdetail.`orders_id` = orders.`id`;
    </select>

二、懒加载

就是之前上面的都是直接查完了,现在是用到了才查。

2.1 配置

在sqlMapConfig.xml中的settings下添加 懒加载(所有的设置都可以搜到

xml 复制代码
<setting name="lazyLoadingEnabled" value="ture"/>

2.2 实现

实现也很简单,和之前差不多就是在association标签头中添加select和column

xml 复制代码
    <select id="findUserByID" parameterType="int" resultType="entity.User">
        select * from user where id=#{id}
    </select>


    <resultMap id="findOrderAndUserLazyLoadingResultMap" type="entity.Orders">
        <id column="id" property="id"/>

        <result column="user_id" property="userId"/>
        <result column="number" property="number"/>
        <result column="createtime" property="createTime"/>
        <result column="note" property="note"/>
        <!-- select选择可以懒加载的 写好的 之前的select 然后将column中的数据作为输入 -->
        <association  property="user" javaType="entity.User" column="user_id" select="mapper.UserMapper.findUserByID">

        </association>

    </resultMap>

    <select id="findOrderAndUserLazyLoading" resultMap="findOrderAndUserLazyLoadingResultMap">
        select * from orders
    </select>

三、缓存

缓存可以提高效率,

然后在Mybatis中,每次查询都会先去缓存区中找,找不到再去数据库中查询。

每次commit以后就会清空缓存区,防止数据不准确(脏读

3.1 一级缓存(session中的缓存

是保存在会话中的缓存,只在同一个会话下有效,默认开启。

3.2 二级缓存(Mapper中的缓存

是保存在Mapper中的缓存,可以跨会话。需要手动设置开启。

xml 复制代码
       <setting name="cacheEnabled" value="ture"/>

然后在需要开启缓存的Mapper配置文件的命名空间里面配置

xml 复制代码
<mapper namespace="mapper.UserMapper">
    <cache
            eviction="FIFO"
            flushInterval="60000"
            size="512"
            readOnly="true"/>
</mapper>

差不多就没了,Mybatis了解到此结束!

相关推荐
andyweike5 小时前
php笔记
开发语言·笔记·php
EatFan5 小时前
Spring Boot 4 落地观察:从 yudao-cloud、matecloud、JPower 看国产脚手架的升级路线与迁移清单
java·spring boot·后端·spring cloud·微服务·后端开发·jdk 21
Maiko Star5 小时前
* LangChain 提示词模板详解:ChatPromptTemplate 的使用与高级特性
java·人工智能·langchain
xian_wwq6 小时前
【学习笔记】等保合规安全基线核查实战
笔记·学习
鼓掌MVP6 小时前
SQL 语句是一个典型的 DDL
sql·ddl
M78佐菲7 小时前
ARM学习笔记(11)
linux·arm开发·笔记·嵌入式硬件·学习
现任明教教主~7 小时前
Thinkphp站群蜘蛛池SaaS系统YanyvSEO含多用户/积分/六大引擎计费/易支付对接
java·开发语言·spring
M78佐菲7 小时前
ARM学习笔记(10)
linux·arm开发·笔记·嵌入式硬件·学习
李游Leo8 小时前
HarmonyOS 7 + ArkUI + RelationalStore 学习笔记:本地数据分层存储与状态持久化架构实践【鸿蒙心迹】
笔记·学习·架构·harmonyos
一条破秋裤8 小时前
Linux 线程创建:pthread_create 与基本回收
java·linux·运维