日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

使用HazelCast进行休眠缓存:基本配置

發(fā)布時間:2023/12/3 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用HazelCast进行休眠缓存:基本配置 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

以前,我們對JPA緩存,機制以及hibernate提供的內容進行了介紹 。

接下來是一個使用Hazelcast作為二級緩存的休眠項目。

為此,我們將在JPA中使用一個基本的spring boot項目。 Spring Boot使用休眠作為默認的JPA提供程序。
我們的設置將非常接近上一篇文章 。
我們將docker與postgresql一起用于我們的sql數(shù)據(jù)庫。

group 'com.gkatzioura' version '1.0-SNAPSHOT'buildscript {repositories {mavenCentral()}dependencies {classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.1.RELEASE")} }apply plugin: 'java' apply plugin: 'idea' apply plugin: 'org.springframework.boot'repositories {mavenCentral() }dependencies {compile("org.springframework.boot:spring-boot-starter-web")compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa'compile group: 'org.postgresql', name:'postgresql', version:'9.4-1206-jdbc42'compile group: 'org.springframework', name: 'spring-jdbc'compile group: 'com.zaxxer', name: 'HikariCP', version: '2.6.0'compile group: 'com.hazelcast', name: 'hazelcast-hibernate5', version: '1.2'compile group: 'com.hazelcast', name: 'hazelcast', version: '3.7.5'testCompile group: 'junit', name: 'junit', version: '4.11' }

通過仔細檢查依賴關系,我們可以看到hikari池,postgresql驅動程序,spring數(shù)據(jù)jpa,當然還有hazelcast。

我們將通過利用Spring boot的數(shù)據(jù)庫初始化功能來自動化數(shù)據(jù)庫,而不是手動創(chuàng)建數(shù)據(jù)庫。

我們將在resources文件夾下創(chuàng)建一個名為schema.sql的文件。

create schema spring_data_jpa_example;create table spring_data_jpa_example.employee(id SERIAL PRIMARY KEY,firstname TEXT NOT NULL,lastname TEXT NOT NULL, email TEXT not null,age INT NOT NULL,salary real,unique(email) );insert into spring_data_jpa_example.employee (firstname,lastname,email,age,salary) values ('Test','Me','test@me.com',18,3000.23);

為了簡單起見,避免進行任何其他配置,我們將把數(shù)據(jù)源,jpa和緩存的配置放在application.yml文件中。

spring:datasource:continue-on-error: truetype: com.zaxxer.hikari.HikariDataSourceurl: jdbc:postgresql://172.17.0.2:5432/postgresdriver-class-name: org.postgresql.Driverusername: postgrespassword: postgreshikari:idle-timeout: 10000jpa:properties:hibernate:cache:use_second_level_cache: trueuse_query_cache: trueregion:factory_class: com.hazelcast.hibernate.HazelcastCacheRegionFactoryshow-sql: true

配置spring.datasource.continue-on-error至關重要,因為一旦應用程序重新啟動,就應該再次嘗試創(chuàng)建數(shù)據(jù)庫,因此崩潰是不可避免的。

任何休眠特定的屬性都駐留在spring.jpa.properties路徑中。 我們啟用了二級緩存和查詢緩存。

同樣,我們將show-sql設置為true。 這意味著,一旦查詢命中數(shù)據(jù)庫,就應通過控制臺對其進行記錄。

然后創(chuàng)建我們的員工實體。

package com.gkatzioura.hibernate.enitites;import javax.persistence.*;/*** Created by gkatzioura on 2/6/17.*/ @Entity @Table(name = "employee", schema="spring_data_jpa_example") public class Employee {@Id@Column(name = "id")@GeneratedValue(strategy = GenerationType.SEQUENCE)private Long id;@Column(name = "firstname")private String firstName;@Column(name = "lastname")private String lastname;@Column(name = "email")private String email;@Column(name = "age")private Integer age;@Column(name = "salary")private Integer salary;public Long getId() {return id;}public void setId(Long id) {this.id = id;}public String getFirstName() {return firstName;}public void setFirstName(String firstName) {this.firstName = firstName;}public String getLastname() {return lastname;}public void setLastname(String lastname) {this.lastname = lastname;}public String getEmail() {return email;}public void setEmail(String email) {this.email = email;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public Integer getSalary() {return salary;}public void setSalary(Integer salary) {this.salary = salary;} }

一切都已設置。 Spring Boot將檢測到該實體并自行創(chuàng)建一個EntityManagerFactory。 接下來是員工的存儲庫類。

package com.gkatzioura.hibernate.repository;import com.gkatzioura.hibernate.enitites.Employee; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.repository.CrudRepository;/*** Created by gkatzioura on 2/11/17.*/ public interface EmployeeRepository extends JpaRepository<Employee,Long> { }

最后一個是控制器

package com.gkatzioura.hibernate.controller;import com.gkatzioura.hibernate.enitites.Employee; import com.gkatzioura.hibernate.repository.EmployeeRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController;import java.util.List;/*** Created by gkatzioura on 2/6/17.*/ @RestController public class EmployeeController {@Autowiredprivate EmployeeRepository employeeRepository;@RequestMapping("/employee")public List<Employee> testIt() {return employeeRepository.findAll();}@RequestMapping("/employee/{employeeId}")public Employee getEmployee(@PathVariable Long employeeId) {return employeeRepository.findOne(employeeId);}}

一旦我們在http:// localhost:8080 / employee / 1發(fā)出請求

控制臺將顯示在數(shù)據(jù)庫發(fā)出的查詢

Hibernate: select employee0_.id as id1_0_0_, employee0_.age as age2_0_0_, employee0_.email as email3_0_0_, employee0_.firstname as firstnam4_0_0_, employee0_.lastname as lastname5_0_0_, employee0_.salary as salary6_0_0_ from spring_data_jpa_example.employee employee0_ where employee0_.id=?

第二次發(fā)出請求時,由于啟用了第二個緩存,因此不會在數(shù)據(jù)庫上發(fā)出查詢。 相反,應從二級緩存中獲取實體。

您可以從github下載該項目。

翻譯自: https://www.javacodegeeks.com/2017/02/hibernate-caching-hazelcast-basic-configuration.html

總結

以上是生活随笔為你收集整理的使用HazelCast进行休眠缓存:基本配置的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。