SpringBoot如何去除hibernate 集成EclipseLink

EclipseLink:EclipseLink持久平台由多个组件构成,主要有EclipseLink-ORM、EclipseLink-OXM、EclipseLink-SDO、 EclipseLink-DAS、EclipseLink-DBWS、EclipseLink-XR、EclipseLink-EIS。点击打开链接

持久化框架建议选用Eclipselink,Eclipselink是JPA的参考实现,同时提供了对于Oracle数据库的个性化支持。

SpringBoot已经集成hibernate,所以首先第一步,去除hibernate,添加EclipseLink

1.pom

2.7.0
2.7.0

  org.springframework.boot
  spring-boot-starter-data-jpa
  
  
    
      org.hibernate
      hibernate-core
    
    
      org.hibernate
      hibernate-entitymanager
    
  


  org.eclipse.persistence
  org.eclipse.persistence.jpa
  ${eclipselink.version}

 

插件(配置静态织入,日志级别等)

 


  com.ethlo.persistence.tools
  eclipselink-maven-plugin
  ${eclipselink-plugin.version}
  
    
      weave
      process-classes
      
        weave
      
    
    
      ddl
      process-classes
      
        ddl
      
    
    
      modelgen
      generate-sources
      
        modelgen
      
    
  
  
     -----修改包名------
    symphony.bina
    oracle
    FINE
  
  
    
      org.eclipse.persistence
      org.eclipse.persistence.jpa
      ${eclipselink.version}
    
  

2. 配置文件

package symphony.bina.config;

import java.util.HashMap;
import java.util.Map;
import javax.sql.DataSource;
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.boot.autoconfigure.transaction.TransactionManagerCustomizers;
import org.springframework.context.annotation.Configuration;
import org.springframework.orm.jpa.vendor.AbstractJpaVendorAdapter;
import org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter;
import org.springframework.transaction.jta.JtaTransactionManager;

/**
 * 配置eclipseLink.
 * 
  • WEAVING 设为静态织入
  • *
  • DDL 设为create-or-extend-tables
  • *
  • 日志级别 设为FINE 并且显示SQL参数
* * @author chenhao * @version 1.0.0 * @since 1.0.0 * * created at 2018/4/3 */ @Configuration public class CustomJpaConfiguration extends JpaBaseConfiguration { /** * 织入类型配置. */ public static final String WEAVING = "static"; /** * DDL配置. */ public static final String DDL = "create-or-extend-tables"; @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection") protected CustomJpaConfiguration(DataSource dataSource, JpaProperties properties, ObjectProvider jtaTransactionManager, ObjectProvider transactionManagerCustomizers) { super(dataSource, properties, jtaTransactionManager, transactionManagerCustomizers); } @Override protected AbstractJpaVendorAdapter createJpaVendorAdapter() { return new EclipseLinkJpaVendorAdapter(); } @Override protected Map getVendorProperties() { final Map map = new HashMap<>(); map.put("eclipselink.weaving", WEAVING); map.put("eclipselink.ddl-generation", DDL); return map; } }

 

3. 数据库链接

 

spring:
    datasource:
        url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8
        username: root
        password:
        driver-class-name: com.mysql.jdbc.Driver
    jpa:
      show-sql: true

设置成自己的数据库链接数据即可

4. 测试

package symphony.bina.config;

import static org.junit.Assert.*;

import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;


/**
 * 配置eclipseLink的测试类.
 *
 * @author chenhao
 * @version 1.0.0
 * @since 1.0.0
 *
 * created at 2018/4/3
 */
@SpringBootTest
@RunWith(SpringRunner.class)
public class CustomJpaConfigurationTest {

  /**
   * 注入{@link EntityManager}以测试eclipseLink配置是否正常.
   */
  @PersistenceContext
  EntityManager entityManager;


  @Test
  public void testForEclipselink() {

    assertNotNull(entityManager);
    Map properties = entityManager.getProperties();
    properties.forEach((k, v) -> {
      System.out.print(k);
      System.out.print(" ====> ");
      System.out.println(v);
    });

    String ddl = (String) properties.get("eclipselink.ddl-generation");
    assertNotNull(ddl);
    assertEquals(CustomJpaConfiguration.DDL, ddl);
    System.out.println("=====================");
    System.out.println("Eclipselink Test PASSED!");

  }


}

 

SpringBoot如何去除hibernate 集成EclipseLink_第1张图片

 

SpringBoot如何去除hibernate 集成EclipseLink_第2张图片

 

到此大功告成

参考:https://blog.csdn.net/cnhome/article/details/61923760

参考文档:

https://github.com/spring-projects/spring-data-examples/blob/master/jpa/eclipselink/README.adoc 

http://www.baeldung.com/spring-eclipselink Baeldung教程

http://www.eclipse.org/eclipselink/documentation/2.5/concepts/app_dev007.htm 官方文档关于Eclipselink的织入说明

 

你可能感兴趣的:(springBoot)