之前我们通过了关于 JPA 缓存、机制和 hibernate 提供的内容的介绍。
接下来是一个使用 Hazelcast 作为二级缓存的休眠项目。
为此,我们将使用带有 JPA 的基本 Spring Boot 项目。 Spring boot 使用 hibernate 作为默认的 JPA 提供者。我们的设置将与之前的帖子 中的设置非常接近。我们将使用带有 docker 的 postgresql 作为我们的 sql 数据库。
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 data jpa,当然还有 hazelcast。
我们将通过利用 Spring boot 的数据库初始化功能 来自动创建数据库,而不是手动创建数据库。
我们将在资源文件夹下创建一个名为 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);
为了保持简单并避免任何进一步的配置,我们应将数据源、jpa 和缓存的配置放在 application.yml 文件中。
spring: datasource: continue-on-error: true type: com.zaxxer.hikari.HikariDataSource url: jdbc:postgresql://172.17.0.2:5432/postgres driver-class-name: org.postgresql.Driver username: postgres password: postgres hikari: idle-timeout: 10000 jpa: properties: hibernate: cache: use_second_level_cache: true use_query_cache: true region: factory_class: com.hazelcast.hibernate.HazelcastCacheRegionFactory show-sql: true
配置 spring.datasource.continue-on-error 是至关重要的,因为一旦应用程序重新启动,应该有第二次尝试创建数据库,因此崩溃是不可避免的。
任何特定于 hibernate 的属性都位于 spring.jpa.properties 路径中。我们启用了二级缓存和查询缓存。
我们还将 show-sql 设置为 true。这意味着一旦查询命中数据库,它将通过控制台记录下来。
然后创建我们的员工实体。
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 将检测实体并自行创建一个 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 { @Autowired private 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 发出请求
控制台将显示在数据库发出的查询
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=?
我们第二次发出请求,因为我们启用了第二个缓存,所以不会对数据库发出查询。相反,实体应从二级缓存中获取。
您可以从 github 下载该项目。
标签2: Java教程地址:https://www.cundage.com/article/jcg-hibernate-caching-hazelcast-basic-configuration.html