Hibernate学习之二级缓存
© 版权声明:本文为博主原创文章,转载请注明出处
二级缓存
- 二级缓存又称“全局缓存”、“应用级缓存”
- 二级缓存中的数据可适用范围是当前应用的所有会话
- 二级缓存是可插拔式缓存,默认是EHCache
配置二级缓存步骤
- 添加二级缓存所需jar包
- 在hibernate.cfg.xml中开启二级缓存并配置二级缓存的提供类
- 添加二级缓存的属性配置文件
- 在需要被缓存的表所对应的映射文件中添加<cache/>标签
二级缓存数据
- 很少被修改的数据
- 不是很重要的数据,允许偶尔出现并发的数据
- 不会被高并发访问的数据
- 参考数据(eg.字典表等)
一二级缓存的对比
实例
1.项目结构
2.pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.proxy</groupId> <artifactId>Hibernate-Ehcache</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <hibernate.version>5.1.7.Final</hibernate.version> </properties> <dependencies> <!-- Junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!-- Hibernate --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>${hibernate.version}</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-ehcache</artifactId> <version>${hibernate.version}</version> </dependency> <!-- MySQL --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.42</version> </dependency> <!-- ehcache --> <!-- <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache</artifactId> <version>2.10.4</version> </dependency> --> </dependencies> </project>
3.Student.java
package org.hibernate.model; import java.util.Date; public class Student { private long id;// 学号 private String name;// 姓名 private Date birthday;// 生日 private String sex;// 性别 public Student() { } public Student(long id, String name, Date birthday, String sex) { this.id = id; this.name = name; this.birthday = birthday; this.sex = sex; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public long getId() { return id; } public void setId(long id) { this.id = id; } }
4.Student.hbm.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" > <hibernate-mapping> <class name="org.hibernate.model.Student" table="STUDENT"> <cache usage="read-only"/><!-- 该表加入二级缓存 --> <id name="id" type="java.lang.Long"> <column name="ID"/> <generator class="assigned"/> </id> <property name="name" type="java.lang.String"> <column name="NAME"/> </property> <property name="birthday" type="date"> <column name="BIRTHDAY"/> </property> <property name="sex" type="java.lang.String"> <column name="SEX"/> </property> </class> </hibernate-mapping>
5.hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <!-- 配置SessionFactory --> <session-factory> <!-- 配置数据库连接信息 --> <property name="connection.username">root</property> <property name="connection.password">***</property> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url"> jdbc:mysql:///hibernate?useSSL=true&characterEncoding=UTF-8 </property> <!-- 配置常用属性 --> <property name="hbm2ddl.auto">update</property><!-- 自动检查并创建表结构 --> <property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property><!-- 方言 --> <property name="show_sql">true</property><!-- 显示SQL语句 --> <!-- 开启二级缓存 --> <property name="hibernate.cache.use_second_level_cache">true</property> <!-- 指定二级缓存的提供类 --> <property name="hibernate.cache.region.factory_class"> org.hibernate.cache.ehcache.EhCacheRegionFactory </property> <!-- 引入映射文件 --> <mapping resource="hbm/Student.hbm.xml"/> </session-factory> </hibernate-configuration>
6.ehcache.xml
<ehcache> <diskStore path="java.io.tmpdir"/><!-- 缓存文件保存路径 --> <!-- 默认缓存设置 maxElementsInMemory:缓存的最大数量 eternal:设置元素是否是永久的。如果设置为true,则timeout失效 timeToIdleSeconds:设置元素过期前的空闲时间 timeToLiveSeconds:设置元素过期前的活动时间 overflowToDisk:当缓存中的数量达到限制后,是否保存到disk --> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true"/> <cache name="Student" maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="300" timeToLiveSeconds="600" overflowToDisk="true"/> </ehcache>
7.TestChcache.java
package org.hibernate.test; import java.util.Date; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.model.Student; import org.junit.After; import org.junit.Before; import org.junit.Test; public class TestChcache { private SessionFactory sessionFactory; private Session session; private Transaction transaction; @Before public void before() { sessionFactory = new Configuration().configure().buildSessionFactory();// 创建SessionFactory对象 session = sessionFactory.openSession();// 获取Session对象 transaction = session.beginTransaction();// 开启事务 } @After public void after() { transaction.commit();// 提交事务 session.close();// 关闭Session sessionFactory.close();// 关闭SessionFactory } @Test public void init() { Student student = new Student(1L, "张三", new Date(), "男"); session.save(student); student = new Student(2L, "李四", new Date(), "男"); session.save(student); } @Test public void testChcache() { Student student = session.get(Student.class, 1L); System.out.println(student.getName()); session = sessionFactory.openSession(); student = session.get(Student.class, 1L); System.out.println(student.getName()); } }
8.效果预览
8.1 首先执行init()方法,初始化表数据
8.2 屏蔽Student.hbm.xml中的<cache usage="read-only"/>,执行testChcache()方法
8.3 不屏蔽Student.hbm.xml中的<cache usage="read-only"/>,执行testChcache()方法
© 版权声明:本文为博主原创文章,转载请注明出处