关于spring:Ebean-ORM框架介绍1增强注解

5次阅读

共计 4714 个字符,预计需要花费 12 分钟才能阅读完成。

在理解 Ebeam 框架之前,始终都在应用 JPA 作为 Spring Boot 的 ORM 框架。JPA 用起来比较简单的,对对象的增删改操作,简直齐全不须要接触 SQL 语句,更适宜畛域驱动设计的建模办法。但对一些非业务操作的技术解决和查问尤其是简单查问的反对较弱,这也是有人抉择 Mybatis 的重要起因。

Ebean ORM 框架,能够说简直反对所有的 JPA 的性能同时也兼顾了 Mybatis 的灵活性,并且还有一些较实用的减少性能。本系列文章将一一介绍 Ebean 特有的较实用的性能。明天介绍 Ebean 的加强注解性能。

ebean 文档 https://ebean.io/docs/

一、数据库反对

Ebean 和 JPA 相似可反对多种数据库

Summary

Platform Identity DbArray DbJson UUID History
H2 *Identity & Sequence Partial Simulated Native Triggers
Postgres *Identity & Sequence Full Full Native Triggers
MySql Identity Simulated Full Triggers
SQL Server 17 *Sequence & Identity Simulated Full Native Native
SQL Server 16 *Identity Simulated Full Native
Oracle *Sequence & Identity Simulated Full Native
DB2 *Identity & Sequence None
SAP Hana
SQLite *Identity Partial Simulated None
ClickHouse
Cockroach
NuoDB

援用至:https://ebean.io/docs/database/

二、装置 ebean 插件

想要在 idea 应用调试代码必须要装置 Ebean enhancer 插件

  1. 关上 idea, File > Settings > Plugins > Ebean enhancer 来装置插件,如图所示:

  1. 启用 ebean 减少工具,确保 build 下的 “Ebean enhancer” 后面有一个勾

如果不勾运行的时候就可能报错

三、注解介绍

public abstract class BaseModel extends Model {

  @Id
  long id;

  @Version
  Long version;

  @WhenCreated
  Instant whenCreated;

  @WhenModified
  Instant whenModified;
}


public class Customer extends BaseModel {

  public enum Status {@EnumValue("N")
    NEW,
    @EnumValue("A")
    APPROVED,
    @EnumValue("S")
    SHIPPED,
    @EnumValue("C")
    COMPLETE,
    @EnumValue("F")
    FOO

  }

  @DbComment("the name")
  private String name;

  @DbArray
  @DbComment("the array")
  private List<UUID> uids = new ArrayList<>();

  @DbJson
  private SomeEmbedd some;

  @SoftDelete
  private boolean deleted;

  Status status;

  @DbMap(length = 800)
  Map<String, SomeEmbedd> map;
}

数据库格局:

1. @WhenCreated、@WhenModified

记录的创立工夫和最初批改工夫

jpa:

@PrePersist
public void preCreate() {createTime = new Date();
  updateTime = createTime;
}

@PreUpdate
public void preUpdate() {updateTime = new Date();
}

2. @DbComment(“the name”)

数据库字段正文

jpa:

 @Column(columnDefinition = "int(11) DEFAULT NULL COMMENT' 类型 '")

3. @DbArray

以数组造成存储数据,数据库字段为字符串,相当于架构自行做了格局转换。在数据量不太大的场景能够应用这种形式。

4. @DbJson

和 @DbArray 相似,以 json 类型存储,架构做格局转换

@Embeddable
public class SomeEmbedd {

  String one;
  String two;
  String three;
}

@Embeddable配合应用可将对象转为 JSON 类型保留在数据库中

5. @DbMap

和 @DbArray 相似,以 map 转为字符串存储,架构做格局转换

6. @SoftDelete

软删除,对架构实现此性能,并对架构提供的查问语法无效,这个性能还是挺实用的。

7. @EnumValue

枚举值映射

  public enum Status {@EnumValue("N")
    NEW,
    @EnumValue("A")
    APPROVED,
    @EnumValue("S")
    SHIPPED,
    @EnumValue("C")
    COMPLETE,
    @EnumValue("F")
    FOO
  }

Jpa:

@Enumerated(EnumType.STRING)
private Status customerStatus

jpa 应用 @Enumerated 注解可映射枚举值字符串或枚举索引值到数据库,如果想自定义须要写肯定的代码,而 @EnumValue配置起来较灵便

四、配置

### 1. maven 配置

<!-- Query bean support -->
<dependency>
    <groupId>io.ebean</groupId>
    <artifactId>ebean-querybean</artifactId>
    <version>${ebean.version}</version>
</dependency>

<!-- APT Query bean generation for Java -->
<dependency>
    <groupId>io.ebean</groupId>
    <artifactId>querybean-generator</artifactId>
    <version>${ebean.version}</version>
    <scope>provided</scope>
</dependency>


<!-- Test dependencies -->
<!-- includes docker test database container support  -->
<dependency>
    <groupId>io.ebean</groupId>
    <artifactId>ebean-test</artifactId>
    <version>${ebean.version}</version>
    <scope>test</scope>
</dependency>

...

<plugin>
  <groupId>io.repaint.maven</groupId>
  <artifactId>tiles-maven-plugin</artifactId>
  <version>2.18</version>
  <extensions>true</extensions>
  <configuration>
    <tiles>
      <!-- other tiles ... -->
      <tile>io.ebean.tile:enhancement:12.6.2</tile>
    </tiles>
  </configuration>
</plugin>

2. YAML 配置

正式 application.yaml 此处必须是 YAML

datasource:
  db:
    username: root
    password: 123456
    url: jdbc:mysql://./db_customer

单元测试 application.yaml

ebean:
  migration:
    run: run

  test:
    platform: mysql # h2, postgres, mysql, oracle, sqlserver, sqlite
    ddlMode: none # none | dropCreate | create | migration | createOnly | migrationDropCreate
    dbName: my_app
    mysql:
      version: 5.7
      containerName: ms55
      collation: utf8mb4_unicode_ci
      characterSet: utf8mb4

这个是个 docker 的数据库测试环境,只有本机有装置好 docker,进行单元测试时可主动创立 image 并运行。

并能够通过工具连贯:

username: {databaseName}
password: test
port: 4306
url: jdbc:mysql://localhost:{port}/{databaseName}
driver: com.mysql.jdbc.Driver
image: mysql:{version}

五、模型操作

1. 应用 Model 内联操作

应用 JPA 时,模型的增删改须要引入 Repository来操作,Ebean的模型间接继承零碎的 Model 类,可实现内联操作

public class Customer extends Model {...}

@Test
public void create() {Customer customer = Customer.builder()
    .name("hy")
    .phone("13812345678")
    .build();
  customer.save();}

@Test
public void update() {Customer customer = Customer.find.byId(1L);
  Optional.ofNullable(customer).ifPresent(o -> {o.setName(UUID.randomUUID().toString());
    o.save();});
}

@Test
public void delete() {Customer customer = Customer.find.byId(1L);
  System.err.println(customer);
  Optional.ofNullable(customer).ifPresent(o -> {o.delete();
  });
}

查问操作:

public class CustomerFinder extends Finder<Long, Customer> {public CustomerFinder() {super(Customer.class);
  }
}

public class Customer extends Model {public static final CustomerFinder find = new CustomerFinder();
  ...
}

@Test
public void find() {Customer customer = Customer.find.byId(1L);
}

内联操作从代码上看起来优雅了很多,也简化了代码。但在某种层面上也减少了入侵性,没有面向接口仓库实现形式解耦,当然你也能够抉择应用 Repository 形式

六、综述

Ebean 还有很多 JPA 没有的高级性能,如历史记录、草稿、加密、复合查问、多数据反对、多租户等等性能,后续期待更新。

文中代码因为篇幅起因有肯定省略并不是残缺逻辑,如有趣味请 Fork 源代码 https://gitee.com/hypier/barr…

七、请关注我的公众号

正文完
 0