package com.oaec.mybatis.test;

import com.oaec.mybatis.dao.StudentDao;
import com.oaec.mybatis.entity.Student;
import com.oaec.mybatis.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.Test;

import java.util.HashSet;
import java.util.Set;

/**
* Created by admin on 2017-07-24.
*/
public class Test1 {
/*
* 缓存:视频缓存,音乐缓存,图片缓存
* mybaits中的缓存机制(一般来说是运行在内存中的,但是缓存不会无限制的扩大,当到了一定程度之后
* 缓存会按照一些策略,把一些数据写入到硬盘里):
* 缓存有什么作用:提高查询效率
* 把查询的结果放在缓存里,每次查询单的时候,先去缓存里面找一下,
* 如果有对应的结果,就直接从缓存中返还给用户,如果没有,再去数据库里查询。
*
* 在mybaits中,缓存分为两种,一种叫一级缓存(sqlSession级别的缓存),
* 第二种叫二级缓存(mapper级别的缓存)
* 一级缓存:在同一个sqlSession中,执行两次相同的sql语句,并且参数也相同的时候
* 第一次查询的结果会先写到缓存里,第二次查询的时候,先去缓存中找对应的结果,如果没有
* 再去数据库查询。
* 一级缓存是永远存在的,开发者无法关闭一级缓存。
* 说明:在同一个sqlSession中,只要是发生了任何的insert,update,delete操作
* 都会把一级缓存给清空。
*
* 一级缓存的实现机制:
*在SqlSession中存在一个HashMap<String,object>的一个集合
* 其中个的key就是你执行的sql的(statementIdnamespace+sqlId)+参数
* value就是你查询出来的对象。
*
*
* mybatis的二级缓存:
* 二级缓存,默认是不打开的,就是说可以不使用二级缓存,如果想使用的话
* 需要打开开关,开关位于myabtis的核心配置文件中的settings
* 有一个配置项叫做cacheEnabled,设置为true即可。
* 打开了总开关,还不够,需要给买一个mapper文件打开二级缓存的开关。
*
* 二级缓存的工作机制:
* 当你的二级缓存开关打开以后,sqlSession关闭之前,会把sqlSession中缓存的内容
* 写入到二级缓存。
* 想要实现二级缓存,对应缓存结果应该是实现了Serialiazable
*
*
*
* 集成第三方jar
* 由于缓存不是mybatis 的长项,所以运行集成第三方的缓存
* 其中ehcache是做的比较不错的一款产品。
* 想要在mybaits中集成echcache,需要两个jar
* 1、mybatis提供的于ehcache集成的jar
* 2、ehcache本身的jar
*
* 把相关的jar文件复制到lib下,然后打开ehcache-core-2.6.8.jar
* 去里面找到一个叫做ehcache-failsafe.xml文件,复制这个文件到src下
* 并且重命名为ehcache.xml,去掉里面的注释
*
* */
@Test
public void test1(){
SqlSession sqlSession=MybatisUtil.getFactory().openSession();
StudentDao dao=sqlSession.getMapper(StudentDao.class);
Student stu=dao.getStudentById(1);
System.out.println("stu:"+stu);
Student ns= new Student();
ns.setName("小黑");
ns.setAge(30);
dao.addStudent(ns);
System.out.println("--------------");
Student stu2=dao.getStudentById(1);
System.out.println("stu2:"+stu2);
}
@Test
public void test2(){
SqlSession sqlSession=MybatisUtil.getFactory().openSession();
StudentDao dao=sqlSession.getMapper(StudentDao.class);
Student stu=dao.getStudentById(1);
System.out.println("stu:"+stu);
//算起来SqlSession关闭,通过sqlSession获取的Dao的生命周期也结束了
sqlSession.close();
System.out.println("------------------");
//重新开启一个SqlSession
SqlSession sqlSession2=MybatisUtil.getFactory().openSession();
StudentDao dao2=sqlSession2.getMapper(StudentDao.class);
Student stu2=dao2.getStudentById(1);
System.out.println("stu2:"+stu2);
SqlSession sqlSession3=MybatisUtil.getFactory().openSession();
StudentDao dao3=sqlSession3.getMapper(StudentDao.class);
Student stu3=dao3.getStudentById(1);
System.out.println("stu3:"+stu3);

}
}

mybatis 中的缓冲的更多相关文章

  1. [原创]关于mybatis中一级缓存和二级缓存的简单介绍

    关于mybatis中一级缓存和二级缓存的简单介绍 mybatis的一级缓存: MyBatis会在表示会话的SqlSession对象中建立一个简单的缓存,将每次查询到的结果结果缓存起来,当下次查询的时候 ...

  2. 记录一次bug解决过程:mybatis中$和#的使用

    一.总结 mybatis中使用sqlMap进行sql查询时,经常需要动态传递参数.动态SQL是mybatis的强大特性之一,也是它优于其他ORM框架的一个重要原因.mybatis在对sql语句进行预编 ...

  3. mybatis中#{}与${}的差别(如何防止sql注入)

    默认情况下,使用#{}语法,MyBatis会产生PreparedStatement语句中,并且安全的设置PreparedStatement参数,这个过程中MyBatis会进行必要的安全检查和转义. # ...

  4. mybatis 中的where标签

    mybatis中的where标签可以去除 开头的 and 或者 or 但是放在后面的不行 失败的: <select id="countNotesByParam" parame ...

  5. Mybatis中SqlMapper配置的扩展与应用(3)

    隔了两周,首先回顾一下,在Mybatis中的SqlMapper配置文件中引入的几个扩展机制: 1.引入SQL配置函数,简化配置.屏蔽DB底层差异性 2.引入自定义命名空间,允许自定义语句级元素.脚本级 ...

  6. mybatis中使用使用模块化sql

    主要使用到mybatis中的标签 <sql id="tempId"> select * from student <sql> 使用的标签如下: <in ...

  7. “mybatis 中使用foreach 传

    为了帮助网友解决“mybatis 中使用foreach 传”相关的问题,中国学网通过互联网对“mybatis 中使用foreach 传”相关的解决方案进行了整理,用户详细问题包括:mybatismap ...

  8. Mybatis中的in查询和foreach标签

    Mybatis中的foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合. foreach元素的属性主要有 item,index,collection,open,separato ...

  9. mybatis中foreach的用法(转)

    foreach一共有三种类型,分别为List,[](array),Map三种. foreach属性 属性 描述 item 循环体中的具体对象.支持属性的点路径访问,如item.age,item.inf ...

随机推荐

  1. I/O多路复用之 epoll 详解

    1,epoll 原理(POLLIN,POLLOUT 状态): https://blog.csdn.net/hdutigerkin/article/details/7517390 https://blo ...

  2. 关于servelt的相关介绍

    1.@WebServlet注解的作用 在Servlet 3.0中,使用@WebServlet注解可实现servlet和url的映射,它告知容器哪些Servlet会提供服务以及额外信息,其作用相当于之前 ...

  3. 【收藏】ETH以太坊各个环境的公共的RPC服务!!!

    Choose a Network Use one of these endpoints as your Ethereum client provider or IPFS endpoint. NOTE: ...

  4. kivy sdl2 - ImportError: DLL load failed: 找不到指定的模块。

    kivy version : windows:win python version:3.6 sdl2 - ImportError: DLL load failed: 找不到指定的模块. 运行以下dem ...

  5. vue中v-model 与 v-bind:value

    之前一直认为,v-model相当于下方代码的语法糖,如下: <h1>{{inputValue}}</h1> <input type="text" :v ...

  6. supermap数据库型数据源的数据索引问题

    按如下方式查看帮助文档,根据不同的场景选用不同的索引

  7. matplotlib 初次编译无法运行

    终端 解决方案:vim ~/.matplotlib/matplotlibrc 输入backend: TkAgg 保存

  8. python实现bt种子 torrent转magnet

    Python实现bt转磁链  参考前人资料主要两种方式 1,利用python的bencode模块 2,安装libtorrent模块 尝试过两种方法特记录 环境:Windows系统  python 3 ...

  9. Linux 远程工具Screen 的应用

    挂断原理参考:https://www.ibm.com/developerworks/cn/linux/l-cn-screen/ 要求,python2 常用操作: 创建screen screen -L ...

  10. jmeter用Windows电脑分布式部署

    当然,java环境.jmeter安装我这里就不说了. 使用1个controller(imac电脑),2个agent(Windows7 系统) 一.agent配置(Windows7系统) 1.电脑环境变 ...