在理解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
插件
- 关上idea, File > Settings > Plugins > Ebean enhancer 来装置插件,如图所示:
- 启用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…
七、请关注我的公众号
发表回复