Mybatis一级缓存与二级缓存
程序员文章站
2022-07-07 20:11:46
测试package cn.ps.lseeon04.check;import java.io.InputStream;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import org.apache.ibatis.session.SqlSessi......
测试
package cn.ps.lseeon04.check;
import java.io.InputStream;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
/**
* 1.解决sql硬编码的问题
* 2.解决面向对象面向问题 每一个对象一个xml文件(映射文件 MAPPING)
*
*/
public class TestMybatis {
// 获取session对象
public static SqlSession getSeesion() {
// 获取config.xml文件
String resources = "config.xml";
// 读取config.xml文件
InputStream resourceAsStream = TestMybatis.class.getResourceAsStream(resources);
// sessopm工厂负责生产回话
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
// 回话就是跟数据库产生的连接可以操作数据库的增删改查
SqlSession openSession = sqlSessionFactory.openSession();
return openSession;
}
/**
* 一级缓存: 会有内存溢出导致服务器奔溃 同一个seesion对象调用多次查询相同主键获取的数据发起一次sql语句
*/
@Test
public void testCheck() {
SqlSession seesion = getSeesion();
EmpMapper mapper = seesion.getMapper(EmpMapper.class);
Emp queryEmp = mapper.queryEmp("7521");
Emp queryEmp2 = mapper.queryEmp("7521");
System.out.println(queryEmp == queryEmp2);
}
/**
* 二级缓存:数据存在redis里面数据库不会奔溃 同一个SqlSessiontory 不同的session对象
*/
@Test
public void testSecondCheck() {
String resources = "config.xml";
InputStream resourceAsStream = TestMybatis.class.getResourceAsStream(resources);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
SqlSession openSession = sqlSessionFactory.openSession();
SqlSession openSession1 = sqlSessionFactory.openSession();
EmpMapper mapper = openSession.getMapper(EmpMapper.class);
Emp queryEmp = mapper.queryEmp("7521");
openSession.close();
EmpMapper mapper1 = openSession1.getMapper(EmpMapper.class);
Emp queryEmp1 = mapper1.queryEmp("7521");
System.out.println(queryEmp==queryEmp1);
}
}
实体类
package cn.ps.lseeon04.check;
import java.io.Serializable;
import java.util.List;
public class Emp implements Serializable{
private String empno;
private String ename;
private int sal;
private List empList;
public List getEmpList() {
return empList;
}
public void setEmpList(List empList) {
this.empList = empList;
}
public String getEmpno() {
return empno;
}
public void setEmpno(String empno) {
this.empno = empno;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public int getSal() {
return sal;
}
public void setSal(int sal) {
this.sal = sal;
}
@Override
public String toString() {
return "Emp [empno=" + empno + ", ename=" + ename + ", sal=" + sal + "]";
}
}
接口
package cn.ps.lseeon04.check;
public interface EmpMapper {
public Emp queryEmp(String empno);
}
config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<properties resource="jdbc.properties"></properties>
<!-- 控制台书库日志 -->
<settings>
<setting name="logImpl" value="STDOUT_LOGGING" />
</settings>
<!-- 给返回类型取别名 -->
<typeAliases>
<typeAlias type="cn.ps.lesson02.selectKey.User" alias="user" />
</typeAliases>
<plugins>
<!-- com.github.pagehelper为PageHelper类所在包名 -->
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<!-- 使用下面的方式配置参数,后面会有所有的参数介绍 -->
<property name="supportMethodsArguments" value="true" />
</plugin>
</plugins>
<!-- 连接数据库 -->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${driverClassName}" />
<property name="url" value="${url}" />
<property name="username" value="${userNames}" />
<property name="password" value="${password}" />
</dataSource>
</environment>
</environments>
<!-- 扫描EmpMapper.xml文件 -->
<mappers>
<mapper resource="cn/ps/lseeon04/check/EmpMapper.xml"></mapper>
</mappers>
</configuration>
EmpMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.ps.lseeon04.check.EmpMapper">
<!--
eviction:缓存的回收策略 FIFO:先进先出,按对象进入缓存的顺序来移除它们
flushInterval:缓存刷新间隔,缓存多长时间清空一次,默认不清空,设置一个毫秒值
size:缓存存放多少个元素
type:指定自定义缓存的全类名(实现Cache接口即可)
-->
<cache eviction="FIFO" flushInterval="6000" size="512" type="org.mybatis.caches.redis.RedisCache"></cache>
<select id="queryEmp" resultType="cn.ps.lseeon04.check.Emp">
SELECT * FROM emp where empno = ${0}
</select>
</mapper>
本文地址:https://blog.csdn.net/weixin_43728813/article/details/85982123