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了解到此结束!

相关推荐
雨中飘荡的记忆5 小时前
ElasticJob分布式调度从入门到实战
java·后端
考虑考虑14 小时前
JDK25模块导入声明
java·后端·java ee
_小马快跑_15 小时前
Java 的 8 大基本数据类型:为何是不可或缺的设计?
java
Re_zero18 小时前
线上日志被清空?这段仅10行的 IO 代码里竟然藏着3个毒瘤
java·后端
洋洋技术笔记18 小时前
Spring Boot条件注解详解
java·spring boot
程序员清风1 天前
程序员兼职必看:靠谱软件外包平台挑选指南与避坑清单!
java·后端·面试
皮皮林5512 天前
利用闲置 Mac 从零部署 OpenClaw 教程 !
java
华仔啊2 天前
挖到了 1 个 Java 小特性:var,用完就回不去了
java·后端
SimonKing2 天前
SpringBoot整合秘笈:让Mybatis用上Calcite,实现统一SQL查询
java·后端·程序员
日月云棠2 天前
各版本JDK对比:JDK 25 特性详解
java