- Spring Boot ORM 教程
- Spring Boot ORM - 主頁
- Spring Boot ORM - 概述
- 環境設定
- Spring Boot ORM - JPA
- Spring Boot ORM 和 Spring Data JPA
- Spring Boot ORM - 建立專案
- Application.properties
- Spring Boot ORM - 更新專案
- Spring Boot ORM - 測試 Hibernate
- Spring Boot ORM 和 EclipseLink
- Maven EclipseLink
- 更新 EclipseLink 專案
- Spring Boot ORM - 測試 EclipseLink
- Spring Boot ORM 有用資源
- Spring Boot ORM - 快速指南
- Spring Boot ORM - 有用資源
- Spring Boot ORM - 討論
Spring Boot ORM - 更新 EclipseLink 專案
Spring Boot 使用 HibernateJpaAutoConfiguration,它預設配置 hibernate 實現。為了切換到 EclipseLink,我們需要建立一個自定義配置類,它將擴充套件 JpaBaseConfiguration 類。JpaBaseConfiguration 是用於擴充套件和配置針對任何 ORM 實現的 JPA 的基類。以下是 EclipsLinkJpaConfiguration 的程式碼。
JPA 配置
建立 EclipseLink 配置類。
EclipsLinkJpaConfiguration.java
package com.tutorialspoint.springbootorm;
import java.util.HashMap;
import java.util.Map;
import javax.sql.DataSource;
import org.eclipse.persistence.config.PersistenceUnitProperties;
import org.eclipse.persistence.logging.SessionLog;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.vendor.AbstractJpaVendorAdapter;
import org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter;
import org.springframework.transaction.jta.JtaTransactionManager;
// EclipseLink Specific Configuration Class
@Configuration
public class EclipsLinkJpaConfiguration extends JpaBaseConfiguration {
protected EclipsLinkJpaConfiguration(DataSource dataSource, JpaProperties properties,
ObjectProvider<JtaTransactionManager> jtaTransactionManager) {
super(dataSource, properties, jtaTransactionManager);
}
// EclipseLink JPA Adaptor
@Override
protected AbstractJpaVendorAdapter createJpaVendorAdapter() {
return new EclipseLinkJpaVendorAdapter();
}
// EclipseLink Properties
@Override
protected Map<String, Object> getVendorProperties() {
Map<String, Object> map = new HashMap<>();
map.put(PersistenceUnitProperties.WEAVING, "false");
map.put(PersistenceUnitProperties.LOGGING_LEVEL, SessionLog.FINER_LABEL);
map.put(PersistenceUnitProperties.DDL_GENERATION, PersistenceUnitProperties.CREATE_ONLY);
map.put(PersistenceUnitProperties.LOGGING_LEVEL, SessionLog.FINER_LABEL);
return map;
}
// Database Connection properties setup
@Bean
public static DataSource dataSource() {
final DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://:3306/tutorialspoint");
dataSource.setUsername("root");
dataSource.setPassword("root@123");
return dataSource;
}
}
我們分別使用 createJpaVendorAdapter()、dataSource() 和 getVendorProperties() 方法添加了介面卡、資料來源和屬性。
更新實體
還需要更新實體,用 Integer 代替 int。
Employee.java
package com.tutorialspoint.springbootorm.entity;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
// entity class to persist object to Employee Table
@Entity
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String name;
private Integer age;
private String email;
// setter, getter methods
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
廣告