大家好,又见面了,我是你们的朋友全栈君。
一、环境的搭建
请参考EHcache环境的配置
二、实体类(Student.java)
package com.hibernate.beans;
public class Student {
private Integer id;
private String name;
private int age;
private double score;
public Student() {
super();
}
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
public Integer getId() {
return id;
}
public Student(String name, int age, double score) {
super();
this.name = name;
this.age = age;
this.score = score;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", age=" + age
+ ", score=" + score + "]";
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
}
三、映射文件(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 package="com.hibernate.beans">
<class name="Student" table="t_student">
<id name="id" column="t_id">
<!-- 主键生成策略 -->
<generator class="native"></generator>
</id>
<property name="name" column="t_name"></property>
<property name="age" column="t_age"></property>
<property name="score" column="t_score"></property>
</class>
</hibernate-mapping>
四、主配置文件(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>
<session-factory>
<!--DB连接四要素 Ctrl shift C -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF8</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123456</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 数据源:数据库连接池 -->
<property name="hibernate.connection.provider_class">org.hibernate.c3p0.internal.C3P0ConnectionProvider</property>
<!-- 当前session的上下文 保证在同一线程中获取到的是同一个session-->
<property name="hibernate.current_session_context_class">thread</property>
<!-- 自动建表-->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 显示SQL -->
<property name="hibernate.show_sql">true</property>
<!-- 格式化SQL -->
<property name="hibernate.format_sql">true</property>
<!-- 开启二级缓存 -->
<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="com/hibernate/beans/Student.hbm.xml"/>
<!-- 配置缓存类 -->
<class-cache usage="read-only" class="com.hibernate.beans.Student"/>
<!-- 配置缓存集合 <collection-cache usage="read-only" collection="com.hibernate.beans.Student.name"/> -->
</session-factory>
</hibernate-configuration>
五、测试代码
------
public void testEHcache(){
//获取session
Session session = HbnUtils.getSession();
try {
//开启事务
session.beginTransaction();
//第一次执行get操作
Student student = session.get(Student.class, 1);
System.out.println(student);
//第二次执行get操作
Student student1 = session.get(Student.class, 1);
System.out.println(student1);
//清除session缓存
session.clear();
//第三次执行get操作
Student student2 = session.get(Student.class, 1);
System.out.println(student2);
//提交事务
session.getTransaction().commit();
} catch (Exception e) {
//回滚事务
session.getTransaction().rollback();
}
}
结果
select
student0_.t_id as t_id1_0_0_,
student0_.t_name as t_name2_0_0_,
student0_.t_age as t_age3_0_0_,
student0_.t_score as t_score4_0_0_
from
t_student student0_
where
student0_.t_id=?
Student [id=1, name=李四, age=20, score=90.0]
Student [id=1, name=李四, age=20, score=90.0]
Student [id=1, name=李四, age=20, score=90.0]
分析
日志上的sql语句是第一次查询出现了,
第一次输出的详情是从数据库中加载来的,
第二次输出的详情是从session缓存中读取的,
然后session缓存被清空,
第三次输出的详情就只能是从二级缓存中读取的了
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/142426.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...