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

<eclipselink.version>2.7.0</eclipselink.version>
<eclipselink-plugin.version>2.7.0</eclipselink-plugin.version>
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-jpa</artifactId>
  <!--排除Hibernate相关依赖-->
  <exclusions>
    <exclusion>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-core</artifactId>
    </exclusion>
    <exclusion>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-entitymanager</artifactId>
    </exclusion>
  </exclusions>
</dependency>
<dependency>
  <groupId>org.eclipse.persistence</groupId>
  <artifactId>org.eclipse.persistence.jpa</artifactId>
  <version>${eclipselink.version}</version>
</dependency>

 

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

 

<plugin>
  <groupId>com.ethlo.persistence.tools</groupId>
  <artifactId>eclipselink-maven-plugin</artifactId>
  <version>${eclipselink-plugin.version}</version>
  <executions>
    <execution>
      <id>weave</id>
      <phase>process-classes</phase>
      <goals>
        <goal>weave</goal>
      </goals>
    </execution>
    <execution>
      <id>ddl</id>
      <phase>process-classes</phase>
      <goals>
        <goal>ddl</goal>
      </goals>
    </execution>
    <execution>
      <id>modelgen</id>
      <phase>generate-sources</phase>
      <goals>
        <goal>modelgen</goal>
      </goals>
    </execution>
  </executions>
  <configuration>
     -----修改包名------
    <basePackage>symphony.bina</basePackage>
    <databaseProductName>oracle</databaseProductName>
    <logLevel>FINE</logLevel>
  </configuration>
  <dependencies>
    <dependency>
      <groupId>org.eclipse.persistence</groupId>
      <artifactId>org.eclipse.persistence.jpa</artifactId>
      <version>${eclipselink.version}</version>
    </dependency>
  </dependencies>
</plugin>

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.
 * <ul><li>WEAVING 设为静态织入</li>
 * <li>DDL 设为create-or-extend-tables</li>
 * <li>日志级别 设为FINE 并且显示SQL参数</li></ul>
 *
 * @author <a href="mailto:chenhao2010h@163.com">chenhao</a>
 * @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> jtaTransactionManager,
      ObjectProvider<TransactionManagerCustomizers> transactionManagerCustomizers) {
    super(dataSource, properties, jtaTransactionManager, transactionManagerCustomizers);
  }

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

  @Override
  protected Map<String, Object> getVendorProperties() {
    final Map<String, Object> 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 <a href="mailto:chenhao2010h@163.com">chenhao</a>
 * @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<String, Object> 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!");

  }


}

 

 

 

到此大功告成

参考: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的织入说明

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值