JPA-二级缓存

persistence.xml配置如下

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    <persistence-unit name="jpa-1" transaction-type="RESOURCE_LOCAL">
        <!-- 配置使用什么产品来实现JPA 
        1.实际上配置的是 javax.persistence.spi.PersistenceProvider接口的实现类
        2.若JPA项目中只有一个JPA的实现产品,则可以不配置该节点 -->
        <provider>org.hibernate.ejb.HibernatePersistence</provider>

        <!-- 添加持久化类 -->
        <class>cn.bdqn.entity.Customer</class>
        <class>cn.bdqn.entity.Order</class>
        <class>cn.bdqn.entity.Department</class>
        <class>cn.bdqn.entity.Manager</class>
        <class>cn.bdqn.entity.Category</class>
        <class>cn.bdqn.entity.Item</class>
        
        <!--   
                        配置二级缓存的策略
           ALL:所有的实体类都被缓存
           NONE:所有的实体类都不被缓存
           ENABLE_SELECTIVE:标识@Cacheable(true)注解的实体类被缓存
           DISABLE_SELECTIVE:缓存除标识@DISABLE_SELECTIVE以外的所有实体类
           UNSPECIFIED:默认值 JPA产品默认值将被使用
         -->
        <shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
        
        <properties>
            <!-- 连接数据库的基本信息 -->
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql:///jpa" />
            <property name="javax.persistence.jdbc.user" value="root"></property>
            <property name="javax.persistence.jdbc.password" value="123456" />
            <!-- 配置hibernate的基本属性 -->
            <property name="hibernate.format_sql" value="true" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.hbm2ddl.auto" value="update" />
            <!-- 二级缓存相关 -->
            <!-- 启用二级缓存 -->
            <property name="hibernate.cache.use_second_level_cache" value="true"/>
            <!-- 配置二级缓存的是此案产品 -->
            <property name="hibernate.cache.region.factory_class" value="org.hibernate.cache.ehcache.EhCacheRegionFactory"/>
            <!-- 配置启用查询缓存 -->
            <property name="hibernate.cache.use_query_cache" value="true"/>
        </properties>
    </persistence-unit>
</persistence>
在需要被缓存的实体类加上@Cacheable(true)

@Table(name="customers") 
@Entity
@Cacheable(true)
public class Customer {
@Test

  @Test
    public void SecondLevelCache(){
        Customer customer1 = entityManager.find(Customer.class, 1);
        transaction.commit();
        entityManager.close();
        
        entityManager = entityManagerFactory.createEntityManager();
        transaction = entityManager.getTransaction();
        transaction.begin();
        Customer customer2 = entityManager.find(Customer.class, 1);
    }

只会发送一条SQL语句


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值