【MyBatis】day03动态SQL和缓存机制

简介: 【MyBatis】day03动态SQL和缓存机制

第九章 Mybatis动态SQL【重点】

SQL中注释

//方式一

-- 1=1

//方式二【推荐使用】

<!-- 1=1 -->

9.1 动态SQL概述

  • 动态SQL指的是:SQL语句可动态化
  • Mybatis的动态SQL中支持OGNL表达式语言,OGNL( Object Graph Navigation Language )对象图导航语言

9.2 常用标签

  • if标签:用于完成简单的判断
  • where标签:用于解决where关键字及where后第一个and或or的问题
  • trim标签: 可以在条件判断完的SQL语句前后添加或者去掉指定的字符
  • prefix: 添加前缀
  • prefixOverrides: 去掉前缀
  • suffix: 添加后缀
  • suffixOverrides: 去掉后缀
  • set标签:主要用于解决set关键字及多出一个【,】问题
  • choose标签:类似java中if-else【switch-case】结构
  • foreach标签:类似java中for循环
  • collection: 要迭代的集合
  • item: 当前从集合中迭代出的元素
  • separator: 元素与元素之间的分隔符
  • open: 开始字符
  • close:结束字符
  • sql标签:提取可重用SQL片段

9.3 示例代码

1. <?xml version="1.0" encoding="UTF-8" ?>
2. <!DOCTYPE mapper
3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
5. 
6. <mapper namespace="com.atguigu.mybatis.mapper.EmployeeMapper">
7. <sql id="emp_col">
8.         id,
9.         last_name,
10.         email,
11.         salary
12. </sql>
13. <sql id="select_employee">
14.         select
15.             id,
16.             last_name,
17.             email,
18.             salary
19.         from
20.             tbl_employee
21. </sql>
22. <!-- 按条件查询员工信息【条件不确定】-->
23. <select id="selectEmpByOpr" resultType="employee">
24. <include refid="select_employee"></include>
25. <where>
26. <if test="id != null">
27.                and id = #{id}
28. </if>
29. <if test="lastName != null">
30.                 and last_name = #{lastName}
31. </if>
32. <if test="email != null">
33.                 and email = #{email}
34. </if>
35. <if test="salary != null">
36.                 and salary = #{salary}
37. </if>
38. </where>
39. </select>
40. 
41. <select id="selectEmpByOprTrim" resultType="employee">
42. <include refid="select_employee"></include>
43. <trim prefix="where" suffixOverrides="and">
44. <if test="id != null">
45.                 id = #{id} and
46. </if>
47. <if test="lastName != null">
48.                 last_name = #{lastName} and
49. </if>
50. <if test="email != null">
51.                 email = #{email} and
52. </if>
53. <if test="salary != null">
54.                 salary = #{salary}
55. </if>
56. </trim>
57. </select>
58. 
59. <update id="updateEmpByOpr">
60.         update
61.             tbl_employee
62. <set>
63. <if test="lastName != null">
64.                 last_name=#{lastName},
65. </if>
66. <if test="email != null">
67.                 email=#{email},
68. </if>
69. <if test="salary != null">
70.                 salary=#{salary}
71. </if>
72. </set>
73.         where
74.             id = #{id}
75. </update>
76. 
77. 
78. <select id="selectEmpByOneOpr" resultType="employee">
79.         select
80. <include refid="emp_col"></include>
81.         from
82.             tbl_employee
83. <where>
84. <choose>
85. <when test="id != null">
86.                     id = #{id}
87. </when>
88. <when test="lastName != null">
89.                     last_name = #{lastName}
90. </when>
91. <when test="email != null">
92.                     email = #{email}
93. </when>
94. <when test="salary != null">
95.                     salary = #{salary}
96. </when>
97. <otherwise>
98.                     1=1
99. </otherwise>
100. </choose>
101. </where>
102. </select>
103. 
104. <select id="selectEmpByIds" resultType="employee">
105.         select
106.             id,
107.             last_name,
108.             email,
109.             salary
110.         from
111.             tbl_employee
112. <where>
113.             id in(
114. <foreach collection="ids" item="id" separator=",">
115.                 #{id}
116. </foreach>
117.             )
118. </where>
119. 
120. </select>
121. 
122. <insert id="batchInsertEmp">
123.         INSERT INTO
124.             tbl_employee(last_name,email,salary)
125.         VALUES
126. <foreach collection="employees" item="emp" separator=",">
127.                 (#{emp.lastName},#{emp.email},#{emp.salary})
128. </foreach>
129. </insert>
130. </mapper>

第十章 Mybatis中缓存机制

10.1 缓存概述

  • 生活中缓存
  • 缓存一些音频、视频优势
  • 节约数据流量
  • 提高播放性能
  • 程序中缓存【Mybatis缓存】
  • 使用缓存优势
  • 提高查询效率
  • 降低服务器压力

10.2 Mybatis中的缓存概述

  • 一级缓存
  • 二级缓存
  • 第三方缓存

10.3 Mybatis缓存机制之一级缓存

  • 概述:一级缓存【本地缓存(Local Cache)或SqlSession级别缓存】
  • 特点
  • 一级缓存默认开启
  • 不能关闭
  • 可以清空
  • 缓存原理
  • 第一次获取数据时,先从数据库中加载数据,将数据缓存至Mybatis一级缓存中【缓存底层实现原理Map,key:hashCode+查询的SqlId+编写的sql查询语句+参数】
  • 以后再次获取数据时,先从一级缓存中获取,如未获取到数据,再从数据库中获取数据。
  • 一级缓存五种失效情况
  1. 不同的SqlSession对应不同的一级缓存
  2. 同一个SqlSession但是查询条件不同
  3. 同一个SqlSession两次查询期间执行了任何一次增删改操作
  • 清空一级缓存
  1. 同一个SqlSession两次查询期间手动清空了缓存
  • sqlSession.clearCache()
  1. 同一个SqlSession两次查询期间提交了事务
  • sqlSession.commit()

10.4 Mybatis缓存机制之二级缓存

  • 二级缓存【second level cache】概述
  • 二级缓存【全局作用域缓存】
  • SqlSessionFactory级别缓存
  • 二级缓存特点
  • 二级缓存默认关闭,需要开启才能使用
  • 二级缓存需要提交sqlSession或关闭sqlSession时,才会缓存。
  • 二级缓存使用的步骤:
    ① 全局配置文件中开启二级缓存<setting name="cacheEnabled" value="true"/>
    ② 需要使用二级缓存的映射文件处使用cache配置缓存<cache />
    ③ 注意:POJO需要实现Serializable接口
    关闭sqlSession或提交sqlSession时,将数据缓存到二级缓存
  • 二级缓存底层原理
  • 第一次获取数据时,先从数据库中获取数据,将数据缓存至一级缓存;当提交或关闭SqlSession时,将数据缓存至二级缓存
  • 以后再次获取数据时,先从一级缓存中获取数据,如一级缓存没有指定数据,再去二级缓存中获取数据。如二级缓存也没有指定数据时,需要去数据库中获取数据,......
  • 二级缓存相关属性
  • eviction=“FIFO”:缓存清除【回收】策略。
  • LRU – 最近最少使用的:移除最长时间不被使用的对象。
  • FIFO – 先进先出:按对象进入缓存的顺序来移除它们。
  • flushInterval:刷新间隔,单位毫秒
  • size:引用数目,正整数
  • readOnly:只读,true/false
  • 二级缓存的失效情况
  • 在两次查询之间,执行增删改操作,会同时清空一级缓存和二级缓存
  • sqlSession.clearCache():只是用来清除一级缓存。

10.5 Mybatis中缓存机制之第三方缓存

  • 第三方缓存:EhCache
  • EhCache 是一个纯Java的进程内缓存框架
  • 使用步骤
  • 导入jar包
1. <!-- mybatis-ehcache -->
2. <dependency>
3. <groupId>org.mybatis.caches</groupId>
4. <artifactId>mybatis-ehcache</artifactId>
5. <version>1.0.3</version>
6. </dependency>
7. 
8. <!-- slf4j-log4j12 -->
9. <dependency>
10. <groupId>org.slf4j</groupId>
11. <artifactId>slf4j-log4j12</artifactId>
12. <version>1.6.2</version>
13. <scope>test</scope>
14. </dependency>
  • 编写配置文件【ehcache.xml】
1. <?xml version="1.0" encoding="UTF-8"?>
2. <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3. xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">
4. <!-- 磁盘保存路径 -->
5. <diskStore path="E:\mybatis\ehcache" />
6. 
7. <defaultCache
8. maxElementsInMemory="512"
9. maxElementsOnDisk="10000000"
10. eternal="false"
11. overflowToDisk="true"
12. timeToIdleSeconds="120"
13. timeToLiveSeconds="120"
14. diskExpiryThreadIntervalSeconds="120"
15. memoryStoreEvictionPolicy="LRU">
16. </defaultCache>
17. </ehcache>
  • 加载第三方缓存【映射文件】
  • 开始使用
  • 注意事项
  • 第三方缓存,需要建立在二级缓存基础上【需要开启二级缓存,第三方缓存才能生效】
  • 如何让第三方缓存失效【将二级缓存设置失效即可】


相关文章
|
13天前
|
SQL XML Java
mybatis 调用修改SQL时 出现了一个问题 没有修改成功也没有报错
mybatis 调用修改SQL时 出现了一个问题 没有修改成功也没有报错
17 0
|
1月前
|
SQL XML Java
程序员都要懂的SQL防注入Mybatis框架SQL防注入
程序员都要懂的SQL防注入Mybatis框架SQL防注入
21 0
|
4天前
|
缓存 NoSQL Java
17:缓存机制-Java Spring
17:缓存机制-Java Spring
18 5
|
5天前
|
存储 缓存 自然语言处理
深入PHP内核:探索Opcode缓存机制
【5月更文挑战第1天】 在动态语言如PHP的执行过程中,每次脚本被请求时都需要经过一系列复杂的解析和编译步骤。为了优化这一过程并提高性能,PHP引入了Opcode缓存机制。本文将详细探讨Opcode的概念、作用以及它如何显著提升PHP应用的执行效率。我们将从缓存原理出发,分析几种常见的Opcode缓存工具,并通过实例说明如何在实际项目中实现和优化缓存策略。
|
7天前
|
缓存 NoSQL PHP
【PHP开发专栏】PHP缓存机制与实现
【4月更文挑战第29天】本文介绍了PHP缓存的基本概念、策略及实现方式。PHP缓存包括应用缓存、Web服务器缓存、数据库缓存和分布式缓存,常见策略有缓存预热、更新和懒加载。PHP的缓存实现包括文件缓存、APC、OPcache、Memcached和Redis。最佳实践包括缓存热点数据、控制粒度、设置失效策略、保证一致性和确保安全性。文中还提供了一个新闻列表和详情页的缓存实战示例,帮助开发者理解如何在实际项目中应用缓存。
|
9天前
|
SQL 安全 Java
【Mybatis】Mybatis如何防止sql注入
【Mybatis】Mybatis如何防止sql注入
|
11天前
|
SQL Java 数据库连接
【Mybatis】动态sql之sql的复用
【Mybatis】动态sql之sql的复用
9 0
|
12天前
|
SQL Java 关系型数据库
mybatis-plus启动时自动执行sql脚本
mybatis-plus启动时自动执行sql脚本
17 1
|
13天前
|
缓存 流计算
缓存命中率和过期机制的一般思路
【4月更文挑战第20天】缓存命中率是评估缓存效果的关键,目标是达到90%以上,但某些频繁的小请求场景可能无法实现。过期机制可采用定时删除(精确但开销大)、延迟队列(精确但有队列开销)、懒惰删除(简单但时间不精确)或定期删除(简单但性能损耗不可控)。
17 4
|
13天前
|
缓存 Linux
linux系统缓存机制
linux系统缓存机制