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">
        <!-- 
        配置使用什么 ORM 产品来作为 JPA 的实现 
        1. 实际上配置的是  javax.persistence.spi.PersistenceProvider 接口的实现类
        2. 若 JPA 项目中只有一个 JPA 的实现产品, 则也可以不配置该节点. 
        -->
        <provider>org.hibernate.ejb.HibernatePersistence</provider>

        <!-- 添加持久化类 -->
        <class>com.bart.jpa.beans.Customer</class>
        ....


        <!-- 
        该标签必须放在持久化类的标签之后
        配置二级缓存的策略 
        ALL:所有的实体类都被缓存
        NONE:所有的实体类都不被缓存. 
        ENABLE_SELECTIVE:标识 @Cacheable(true) 注解的实体类将被缓存
        DISABLE_SELECTIVE:缓存除标识 @Cacheable(false) 以外的所有实体类
        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://127.0.0.1:3306/jpa"/>
            <property name="javax.persistence.jdbc.user" value="root"/>
            <property name="javax.persistence.jdbc.password" value="123456"/>

            <!-- 配置 JPA 实现产品的基本属性. 配置 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)标签
这样就配置好JPA的二级缓存

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Spring Boot 中,关闭二级缓存可以通过配置文件或者编程的方式实现。下面是两种常用的方法: 1. 通过配置文件关闭二级缓存: 在 `application.properties` 或者 `application.yml` 配置文件中添加以下配置: ```properties spring.jpa.properties.hibernate.cache.use_second_level_cache=false ``` 或者 ```yaml spring: jpa: properties: hibernate: cache: use_second_level_cache: false ``` 这样配置会禁用 Hibernate 的二级缓存。 2. 编程方式关闭二级缓存: 在你的配置类中注入 `EntityManagerFactory` 对象,并调用 `setSharedCacheMode` 方法将二级缓存模式设置为 `DISABLE_SELECTIVE`,示例如下: ```java import org.hibernate.cache.spi.CachingProvider; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import javax.persistence.EntityManagerFactory; import javax.persistence.PersistenceUnit; @Configuration public class CacheConfig { @PersistenceUnit private EntityManagerFactory entityManagerFactory; @Autowired public void configureCache() { CachingProvider cachingProvider = entityManagerFactory.getCache().getEntityManagerFactory().getCache().getCachingProvider(); cachingProvider.getCacheManager().getCache("your_cache_name").unwrap(javax.cache.Cache.class).getConfiguration().setReadThrough(false); } } ``` 将 `"your_cache_name"` 替换为你实际使用的缓存名称。 以上是关闭二级缓存的两种常用方法,你可以根据实际情况选择其中一种进行配置。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值