步骤:
引用hibernate的ehcache:hibernate-ehcache.jar 。
<!-- 启用二级缓存 --> <property name="cache.use_second_level_cache">true</property> <!-- 配置使用的二级缓存产品 --> <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property> <!-- 指定关联的 .hbm.xml文件 --> <mapping resource="com/shuoeasy/test/User.hbm.xml" /> <!-- 那些类使用二级缓存,并用什么类型的缓存策略 --> <class-cache usage="read-write" class="com.shuoeasy.test.User"/>
代码片段:
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>com.shuoeasy</groupId> <artifactId>test</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>test</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <!-- <version>5.2.0.Final</version> --> <version>4.3.11.Final</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>6.0.2</version> </dependency> <!-- 注意,用的是hibernate-c3p0 --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-c3p0</artifactId> <version>4.3.11.Final</version> </dependency> <!-- 注意,使用hibernate的ehcache插件 --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-ehcache</artifactId> <!-- <version>5.2.1.Final</version> --> <version>4.3.11.Final</version> </dependency> </dependencies> </project>
User.java:
package com.shuoeasy.test; public class User { private int id; private String userName; public User() { } public User(String userName) { super(); this.userName = userName; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } @Override public String toString() { return "User [id=" + id + ", userName=" + userName + "]"; } }
User.hbm.xml:
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Generated 2016-6-25 14:50:51 by Hibernate Tools 3.4.0.CR1 --> <hibernate-mapping> <class name="com.shuoeasy.test.User" table="USER" > <id name="id" type="int"> <column name="ID" /> <!-- 指定主键的生成方式,native:使用数据库本地方式 --> <generator class="native" /> </id> <property name="userName" type="java.lang.String"> <column name="USERNAME" /> </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://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- 连接数据库的基本信息 --> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">111111</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&useLegacyDatetimeCode=false&serverTimezone=Australia/Melbourne&</property> <!-- hibernate 的基本信息 --> <!-- hibernate 所使用的数据库方言 --> <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property> <!-- 执行操作时是否在控制台打印sql --> <property name="show_sql">true</property> <!-- 是否对sql进行格式化 --> <property name="format_sql">true</property> <!-- 指定自动生成数据表策略 --> <property name="hbm2ddl.auto">update</property> <!-- 启用二级缓存 --> <property name="cache.use_second_level_cache">true</property> <!-- 配置使用的二级缓存产品 --> <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property> <!-- 指定关联的 .hbm.xml文件 --> <mapping resource="com/shuoeasy/test/User.hbm.xml" /> <!-- 那些类使用二级缓存,并用什么类型的缓存策略 --> <class-cache usage="read-write" class="com.shuoeasy.test.User"/> </session-factory> </hibernate-configuration>
AppTest.java:
package com.shuoeasy.test; import java.util.Arrays; import java.util.List; import org.hibernate.Criteria; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; import org.hibernate.criterion.Conjunction; import org.hibernate.criterion.Disjunction; import org.hibernate.criterion.Order; import org.hibernate.criterion.Projections; import org.hibernate.criterion.Restrictions; import org.hibernate.service.ServiceRegistry; import org.junit.After; import org.junit.Before; import org.junit.Test; /** * Unit test for simple App. */ public class AppTest { Session session; SessionFactory sf; @Before public void init() { Configuration conf = new Configuration().configure(); ServiceRegistry sr = new StandardServiceRegistryBuilder().applySettings(conf.getProperties()).build(); sf = conf.buildSessionFactory((org.hibernate.service.ServiceRegistry) sr); session = sf.openSession(); session.beginTransaction(); System.out.println("init"); } @After public void destory() { session.getTransaction().commit(); session.close(); sf.close(); System.out.println("dectory"); } /** * 测试是否使用缓存 * hibernate.cfg.xml 加速配置<class-cache usage="read-write" class="com.shuoeasy.test.User"/> * 使用缓之后出现一条sql,不适用会出现两条sql。 */ @Test public void testGet(){ User user1 = (User) session.get(User.class, 1); System.out.println(user1); session.getTransaction().commit(); session.close(); session = sf.openSession(); session.beginTransaction(); User user2 = (User) session.get(User.class, 1); System.out.println(user2); } }