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

做网站需要规划哪些内容access做网站服务器

做网站需要规划哪些内容,access做网站服务器,苗木网站素材,html静态网页模板下载介绍 缓存,就是将经常访问的数据,放到内存中,减少对数据库的访问,提高查询速度。Mybatis中也有缓存的概念,分为一级缓存和二级缓存。 一级缓存 一级缓存是Mybatis中SqlSession对象的缓存。当我们执行查询以后&#x…

介绍

  缓存,就是将经常访问的数据,放到内存中,减少对数据库的访问,提高查询速度。Mybatis中也有缓存的概念,分为一级缓存和二级缓存。

一级缓存

  一级缓存是Mybatis中SqlSession对象的缓存。当我们执行查询以后,结果会存入到SqlSession为我们提供的一块区域中,该区域的结构是一个Map,当我们再次查询同样的数据,mybatis会先去Sqlsession中查询是否有,有的话直接拿出来用,没有再去查询数据库。

  示例:
  Mysql中建好表,对应的实体类也建好:

import java.util.Date;public class Student {private int id;private int age;private String name;private Date birthday;private int score;//get和set方法省略...}

  Dao层的接口:

import java.io.Serializable;public interface StudentMapper {Student getById(Serializable id);}

  Dao层的xml:

<mapper namespace="com.gs.spring_boot_demo.mybatis.mapper.StudentMapper"><select id="getById" resultType="com.gs.spring_boot_demo.mybatis.entity.Student">select * from student where id = #{id}</select></mapper>

  测试类:

import com.gs.spring_boot_demo.mybatis.entity.Student;
import com.gs.spring_boot_demo.mybatis.mapper.StudentMapper;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest
public class MybatisTest {@Autowiredprivate StudentMapper mapper;@Autowiredprivate SqlSessionFactory factory;@Testpublic void test() {// 不能用这种方式,这种方式不会有一级缓存的//Student student = mapper.getById(1);//Student student1 = mapper.getById(1);//System.out.println(student == student1);SqlSession sqlSession = factory.openSession();StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);Student student = mapper.getById(1);Student student1 = mapper.getById(1);System.out.println(student == student1);}}

  看到控制台的输出:

在这里插入图片描述

  查了2次id为1的数据,但只有1条发送sql的日志,并且2次查询的结果是同一个对象。说明一级缓存生效了。
  清除一级缓存的方法也有很多,调用SqlSession对象的commit()、close()、clearCache()、增加、修改、删除时,就会删除一级缓存。

    @Testpublic void test() {SqlSession sqlSession = factory.openSession();StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);Student student = mapper.getById(1);//commit()也能清除//sqlSession.commit();sqlSession.clearCache();Student student1 = mapper.getById(1);System.out.println(student == student1);}

  再次查看控制台的日志:

在这里插入图片描述

  可以看到,因为删除了一级缓存,所以第二次的查询去发送sql查数据库了,2个查询结果也自然不是同一个对象。

二级缓存

  一级缓存的前提条件是同一SqlSession对象,而二级缓存是跨SqlSession对象的,即多个SqlSession对象共享这一份缓存。当一级缓存关闭或提交后,一级缓存会转入到二级缓存,并且缓存的是数据而不是对象。

  示例:
  二级缓存默认未开启,首先开启它:

  Dao层的xml中添加:

<cache/>

  xml的select标签设置属性useCache:

<mapper namespace="com.gs.spring_boot_demo.mybatis.mapper.StudentMapper"><cache/><select id="getById" resultType="com.gs.spring_boot_demo.mybatis.entity.Student" useCache="true">select * from student where id = #{id}</select></mapper>

  开启二级缓存后,实体类Student要实现Serializable接口:

import java.io.Serializable;
import java.util.Date;public class Student implements Serializable {private int id;private int age;private String name;private Date birthday;private int score;//get和set方法省略...}

  测试类:

@Test
public void test() {SqlSession sqlSession = factory.openSession();StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);Student student = mapper.getById(1);//把一级缓存转入到二级缓存,同时清空一级缓存sqlSession.commit();//相同的SqlSession对象Student student1 = mapper.getById(1);System.out.println(student == student1);//不同的SqlSession对象SqlSession sqlSession1 = factory.openSession();StudentMapper mapper1 = sqlSession1.getMapper(StudentMapper.class);Student student2 = mapper1.getById(1);System.out.println(student == student2);
}

  看到控制台的日志:

在这里插入图片描述

  getById(Serializable id)调用了3次,但是只有1条sql日志,后面的2次调用都是查的二级缓存,但对象不是同一个,所以说缓存的是数据而不是对象。

http://www.yayakq.cn/news/968816/

相关文章:

  • 网站基础建设一般多少钱有什么做调查的网站
  • 网站如何能让百度收录北京公司网站建设报价表
  • 现在帮别人做网站赚钱不免费logo在线制作头像
  • 建一个网站问谁WordPress網站放ICP
  • 静态网站公用头部如何调用标题怎么查找网站死链
  • 开源外贸网站网站开发准备流程图
  • 漂亮企业网站源码成都抢先看新钱新闻全搜索
  • 无锡网站seo外包青少儿编程
  • 电脑如何做穿透外网网站wordpress 页面模板不显示
  • 网站建设管理视频wordpress网站属于什么网站吗
  • 一键建网站安阳企业网站建设公司
  • 蒙文网站建设情况汇报材料wordpress主题模板仿
  • 做视频网站用什么服务器配置实验室网站建设
  • 国外好玩的网站wordpress mysql 5.7
  • 青岛住房和城乡建设厅网站网站标题堆砌关键词
  • 宁夏住房和建设厅网站优化稳定网站排名
  • 宁国市网站关键词优化外包最好的建站平台
  • 深圳龙华区怎么样湖州seo排名
  • 中小企业网站建设与管理主要讲授什么福田祥菱v1质量怎么样
  • 小榄网站seo综合优化公司
  • 推广网站广告有哪些wordpress视频无法播放器
  • 网站快照是自己做的吗网站优秀网站地址
  • 余杭区建设规划局网站做网站学
  • 阳西哪里有做网站云浮网站建设咨询
  • 北京造价员变更在哪个网站做桂林人论坛app
  • 门户网站的设计南阳做网站公司
  • 福州网站设计软件公司wordpress 置顶 插件
  • 网站怎么做搜索引擎优化_企业网站策划建设方案
  • 网站建设公司哈网站推广线上推广
  • 举例描述该如何布局网站关键词常见的网站开发语言