在了解 Ebeam 框架之前,一直都在使用 JPA 作为 Spring Boot 的 ORM 框架。JPA 用起来比较简单的,对对象的增删改操作,几乎完全不需要接触 SQL 语句,更适合领域驱动设计的建模方法。但对一些非业务操作的技术处理和查询尤其是复杂查询的支持较弱,这也是有人选择 Mybatis 的重要原因。
Ebean ORM 框架,可以说几乎支持所有的 JPA 的功能同时也兼顾了 Mybatis 的灵活性,并且还有一些较实用的增加功能。本系列文章将一一介绍 Ebean 特有的较实用的功能。今天介绍 Ebean 的增强注解功能。
ebean 文档 https://ebean.io/docs/
一、数据库支持
Ebean 和 JPA 类似可支持多种数据库
引用至: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:
@PrePersistpublic void preCreate() { createTime = new Date(); updateTime = createTime;}
@PreUpdatepublic void preUpdate() { updateTime = new Date();}
复制代码
2. @DbComment("the name")
数据库字段注释
jpa :
@Column(columnDefinition = "int(11) DEFAULT NULL COMMENT '类型'")
复制代码
3. @DbArray
以数组形成存储数据,数据库字段为字符串,相当于架构自行做了格式转换。在数据量不太大的场景可以使用这种方式。
4. @DbJson
和 @DbArray 类似,以 json 类型存储,架构做格式转换
@Embeddablepublic 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 并运行。
并可以通过工具连接:
五、模型操作
1. 使用 Model 内联操作
使用 JPA 时,模型的增删改需要引入 Repository 来操作,Ebean 的模型直接承继系统的 Model 类,可实现内联操作
public class Customer extends Model { ...}
@Testpublic void create() {
Customer customer = Customer.builder() .name("hy") .phone("13812345678") .build(); customer.save();}
@Testpublic void update() { Customer customer = Customer.find.byId(1L); Optional.ofNullable(customer).ifPresent(o -> { o.setName(UUID.randomUUID().toString()); o.save(); });}
@Testpublic 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(); ...}
@Testpublic void find() { Customer customer = Customer.find.byId(1L);}
复制代码
内联操作从代码上看起来优雅了很多,也简化了代码。但在某种层面上也增加了入侵性,没有面向接口仓库实现方式解耦,当然你也可以选择使用 Repository 方式
六、综述
Ebean 还有很多 JPA 没有的高级功能,如历史记录、草稿、加密、复合查询、多数据支持、多租户等等功能,后续期待更新。
文中代码由于篇幅原因有一定省略并不是完整逻辑,如有兴趣请 Fork 源代码 https://gitee.com/hypier/barry-ebean/tree/master/ebean-section-1
七、请关注我的公众号
评论