在了解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: 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("") .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/barry-ebean/tree/master/ebean-section-1
七、请关注我的公众号


版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/134873.html