Spring Boot Orm 简明教程

Spring Boot 使用 HibernateJpaAutoConfiguration ,默认情况下,其配置 Hibernate 实现。为了切换到 EclipseLink,我们需要创建一个自定义配置类,该类将扩展 JpaBaseConfiguration 类。JpaBaseConfiguration 是用于扩展和配置适用于任何 ORM 实现的 JPA 的基类。以下是 EclipsLinkJpaConfiguration 的代码。

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;

@Configuration
public class EclipsLinkJpaConfiguration extends JpaBaseConfiguration {

   protected EclipsLinkJpaConfiguration(DataSource dataSource, JpaProperties properties,
      ObjectProvider<JtaTransactionManager> jtaTransactionManager) {
      super(dataSource, properties, jtaTransactionManager);
   }

   @Override
   protected AbstractJpaVendorAdapter createJpaVendorAdapter() {
      return new EclipseLinkJpaVendorAdapter();
   }

   @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;
   }

   @Bean
   public static DataSource dataSource() {
      final DriverManagerDataSource dataSource = new DriverManagerDataSource();
      dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
      dataSource.setUrl("jdbc:mysql://localhost:3306/tutorialspoint?useSSL=false&allowPublicKeyRetrieval=true");
      dataSource.setUsername("root");
      dataSource.setPassword("root@123");
      return dataSource;
   }
}

我们分别使用 createJpaVendorAdapter()、dataSource() 和 getVendorProperties() 方法添加了 Adapter、DataSource和 Properties。

同时更新实体,使用 Integer 代替 int。

package com.tutorialspoint.springbootorm.entity;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Employee {

   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   private Integer id;
   private String name;
   private Integer age;
   private String email;

   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;
   }
}