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

镇江网站建设案例洛阳网站建站

镇江网站建设案例,洛阳网站建站,wordpress数据备份插件,做设计排版除了昵图网还有什么网站一级缓存 Mybatis对缓存提供支持#xff0c;但是在没有配置的默认情况下#xff0c;它只开启一级缓存#xff0c;一级缓存只是相对于同一个SqlSession而言。所以在参数和SQL完全一样的情况下#xff0c;我们使用同一个SqlSession对象调用一个Mapper方法#xff0c;往往只执… 一级缓存   Mybatis对缓存提供支持但是在没有配置的默认情况下它只开启一级缓存一级缓存只是相对于同一个SqlSession而言。所以在参数和SQL完全一样的情况下我们使用同一个SqlSession对象调用一个Mapper方法往往只执行一次SQL因为使用SelSession第一次查询后MyBatis会将其放在缓存中以后再查询的时候如果没有声明需要刷新并且缓存没有超时的情况下SqlSession都会取出当前缓存的数据而不会再次发送SQL到数据库。                  为什么要使用一级缓存不用多说也知道个大概。但是还有几个问题我们要注意一下。   1、一级缓存的生命周期有多长   a、MyBatis在开启一个数据库会话时会 创建一个新的SqlSession对象SqlSession对象中会有一个新的Executor对象。Executor对象中持有一个新的PerpetualCache对象当会话结束时SqlSession对象及其内部的Executor对象还有PerpetualCache对象也一并释放掉。   b、如果SqlSession调用了close()方法会释放掉一级缓存PerpetualCache对象一级缓存将不可用。   c、如果SqlSession调用了clearCache()会清空PerpetualCache对象中的数据但是该对象仍可使用。   d、SqlSession中执行了任何一个update操作(update()、delete()、insert()) 都会清空PerpetualCache对象的数据但是该对象可以继续使用     2、怎么判断某两次查询是完全相同的查询   mybatis认为对于两次查询如果以下条件都完全一样那么就认为它们是完全相同的两次查询。   2.1 传入的statementId   2.2 查询时要求的结果集中的结果范围   2.3. 这次查询所产生的最终要传递给JDBC java.sql.Preparedstatement的Sql语句字符串boundSql.getSql()    2.4 传递给java.sql.Statement要设置的参数值 二级缓存   MyBatis的二级缓存是Application级别的缓存它可以提高对数据库查询的效率以提高应用的性能。   MyBatis的缓存机制整体设计以及二级缓存的工作模式        SqlSessionFactory层面上的二级缓存默认是不开启的二级缓存的开席需要进行配置实现二级缓存的时候MyBatis要求返回的POJO必须是可序列化的。 也就是要求实现Serializable接口配置方法很简单只需要在映射XML文件配置就可以开启缓存了cache/如果我们配置了二级缓存就意味着 映射语句文件中的所有select语句将会被缓存。映射语句文件中的所欲insert、update和delete语句会刷新缓存。缓存会使用默认的Least Recently UsedLRU最近最少使用的算法来收回。根据时间表比如No Flush Interval,CNFI没有刷新间隔缓存不会以任何时间顺序来刷新。缓存会存储列表集合或对象(无论查询方法返回什么)的1024个引用缓存会被视为是read/write(可读/可写)的缓存意味着对象检索不是共享的而且可以安全的被调用者修改不干扰其他调用者或线程所做的潜在修改。实践 一、创建一个POJO Bean并序列化   由于二级缓存的数据不一定都是存储到内存中它的存储介质多种多样所以需要给缓存的对象执行序列化。(如果存储在内存中的话实测不序列化也可以的。) package com.yihaomen.mybatis.model;import com.yihaomen.mybatis.enums.Gender; import java.io.Serializable; import java.util.List;/***  ProjectName: springmvc-mybatis */ public class Student implements Serializable{private static final long serialVersionUID 735655488285535299L;private String id;private String name;private int age;private Gender gender;private ListTeacher teachers;settersgetters()....;toString(); }    二、在映射文件中开启二级缓存 ?xml version1.0 encodingUTF-8 ? !DOCTYPE mapper PUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtd mapper namespacecom.yihaomen.mybatis.dao.StudentMapper!--开启本mapper的namespace下的二级缓存--!--eviction:代表的是缓存回收策略目前MyBatis提供以下策略。(1) LRU,最近最少使用的一处最长时间不用的对象(2) FIFO,先进先出按对象进入缓存的顺序来移除他们(3) SOFT,软引用移除基于垃圾回收器状态和软引用规则的对象(4) WEAK,弱引用更积极的移除基于垃圾收集器状态和弱引用规则的对象。这里采用的是LRU移除最长时间不用的对形象flushInterval:刷新间隔时间单位为毫秒这里配置的是100秒刷新如果你不配置它那么当SQL被执行的时候才会去刷新缓存。size:引用数目一个正整数代表缓存最多可以存储多少个对象不宜设置过大。设置过大会导致内存溢出。这里配置的是1024个对象readOnly:只读意味着缓存数据只能读取而不能修改这样设置的好处是我们可以快速读取缓存缺点是我们没有办法修改缓存他的默认值是false不允许我们修改--cache evictionLRU flushInterval100000 readOnlytrue size1024/resultMap idstudentMap typeStudentid propertyid columnid /result propertyname columnname /result propertyage columnage /result propertygender columngender typeHandlerorg.apache.ibatis.type.EnumOrdinalTypeHandler //resultMapresultMap idcollectionMap typeStudent extendsstudentMapcollection propertyteachers ofTypeTeacherid propertyid columnteach_id /result propertyname columntname/result propertygender columntgender typeHandlerorg.apache.ibatis.type.EnumOrdinalTypeHandler/result propertysubject columntsubject typeHandlerorg.apache.ibatis.type.EnumTypeHandler/result propertydegree columntdegree javaTypestring jdbcTypeVARCHAR//collection/resultMapselect idselectStudents resultMapcollectionMapSELECTs.id, s.name, s.gender, t.id teach_id, t.name tname, t.gender tgender, t.subject tsubject, t.degree tdegreeFROMstudent sLEFT JOINstu_teach_rel strONs.id str.stu_idLEFT JOINteacher tONt.id str.teach_id/select!--可以通过设置useCache来规定这个sql是否开启缓存ture是开启false是关闭--select idselectAllStudents resultMapstudentMap useCachetrueSELECT id, name, age FROM student/select!--刷新二级缓存select idselectAllStudents resultMapstudentMap flushCachetrueSELECT id, name, age FROM student/select-- /mapper   三、在 mybatis-config.xml中开启二级缓存 ?xml version1.0 encodingUTF-8 ? !DOCTYPE configuration PUBLIC -//mybatis.org//DTD Config 3.0//ENhttp://mybatis.org/dtd/mybatis-3-config.dtd configurationsettings!--这个配置使全局的映射器(二级缓存)启用或禁用缓存--setting namecacheEnabled valuetrue /...../settings.... /configuration   四、测试 package com.yihaomen.service.student;import com.yihaomen.mybatis.dao.StudentMapper; import com.yihaomen.mybatis.model.Student; import com.yihaomen.mybatis.model.Teacher; import com.yihaomen.service.BaseTest; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import java.util.List;/***   *  ProjectName: springmvc-mybatis */ public class TestStudent extends BaseTest {public static void selectAllStudent() {SqlSessionFactory sqlSessionFactory getSession();SqlSession session sqlSessionFactory.openSession();StudentMapper mapper session.getMapper(StudentMapper.class);ListStudent list mapper.selectAllStudents();System.out.println(list);System.out.println(第二次执行);ListStudent list2 mapper.selectAllStudents();System.out.println(list2);session.commit();System.out.println(二级缓存观测点);SqlSession session2 sqlSessionFactory.openSession();StudentMapper mapper2 session2.getMapper(StudentMapper.class);ListStudent list3 mapper2.selectAllStudents();System.out.println(list3);System.out.println(第二次执行);ListStudent list4 mapper2.selectAllStudents();System.out.println(list4);session2.commit();}public static void main(String[] args) {selectAllStudent();} }   结果 [QC] DEBUG [main] org.apache.ibatis.transaction.jdbc.JdbcTransaction.setDesiredAutoCommit(98) | Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection51e0173d][QC] DEBUG [main] org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(139) | Preparing: SELECT id, name, age FROM student [QC] DEBUG [main] org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(139) | Parameters: [QC] DEBUG [main] org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(139) | Total: 6[Student{id1, name刘德华, age55, gendernull, teachersnull}, Student{id2, name张惠妹, age49, gendernull, teachersnull}, Student{id3, name谢霆锋, age35, gendernull, teachersnull}, Student{id4, name王菲, age47, gendernull, teachersnull}, Student{id5, name汪峰, age48, gendernull, teachersnull}, Student{id6, name章子怡, age36, gendernull, teachersnull}]第二次执行[QC] DEBUG [main] org.apache.ibatis.cache.decorators.LoggingCache.getObject(62) | Cache Hit Ratio [com.yihaomen.mybatis.dao.StudentMapper]: 0.0[Student{id1, name刘德华, age55, gendernull, teachersnull}, Student{id2, name张惠妹, age49, gendernull, teachersnull}, Student{id3, name谢霆锋, age35, gendernull, teachersnull}, Student{id4, name王菲, age47, gendernull, teachersnull}, Student{id5, name汪峰, age48, gendernull, teachersnull}, Student{id6, name章子怡, age36, gendernull, teachersnull}]二级缓存观测点[QC] DEBUG [main] org.apache.ibatis.cache.decorators.LoggingCache.getObject(62) | Cache Hit Ratio [com.yihaomen.mybatis.dao.StudentMapper]: 0.3333333333333333[Student{id1, name刘德华, age55, gendernull, teachersnull}, Student{id2, name张惠妹, age49, gendernull, teachersnull}, Student{id3, name谢霆锋, age35, gendernull, teachersnull}, Student{id4, name王菲, age47, gendernull, teachersnull}, Student{id5, name汪峰, age48, gendernull, teachersnull}, Student{id6, name章子怡, age36, gendernull, teachersnull}]第二次执行[QC] DEBUG [main] org.apache.ibatis.cache.decorators.LoggingCache.getObject(62) | Cache Hit Ratio [com.yihaomen.mybatis.dao.StudentMapper]: 0.5[Student{id1, name刘德华, age55, gendernull, teachersnull}, Student{id2, name张惠妹, age49, gendernull, teachersnull}, Student{id3, name谢霆锋, age35, gendernull, teachersnull}, Student{id4, name王菲, age47, gendernull, teachersnull}, Student{id5, name汪峰, age48, gendernull, teachersnull}, Student{id6, name章子怡, age36, gendernull, teachersnull}] Process finished with exit code 0   我们可以从结果看到sql只执行了一次证明我们的二级缓存生效了。   https://gitee.com/huayicompany/springmvc-mybatis 参考 [1]杨开振 著《深入浅出MyBatis技术原理与实战》 电子工业出版社2016.09 [2]博客http://blog.csdn.net/luanlouis/article/details/41280959 [3]博客http://www.cnblogs.com/QQParadise/articles/5109633.html [4]博客http://blog.csdn.net/isea533/article/details/44566257 转载于:https://www.cnblogs.com/happyflyingpig/p/7739749.html
http://www.pierceye.com/news/855330/

相关文章:

  • 网站建设如何把代码沈阳网站制作
  • 微网站自己怎么做的模版网站和语言网站
  • 做平台是做网站和微信小程序的好别京津冀协同发展国家战略
  • 北京怎样做企业网站电脑网页开发
  • 企业网站建设运营方案Wordpress hover插件
  • 做暧暖ox免费网站微信开店小程序怎么弄
  • 网站建站网站网站维护动画设计属于什么大类
  • 深圳宝安上市公司网站建设报价制作网站去哪家好
  • 沈阳做网站客户多吗网站地图抓取
  • 做网站比较专业的公司微信商城在哪里找
  • 网站建设开发的流程网站标题title怎么写
  • 网络营销的优势海宁网站怎么做seo
  • wordpress 英文主题南宁网站排名优化公司
  • 行业网站建设方案有专门做电商网站的CMS吗
  • 网站备案 快递公司变更流程
  • 简单的做图网站wordpress加密授权
  • 哪里做网站域名不用备案新华舆情监测平台
  • 品牌工厂网站建设qt 网站开发
  • xxx网站建设规划家庭服务网站的营销策略
  • 哪里可以做宝盈网站江门百度seo公司
  • 电子商务的网站建设名词解释如何建立官网
  • 网站建设维护外包群排名优化软件
  • 苏州专业建设网站镇江网站建设找思创网络
  • 长春网站排名提升seo关键词推广多少钱
  • 头条网站怎么做的在网站上放广告
  • 网站建设费的会计分录wordpress c博客
  • 网站开发语言字典使用apmserv本地搭建多个网站
  • 建网站费用记账北京时间网站建设
  • 兴化网站开发佛山营销网站建设联系方式
  • 安居客官网网站天津 网站设计制作公司