当前位置: 首页 > news >正文

mybatis:动态sql【2】+转义符+缓存

目录

一、动态sql

1.set、if

2.foreach

二、转义符

三、缓存cache

1. 一级缓存

2. 二级缓存


一、动态sql

1.set、if

        在update语句中使用set标签,动态更新set后的sql语句,,if作为判断条件。

    <update id="updateStuent" parameterType="Student">update student<set><if test="num!=0">num=#{num},</if><if test="name!=null">name=#{name},</if><if test="gender!=0">gender=#{gender}</if></set>where id = #{id}</update>

2.foreach

foreach对集合进行遍历,尤其是in条件语句

    <delete id="deleteStudents" parameterType="list" >delete from student where id in<foreach collection="list" item="item" open="(" separator="," close=")">#{item}</foreach></delete>

collection 是传入参数名,item为元素名,separator代表元素之间的间隔符。  

详细动态sql可查看官网 :mybatis – MyBatis 3 | 动态 SQL

二、转义符

1.在 mybatis 中的 xml 文件,一些特殊符号需要转译:

特殊字符转译字符
<&lt;
"&quot;
&apos;
&&amp;
>&gt;

2.使用<![CDATA[]]> 

通过<![CDATA[ 特殊字符 ]]>包裹特殊字符也可以

    <!--转义字符   " <" 号会报错,">" 号不会报错  --><select id="findstuTurn" resultType="Student">select id,num,gender from student where num &lt; 1000</select><select id="findstuTurn2" resultType="Student">select id,num,gender from student where num <![CDATA[ < ]]> 1000</select>

注意:

  1.  " <" 号会报错,">" 号不会报错;
  2.  如果<![CDATA[ ]]> 里有<if> </if> <where> </where><choose> </choose> <trim> </trim> 等这些标签都不会被解析,所以只把有特殊字符的语句放在 <![CDATA[ ]]> 尽量缩小<![CDATA[ ]]>的范围。 

三、缓存cache

缓存作用:让程序更快的访问数据,减少对数据库的访问压力。

缓存原理:从数据库中查询出来的对象在使用完后不要销毁,而是存储在内存(缓存)中,当再次需要获取该对象时,直接从内存(缓存)中直接获取,不再向数据库执行select 语句,从而减少了对数据库的查询次数,因此提高了数据库的性能。

mybatis有两种缓存方式:

1. 一级缓存

     在同一个SqlSession中执行相同的sql且参数也相同(完全相同的sql查询),第二次查询时,直接从sqlSession中获取,而不再从数据库中获取。   在没有默认配置情况下,mybatis开启一级缓存。

例如:

     @Testvoid testCache(){SqlSession sqlSession = MybatisUtil.getSqlsession();StudentDao studentDao =  sqlSession.getMapper(StudentDao.class);List<Student> students = studentDao.findstuTurn();System.out.println(students);List<Student> student2= studentDao.findstuTurn();sqlSession.commit();sqlSession.close();}

日志中只显示一条查询 

 

一级缓存失效:

  1. sqlSession.close();
  2. sqlSession.clearCache();
  3. SqlSession 中执行了任何一个修改操作(update()、delete()、insert()) ,都会清空缓存的数据。

2. 二级缓存

二级缓存是 SqlSessionFactory 级别的,数据存放在SqlSessionFactory中。

需要手动启动:

1.在核心配置文件中开启二级缓存:

        <!--开启二级缓存--><setting name="cacheEnabled" value="true"/>

2.将 POJO 类实现序列化接口:

        implements Serializable

3.在mapper映射文件中添加cache标签,表示此mapper开启二级缓存:

    <cache flushInterval="100000" ></cache>

 flushInterval可以设置刷新时间(缓存失效)

当 SqlSeesion 关闭时,会将数据存入到二级缓存。

示例:

    @Testvoid testCache1(){SqlSession sqlSession = MybatisUtil.getSqlsession();StudentDao studentDao =  sqlSession.getMapper(StudentDao.class);List<Student> students = studentDao.findstuTurn();System.out.println(students);sqlSession.commit();//sqlSession关闭sqlSession.close();SqlSession sqlSession2 = MybatisUtil.getSqlsession();StudentDao studentDao2 =  sqlSession2.getMapper(StudentDao.class);List<Student> students2 = studentDao2.findstuTurn();System.out.println(students2);sqlSession2.commit();sqlSession2.close();}

结果只查询一次: 

http://www.lryc.cn/news/145462.html

相关文章:

  • 2021年09月 C/C++(五级)真题解析#中国电子学会#全国青少年软件编程等级考试
  • Ansible学习笔记1
  • 解决centos离线安装cmake找不到OpenSSL问题
  • Java 中数据结构ArrayList的用法
  • UDP 多播(组播)
  • 分布式环境集成JWT(Java Web Token)
  • Python实战之数据表提取和下载自动化
  • Midjourney学习(三)6个高级应用
  • C语言:指针类型的意义
  • 如何将 PDF 转换为 Word:前 5 个应用程序
  • AP5192 DC-DC降压恒流LED汽车头灯摩托车电动车大灯电源驱动
  • Python Opencv实践 - Canny边缘检测
  • Python编程练习与解答 练习119:低于和高于平均水平
  • vue中的nextTick的作用
  • 如何通过四个步骤清理网络防火墙规则
  • 打开谷歌浏览器远程调试功能
  • ChatGPT时代的我的博客
  • 同步有关的思考。
  • Flutter Web 项目网络请求报 XMLHttpRequest error 解决方案
  • Python面试:什么是GIL
  • idea --Git Commit Template插件
  • 使用Python脚本添加新的相关节点到arxml文件中的指定位置
  • iOS开发Swift-闭包
  • 从零开始学JAVA——常用类
  • LeetCode 面试题 02.04. 分割链表
  • 基于大语言模型知识问答应用落地实践 – 知识库构建(下)
  • Hive UDF自定义函数上线速记
  • 【nacos】【sentinel】【gateway】docker-compose安装及web项目部署
  • 用idea查看sqlite数据库idea sqlite
  • 流媒体服务器与视频服务器有什么区别?