首页 技术 正文
技术 2022年11月12日
0 收藏 980 点赞 2,717 浏览 2488 个字

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);

}
}

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,083
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,558
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,407
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,180
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,816
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,899