框架学习之JPA(五)

JPA是Java Persistence API的简称,中文名Java持久层API,是JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中。

Sun引入新的JPA ORM规范出于两个原因:其一,简化现有Java EE和Java SE应用开发工作;其二,Sun希望整合ORM技术,实现天下归一。

学习视频:尚硅谷框架jpa学习(有兴趣的同学留言邮箱)

使用软件:eclipse

Java版本:jdk8

本节目录

五、JPA_二级缓存

1.添加jar包

2.修改persistence.xml配置文件

3添加ehcache.xml配置文件

4.启动ehcache.xml配置文件,在persistence.xml配置文件中配置

5.需要缓存的类上添加注解

五、JPA_二级缓存

一级缓存:同一个entityManager中不用重复在数据库中查询同一个数据

二级缓存:

  • <shared-cache-mode> 节点:若 JPA 实现支持二级缓存,该节点可以配置在当前的持久化单元中是否启用二级缓存,可配置如下值:

    • ALL:所有的实体类都被缓存
    • NONE:所有的实体类都不被缓存.
    • ENABLE_SELECTIVE:标识 @Cacheable(true) 注解的实体类将被缓存
    • DISABLE_SELECTIVE:缓存除标识 @Cacheable(false) 以外的所有实体类
    • UNSPECIFIED:默认值,JPA 产品默认值将被使用

1.添加jar包

2.修改persistence.xml配置文件

  • 查看第四步,防止一的persistence.xml文件

3.添加ehcache.xml配置文件

  1. <ehcache>
  2. <!-- Sets the path to the directory where cache .data files are created.
  3.  
  4. If the path is a Java System Property it is replaced by
  5.  
  6. its value in the running VM.
  7.  
  8. The following properties are translated:
  9.  
  10. user.home - User's home directory
  11.  
  12. user.dir - User's current working directory
  13.  
  14. java.io.tmpdir - Default temp file path -->
  15.  
  16. <diskStore path="java.io.tmpdir"/>
  17.  
  18. <!--Default Cache configuration. These will applied to caches programmatically created through
  19.  
  20. the CacheManager.
  21.  
  22. The following attributes are required for defaultCache:
  23.  
  24. maxInMemory - Sets the maximum number of objects that will be created in memory
  25.  
  26. eternal - Sets whether elements are eternal. If eternal, timeouts are ignored and the element
  27.  
  28. is never expired.
  29.  
  30. timeToIdleSeconds - Sets the time to idle for an element before it expires. Is only used
  31.  
  32. if the element is not eternal. Idle time is now - last accessed time
  33.  
  34. timeToLiveSeconds - Sets the time to live for an element before it expires. Is only used
  35.  
  36. if the element is not eternal. TTL is now - creation time
  37.  
  38. overflowToDisk - Sets whether elements can overflow to disk when the in-memory cache
  39.  
  40. has reached the maxInMemory limit.
  41.  
  42. -->
  43.  
  44. <defaultCache
  45.  
  46. maxElementsInMemory="10000"
  47.  
  48. eternal="false"
  49.  
  50. timeToIdleSeconds="120"
  51.  
  52. timeToLiveSeconds="120"
  53.  
  54. overflowToDisk="true"
  55.  
  56. />
  57.  
  58. <!--Predefined caches. Add your cache configuration settings here.
  59.  
  60. If you do not have a configuration for your cache a WARNING will be issued when the
  61.  
  62. CacheManager starts
  63.  
  64. The following attributes are required for defaultCache:
  65.  
  66. name - Sets the name of the cache. This is used to identify the cache. It must be unique.
  67.  
  68. maxInMemory - Sets the maximum number of objects that will be created in memory
  69.  
  70. eternal - Sets whether elements are eternal. If eternal, timeouts are ignored and the element
  71.  
  72. is never expired.
  73.  
  74. timeToIdleSeconds - Sets the time to idle for an element before it expires. Is only used
  75.  
  76. if the element is not eternal. Idle time is now - last accessed time
  77.  
  78. timeToLiveSeconds - Sets the time to live for an element before it expires. Is only used
  79.  
  80. if the element is not eternal. TTL is now - creation time
  81.  
  82. overflowToDisk - Sets whether elements can overflow to disk when the in-memory cache
  83.  
  84. has reached the maxInMemory limit.
  85.  
  86. -->
  87.  
  88. <!-- Sample cache named sampleCache1
  89.  
  90. This cache contains a maximum in memory of 10000 elements, and will expire
  91.  
  92. an element if it is idle for more than 5 minutes and lives for more than
  93.  
  94. 10 minutes.
  95.  
  96. If there are more than 10000 elements it will overflow to the
  97.  
  98. disk cache, which in this configuration will go to wherever java.io.tmp is
  99.  
  100. defined on your system. On a standard Linux system this will be /tmp"
  101.  
  102. -->
  103.  
  104. <cache name="sampleCache1"
  105.  
  106. maxElementsInMemory="10000"
  107.  
  108. eternal="false"
  109.  
  110. timeToIdleSeconds="300"
  111.  
  112. timeToLiveSeconds="600"
  113.  
  114. overflowToDisk="true"
  115.  
  116. />
  117.  
  118. <!-- Sample cache named sampleCache2
  119.  
  120. This cache contains 1000 elements. Elements will always be held in memory.
  121.  
  122. They are not expired. -->
  123.  
  124. <cache name="sampleCache2"
  125.  
  126. maxElementsInMemory="1000"
  127.  
  128. eternal="true"
  129.  
  130. timeToIdleSeconds="0"
  131.  
  132. timeToLiveSeconds="0"
  133.  
  134. overflowToDisk="false"
  135.  
  136. /> -->
  137.  
  138. <!-- Place configuration for your caches following -->
  139.  
  140. </ehcache>

  

4.启动ehcache.xml配置文件,在persistence.xml配置文件中配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2.  
  3. <persistence version="2.0"
  4.  
  5. xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  6.  
  7. xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
  8.  
  9. <persistence-unit name="SGG-jpa" transaction-type="RESOURCE_LOCAL">
  10.  
  11. <!-- 配置使用什么 ORM 产品来作为 JPA 的实现 1. 实际上配置的是 javax.persistence.spi.PersistenceProvider
  12.  
  13. 接口的实现类 2. 若 JPA 项目中只有一个 JPA 的实现产品, 则也可以不配置该节点. -->
  14.  
  15. <provider>org.hibernate.ejb.HibernatePersistence</provider>
  16.  
  17. <!-- 添加持久化类 -->
  18.  
  19. <class>hue.edu.xiong.jpa.Customer</class>
  20.  
  21. <class>hue.edu.xiong.jpa.Order</class>
  22.  
  23. <class>hue.edu.xiong.jpa.Manager</class>
  24.  
  25. <class>hue.edu.xiong.jpa.Department</class>
  26.  
  27. <class>hue.edu.xiong.jpa.Item</class>
  28.  
  29. <class>hue.edu.xiong.jpa.Category</class>
  30.  
  31. <!-- 配置二级缓存的策略
  32.  
  33. ALL:所有的实体类都被缓存
  34.  
  35. NONE:所有的实体类都不被缓存.
  36.  
  37. ENABLE_SELECTIVE:标识 @Cacheable(true)注解的实体类将被缓存
  38.  
  39. DISABLE_SELECTIVE:缓存除标识 @Cacheable(false) 以外的所有实体类
  40.  
  41. UNSPECIFIED:默认值,JPA产品默认值将被使用
  42.  
  43. -->
  44.  
  45. <shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
  46.  
  47. <properties>
  48.  
  49. <!-- 连接数据库的基本信息 -->
  50.  
  51. <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
  52.  
  53. <property name="javax.persistence.jdbc.url" value="jdbc:mysql:///jpa" />
  54.  
  55. <property name="javax.persistence.jdbc.user" value="root" />
  56.  
  57. <property name="javax.persistence.jdbc.password" value="admin" />
  58.  
  59. <!-- 配置JPA实现产品的基本属性,配置hibernate -->
  60.  
  61. <property name="hibernate.format_sql" value="true" />
  62.  
  63. <property name="hibernate.show_sql" value="true" />
  64.  
  65. <property name="hibernate.hbm2ddl.auto" value="update" />
  66.  
  67. <!-- 二级缓存相关 -->
  68.  
  69. <property name="hibernate.cache.use_second_level_cache"
  70.  
  71. value="true" />
  72.  
  73. <property name="hibernate.cache.region.factory_class"
  74.  
  75. value="org.hibernate.cache.ehcache.EhCacheRegionFactory" />
  76.  
  77. <property name="hibernate.cache.use_query_cache" value="true" />
  78.  
  79. </properties>
  80.  
  81. </persistence-unit>
  82.  
  83. </persistence>

 

5.需要缓存的类上添加注解

测试案例:

  1. @Test
  2.  
  3. public void testSecondLevelCache(){
  4.  
  5. Customer customer1 = entityManager.find(Customer.class, 1);
  6.  
  7. transaction.commit();
  8.  
  9. entityManager.close();
  10.  
  11. entityManager = entityManagerFactory.createEntityManager();
  12.  
  13. transaction = entityManager.getTransaction();
  14.  
  15. transaction.begin();
  16.  
  17. Customer customer2 = entityManager.find(Customer.class, 1);
  18.  
  19. }

  

JPA学习(五、JPA_二级缓存)的更多相关文章

  1. Hibernate JPA 中配置Ehcache二级缓存

    在Hibernate3 JPA里配置了一下非分布式环境的二级缓存,效果不错.具体过程如下: 1, 需要引入的jar包 http://ehcache.org/downloads/catalog 下载的包 ...

  2. Hibernate基础学习(六)—Hibernate二级缓存

    一.概述      Session的缓存是一块内存空间,在这个内存空间存放了相互关联的Java对象,这个位于Session缓存内的对象也被称为持久化对象,Session负责根据持久化对象的状态来同步更 ...

  3. mybatis源码学习:一级缓存和二级缓存分析

    目录 零.一级缓存和二级缓存的流程 一级缓存总结 二级缓存总结 一.缓存接口Cache及其实现类 二.cache标签解析源码 三.CacheKey缓存项的key 四.二级缓存TransactionCa ...

  4. Hibernate学习笔记(六) — Hibernate的二级缓存

    我们知道hibernate的一级缓存是将数据缓存到了session中从而降低与数据库的交互.那么二级缓存呢? 一.应用场合 比方.在12306购票时.须要选择出发地与目的地,假设每点一次都与数据库交互 ...

  5. mybatis源码学习(三)-一级缓存二级缓存

    本文主要是个人学习mybatis缓存的学习笔记,主要有以下几个知识点 1.一级缓存配置信息 2.一级缓存源码学习笔记 3.二级缓存配置信息 4.二级缓存源码 5.一级缓存.二级缓存总结 1.一级缓存配 ...

  6. mybatis 学习五 二级缓存不推荐使用

    mybatis 二级缓存不推荐使用 一 mybatis的缓存使用. 大体就是首先根据你的sqlid,参数的信息自己算出一个key值,然后你查询的时候,会先把这个key值去缓存中找看有没有value,如 ...

  7. JPA学习笔记(11)——使用二级缓存

    一级缓存 查询两次id为1的user User user1 = entityManager.find(User.class, 1); User user2 = entityManager.find(U ...

  8. 【Java EE 学习 48】【Hibernate学习第五天】【抓取策略】【二级缓存】【HQL】

    一.抓取策略. 1.hibernate中提供了三种抓取策略. (1)连接抓取(Join Fetch):这种抓取方式是默认的抓取方式.使用这种抓取方式hibernate会在select中内连接的方式获取 ...

  9. Mybatis学习(五)————— 延迟加载和缓存机制(一级二级缓存)

    一.延迟加载 延迟加载就是懒加载,先去查询主表信息,如果用到从表的数据的话,再去查询从表的信息,也就是如果没用到从表的数据的话,就不查询从表的信息.所以这就是突出了懒这个特点.真是懒啊. Mybati ...

随机推荐

  1. tensorflow 2.0 技巧 | 自定义tf.keras.Model的坑

    自定义tf.keras.Model需要注意的点 model.save() subclass Model 是不能直接save的,save成.h5,但是能够save_weights,或者save_form ...

  2. 【VS开发】cmd dos 批处理重命名文件

    原文地址:http://hi.baidu.com/benchoi/item/c1f531f5f1367b0b85d2785b 批处理实现文件批量重命名并自动加递增序列号 有时我们想把一些图片批量重命名 ...

  3. Angular5 *ngIf 和 hidden 的区别

    问题 项目中遇到一个问题,有一个过滤查询的面板,需要通过一个展开折叠的button,来控制它的show 和 hide.这个面板中,有一个Select 组件,一个 input 查询输入框. 原来代码是: ...

  4. MySql数据库优化-汇总

    各位,不喜勿喷,和气生财- 数据库优化,是一种综合性的技术,不是通过某一种方式让数据库效率提高很多,而是通过各个方面的优化,来是数据库效率明显的稳步的提高. 主要包括以下: 1.库表的设计优化(三种范 ...

  5. C++中函数模板的概念和意义

    1,对泛型编程进行学习,泛型编程是实际工程开发中必用的技术,大型公司的通用 库都是采用泛型编程的技术完成的,C++ 中支持泛型编程技术,C++ 中的函数  模板和类模板就是 C++ 中泛型编程技术,本 ...

  6. div距离左边设置

    margin-right:不加负号, margin-left:必须加负号,理解为倒数 margin-left:-10px;

  7. Python进阶编程 面向对象

    一.面向对象 1.1面向对象的基本格式 class 类名: def 方法名(self): print(123) return 123 def 方法名(self): print(123) return ...

  8. 抖音很火的存钱计划,让python告诉你总共可以存到多少钱!

    抖音上有个很火的存钱计划,说是第一天存1块钱,第二天存2块钱,第三天存3块钱.....依此类推存365天,总共可以存到多少钱,我们现在用python告诉你怎么做: #定个初始存入金额 money = ...

  9. 使用ActiveMQ实现JMS消息通信服务

    PTP(点对点的消息模型) 在点对点模型中,相当于两个人打电话,两个人独享一条通信线路.一方发送消息,一方接收消息. 在p2p的模型中,双方通过队列交流,一个队列只有一个生产者和一个消费者. 1.建立 ...

  10. json格式和对象类型的转换20170330

    (1)对象的类型转换成字符串类型(或者更确切的说是json类型的) JSONObject jo = JSONObject.fromObject(map);常见的java代码转换成json 比如:后台J ...