首先加入依赖到pom.xml:

		<!-- hibernnate对二级缓存的支持 -->
		<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-ehcache -->
		<dependency>
		    <groupId>org.hibernate</groupId>
		    <artifactId>hibernate-ehcache</artifactId>
		    <version>5.0.12.Final</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/net.sf.ehcache/ehcache -->
		<dependency>
		    <groupId>net.sf.ehcache</groupId>
		    <artifactId>ehcache</artifactId>
		    <version>2.10.6</version>
		</dependency>

然后在hibernate.xml配置文件中加入以下参数:

				<!-- hibernate5的二级缓存配置 -->
				<prop key="hibernate.cache.use_second_level_cache">true</prop>
				<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>

接着创建二级缓存配置文件ehcache.xml:

<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
    <diskStore path="java.io.tmpdir"/>
    <defaultCache
        maxElementsInMemory="10000"
        eternal="false"
        overflowToDisk="true"
        timeToIdleSeconds="120"
        timeToLiveSeconds="120"
        diskPersistent="false"/>
</ehcache>

最后在需要开启缓存的pojo类中加入注解cache注解:

@Entity
@GenericGenerator(name="idGenerator", strategy="uuid")
@Cache(usage=CacheConcurrencyStrategy.READ_WRITE)
public class Area {

	/**
	 * id:UUID生成法则
	 */
	@Id
	@GeneratedValue(generator="idGenerator")
	private String id;

配置完毕,启动项目,输出hibernate的sql执行语句,会发现第二次查询相同对象时不再执行查询语句。