Mybatis——动态SQL及缓存

Mybatis——动态SQL及缓存

九、动态SQL

Mybatis框架的动态SQL技术是一种根据特定条件动态拼装SQL语句的功能,它存在的意义是为了解决拼接SQL语句字符串时的痛点问题。

  • 因为当我们没有往数据库进行传输数据时,mabatis进行了查询会查询到null;
  • 而当我们没有设置值时,mybatis进行查询会查询到空字符串 “ ”
  • 如果查询到了null与空字符串,则where后面的表达式则无法进行拼接,此时我们运用动态sql。

1、if

if标签可通过test属性的表达式进行判断,若表达式的结果为true,则标签中的内容会执行;反之标签中的内容不会执行

<!--List<Emp> getEmpListByMoreTJ(Emp emp);-->

<select id="getEmpListByMoreTJ" resultType="Emp">
   <!--为了避免ename出现null与空字符串(就会多出一个and),而无法与下面的sql语句进行拼接,加上一句恒成立的1=1-->
   select * from t_emp where 1=1
   <if test="ename != '' and ename != null">
      and ename = #{ename}
   </if>
   <if test="age != '' and age != null">
      and age = #{age}
   </if>
   <if test="sex != '' and sex != null">
      and sex = #{sex}
   </if>
</select>

2、where

<select id="getEmpListByMoreTJ2" resultType="Emp">
   select * from t_emp
   <where>
      <if test="ename != '' and ename != null">
         ename = #{ename}
      </if>
      <if test="age != '' and age != null">
         and age = #{age}
      </if>
      <if test="sex != '' and sex != null">
         and sex = #{sex}
      </if>
   </where>
</select>

where和if一般结合使用:

  • 若where标签中的if条件都不满足,则where标签没有任何功能,即不会添加where关键字
  • 若where标签中的if条件满足,则where标签会自动添加where关键字,并将条件最前方多余的and去掉

注意:where标签不能去掉条件最后多余的and

3、trim

<select id="getEmpListByMoreTJ" resultType="Emp">
   select * from t_emp
   <trim prefix="where" suffixOverrides="and">
      <if test="ename != '' and ename != null">
         ename = #{ename} and
      </if>
      <if test="age != '' and age != null">
         age = #{age} and
      </if>
      <if test="sex != '' and sex != null">
         sex = #{sex}
      </if>
   </trim>
</select>

trim用于去掉或添加标签中的内容(若标签中无内容时,trim也没有任何效果(不会只剩下where))
常用属性:

  • prefix:在trim标签中的内容的前面添加某些内容
  • prefixOverrides:在trim标签中的内容的前面去掉某些内容
  • suffix:在trim标签中的内容的后面添加某些内容
  • suffixOverrides:在trim标签中的内容的后面去掉某些内容

4、choose、when、otherwise

when 标签至少有一个 ;otherwise标签至多有一个。
choose、when、otherwise相当于if…else if…else

<!--List<Emp> getEmpListByChoose(Emp emp);-->
<select id="getEmpListByChoose" resultType="Emp">
<!--这里的include refid 是可以记录一段公共sql片段,在使用的地方通过include标签进行引入-->
   select <include refid="empColumns"></include> from t_emp
   <where>
      <choose>
         <when test="ename != '' and ename != null">
            ename = #{ename}
         </when>
         <when test="age != '' and age != null">
            age = #{age}
         </when>
         <when test="sex != '' and sex != null">
            sex = #{sex}
         </when>
         <when test="email != '' and email != null">
            email = #{email}
         </when>
      </choose>
   </where>
</select>

5、foreach(批量添加和批量删除时常用)

<!--int insertMoreEmp(@Param(“emps”)List<Emp> emps);-->
<insert id="insertMoreEmp">
   insert into t_emp values
   <foreach collection="emps" item="emp" separator=",">
      (null,#{emp.ename},#{emp.age},#{emp.sex},#{emp.email},null)
   </foreach>
   
</insert>
<!--int deleteMoreByArray(@Param(“eid”)int[] eids);-->
<delete id="deleteMoreByArray">
   delete from t_emp where
   <foreach collection="eids" item="eid" separator="or">
      eid = #{eid}
   </foreach>
</delete>

<!--int deleteMoreByArray(@Param(“eid”)int[] eids);-->
<delete id="deleteMoreByArray">
   delete from t_emp where eid in
   <foreach collection="eids" item="eid" separator="," open="(" close=")">
      #{eid}
   </foreach>
</delete>

属性:

  • collection:设置要循环的数组或集合
  • item:表示集合或数组中的每一个数据
  • separator:设置循环体之间的分隔符
  • open:设置foreach标签中的内容的开始符
  • close:设置foreach标签中的内容的结束符

6、SQL片段(便捷操作)

sql片段,可以记录一段公共sql片段,在使用的地方通过include标签进行引入

<sql id="empColumns">
   eid,ename,age,sex,did
</sql>
   select <include refid="empColumns"></include> from t_emp

十、MyBatis的缓存

1、MyBatis的一级缓存

一级缓存是SqlSession级别的,通过同一个SqlSession查询的数据会被缓存,下次查询相同的数据,就会从缓存中直接获取,不会从数据库重新访问

使一级缓存失效的四种情况:

  • 不同的SqlSession对应不同的一级缓存
  • 同一个SqlSession但是查询条件不同
  • 同一个SqlSession两次查询期间执行了任何一次增删改操作
  • 同一个SqlSession两次查询期间手动清空了缓存(SqlSession.clearCache(); 手动清空缓存)

2、MyBatis的二级缓存

二级缓存是SqlSessionFactory级别,通过同一个SqlSessionFactory创建的SqlSession查询的结果会被缓存;此后若再次执行相同的查询语句,结果就会从缓存中获取

二级缓存开启的条件:

a>在核心配置文件中,设置全局配置属性cacheEnabled=“true”,默认为true,不需要设置
b>在映射文件中添加标签
c>二级缓存必须在SqlSession关闭(sqlSession.close();)或提交之后有效
d>查询的数据所转换的实体类类型必须实现序列化的接口

使二级缓存失效的情况:

  • 两次查询之间执行了任意的增删改,会使一级和二级缓存同时失效

3、二级缓存的相关配置

在mapper配置文件中添加的cache标签可以设置一些属性:

  • eviction属性:缓存回收策略
    LRU(Least Recently Used) – 最近最少使用的:移除最长时间不被使用的对象。
    FIFO(First in First out) – 先进先出:按对象进入缓存的顺序来移除它们。
    SOFT – 软引用:移除基于垃圾回收器状态和软引用规则的对象。
    WEAK – 弱引用:更积极地移除基于垃圾收集器状态和弱引用规则的对象。
    默认的是 LRU。
  • flushInterval属性:刷新间隔,单位毫秒
    默认情况是不设置,也就是没有刷新间隔,缓存仅仅调用语句时刷新
  • size属性:引用数目,正整数
    代表缓存最多可以存储多少个对象,太大容易导致内存溢出
  • readOnly属性:只读,true/false
    true:只读缓存;会给所有调用者返回缓存对象的相同实例。因此这些对象不能被修改。这提供了很重要的性能优势。
    false:读写缓存;会返回缓存对象的拷贝(通过序列化)。这会慢一些,但是安全,因此默认是false。

4、MyBatis缓存查询的顺序

  • 先查询二级缓存,(因为一级缓存不是直接写入二级缓存,需要等到SqlSession关闭或提交后才会写入二级缓存中,所以先查二级缓存。)因为二级缓存中可能会有其他程序已经查出来的数据,可以拿来直接使用。
  • 如果二级缓存没有命中,再查询一级缓存
  • 如果一级缓存也没有命中,则查询数据库
  • SqlSession关闭之后,一级缓存中的数据会写入二级缓存

5、整合第三方缓存EHCache(代替Mabitas中的二级缓存。Mybatis中的一次缓存不能被代替。)

a>添加依赖

<!-- Mybatis EHCache整合包 -->
<dependency>
   <groupId>org.mybatis.caches</groupId>
   <artifactId>mybatis-ehcache</artifactId>
   <version>1.2.1</version>
</dependency>

<!-- slf4j日志门面的一个具体实现 -->
<dependency>
   <groupId>ch.qos.logback</groupId>
   <artifactId>logback-classic</artifactId>
   <version>1.2.3</version>
</dependency>

b>各jar包功能
在这里插入图片描述
c>创建EHCache的配置文件ehcache.xml

<?xml version="1.0" encoding="utf-8" ?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">
      <!-- 磁盘保存路径 -->
      <diskStore path="D:\atguigu\ehcache"/>
      
      <defaultCache
            maxElementsInMemory="1000"
            maxElementsOnDisk="10000000"
            eternal="false"
            overflowToDisk="true"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            diskExpiryThreadIntervalSeconds="120"
            memoryStoreEvictionPolicy="LRU">
      </defaultCache>
</ehcache>

d>设置二级缓存的类型

<cache type="org.mybatis.caches.ehcache.EhcacheCache"/>

e>加入logback日志

存在SLF4J时,作为简易日志的log4j将失效,此时我们需要借助SLF4J的具体实现logback来打印日志。

创建logback的配置文件logback.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="true">
   <!-- 指定日志输出的位置 -->
   <appender name="STDOUT"
       class="ch.qos.logback.core.ConsoleAppender">
      <encoder>
      <!-- 日志输出的格式 -->
      <!-- 按照顺序分别是:时间、日志级别、线程名称、打印日志的类、日志主体内容、换行 -
->

      <pattern>[%d{HH:mm:ss.SSS}] [%-5level] [%thread] [%logger]
[%msg]%n</pattern>
      </encoder>
   </appender>
   
   <!-- 设置全局日志级别。日志级别按顺序分别是:DEBUG、INFO、WARN、ERROR -->
   <!-- 指定任何一个日志级别都只打印当前级别和后面级别的日志。 -->
   <root level="DEBUG">
      <!-- 指定打印日志的appender,这里通过“STDOUT”引用了前面配置的appender -->
      <appender-ref ref="STDOUT" />
   </root>
   
   <!-- 根据特殊需求指定局部日志级别 -->
   <logger name="com.atguigu.crowd.mapper" level="DEBUG"/>
   
</configuration>

f>EHCache配置文件说明

在这里插入图片描述

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

真真最可爱

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值