JHipster Domain Language (JDL)
本文翻译自官网:https://jhipster.github.io/jdl/
JHipster Domain Language (JDL)
JDL是JHipster的领域特殊语言(specific domain language).这种语言的目的是为了使用简单和友好的语法在一个文件(也可以是多个文件)里描述实体以及他们的关系。
这些工具是使用实体子生成器的替代。设计这些工具的理念是使用可视化工具总是比使用经典Yeoman问答容易管理实体间的关系。
(如果你喜欢这个项目,别忘了点星在github上。这句就不仔细翻译了。。)
- JDL Sample
- How to use it
- The language
3.1 Entity Declaration
3.2 Relationship Declaration
3.3 Enumerations
3.4 Blobs
3.5 Option declaration
3.6 Microservice-related options - Commenting
- All the relationships
- Constants
- Annexes
- Issues and bugs
JDL Sample
How to use it
如果你想使用JHipster UML而不是import-jdl
子生成器,你需要安装这个工具,命令是运行npm install -g jhipster-uml
。
- 使用扩展名 ‘.jh’或者’.jdl’创建一个文件
- 使用JDL-STudio声明你的实体和关系,然后从这个工具里下载你的文件
- 在JHipster生成的应用文件夹中,运行命令
yo jhipster:import-jdl my_file.jdl
或者jhipster-uml my_file.jdl
如果你想把这些用在自己的项目里,你可以添加这个依赖在本地,命令为npm install jhipster-core --save
。然后保存在你的package.json
文件中。
The language
Entity declaration
entity <entity name> {<field name> <type> [<validation>*]
}
支持的类型以及校验如链接https://jhipster.github.io/jdl/#annexes。如果校验需要一个值,在校验后面简单增加(<value>)
即可。
entity A
entity B
entity C {}
entity D {name String required,address String required maxlength(100),age Integer required min(18)
}
因为JDL设计理念是简单使用和简单可读,如果你的实体是空的(没有字段),你可以直接声明一个实体使用entity A
或者 entity A {}
.
注意:JHipster会增加一个默认的id
字段,所以你不用担心这个字段。
Relationship declaration
relationship (OneToMany | ManyToOne | OneToOne | ManyToMany) {<from entity>[{<relationship name>}] to <to entity>[{<relationship name>}]
}
(OneToMany | ManyToOne| OneToOne | ManyToMany)
是关系的类型<from entity>
是拥有此关系的实体拥有者:the source<to entity>
是这个关系连接到的实体名字:the destination<relationship name>
is the name of the field having the other end as type(不会翻、、请大侠赐教,个人理解这个就是对应关系的一个名字,这个名字可以任意起的,这个名字后面加小括号指定这个关系指定的字段)required
注入的字段是否是必须的。
下面是一些简单的例子:
一个BOOK有一个,必须的,Author,一个Author有几本Books。
entity Book
entity Authorrelationship OneToMany {Author{book} to Book{writer(name) required}
}
当然,在实际例子中,你会有一大堆关系,总是写同样的三行会很无聊。所以你可以这么声明:
entity A
entity B
entity C
entity Drelationship OneToOne {A{b} to B{a},B{c} to C
}
relationship ManyToMany {A{d} to D{a},C{d} to D{c}
}
默认,连接使用id字段,但是你可以连接使用其他字段,所以你可能会这么写
entity A {name String required
}
entity Brelationship OneToOne {A{b} to B{a(name)}
}
Enumerations
- 在文件中声明一个枚举:
enum Language { FRENCH, ENGLISH, SPANISH }
- 在一个实体中,增加一个字段用枚举作为类型::
entity Book { title String required, description String, language Language }
Blob (byte[])
JHipster给予你很大的自由,可以选择一个图片类型或者任何二进制类型。JDL让你一样得到这些东西:仅仅使用编辑器创建一个惯例类型(见DataType),使用下面的约定来命名:
AnyBlob
或者仅仅Blob
去创建一个任意二进制类型ImageBlob
创建一个图片类型的字段ImageBlob
创建一个CLOB的字段(long text)
Option declaration
在JHipster,你可以为你的实体指定特殊选项,例如分页或者DTO。你可以使用JDL做同样的事:
entity A {name String required
}entity B {}entity C {}dto A, B with mapstructpaginate A, C with infinite-scroll
paginate B with pagerservice A with serviceClass
service C with serviceImpl
语法中的关键字dto
, paginate
, service
及 with
就是为了支持这些特性。如果错误的选项被指定,JDL会报红色错误信息,并且会忽略这个选项,以保证不损坏JHipster的JSON文件
Service option
如果没有指定services会创建一个资源类直接调用库接口。这是默认和最简单的设置,见A。Service with serviceClass (see B) will make the resource call the service class which will call the repository interface. Service with serviceImpl (see C) will make a service interface which will be used by the resource class.接口implenment了一个impl类,这个类将会call repository interface。
如果不确定是否需要service,可以使用默认的设置,对CRUD是ok的。如果你有很多业务逻辑,要使用很多库,比较理想就是创建一个service类了。JHipster不推荐不必要的接口,但是如果你需要,你就建继承impl的service类。
entity A {}
entity B {}
entity C {}// no service for A
service B with serviceClass
service C with serviceImpl
JDL也支持多选项设置:
entity A
entity B
...
entity Zdto * with mapstruct
service all with serviceImpl
paginate C, with pager
注意 *
和all
是等价的。最新版本还引进了排除语法(这个语法对于设置所有实体的选项时很有用)
entity A
entity B
...
entity Zdto * with mapstruct except A
service all with serviceImpl except A, B, C
paginate C, with pager
在JHipster里,你可以选择是否不要一些客户端代码,或者服务端代码。即使你想添加一些后缀对Angular相关的文件,你都可以这么做。在JDL,你也可以
entity A
entity B
entity CskipClient for A
skipServer for B
angularSuffix * with mySuperEntities
最后,表名也可以指定,这样实体的名字和表名可以不一样,默认实体的名字就是表的名字。
entity A // A is the table's name here
entity B (the_best_entity) // the_best_entity is the table's name
Microservice-related options
JHipster V3,可以创建微服务。你可以指定一些选项在JDL中生存你想要的实体:微服务的名称,搜索引擎。指定微服务的名字的例子:
entity A
entity B
entity Cmicroservice * with mysuperjhipsterapp except C
microservice C with myotherjhipsterapp
search * with elasticsearch except C
第一种选项时用来告诉JHipster,你的微服务如何处理你的实体,第二种是指定你想使用的搜索引擎。
Commenting & Javadoc
可以给JDL文件添加Javadoc & comments。
和Java一样,下面的例子说明如何添加:
/*** Class comments.* @author The JHipster team.*/
entity MyEntity { // another form of comment/** A required attribute */myField String required,mySecondField String // another form of comment
}/*** Second entity.*/
entity MySecondEntity {}relationship OneToMany {/** This is possible too! */MyEntity{mySecondEntity}to/*** And this too!*/MySecondEntity{myEntity}
}
这些注释会被JHipster自动加为Javadoc comments。
JDL处理自己的注释如下:
// an ignored comment
/** not an ignored comment */
//
开头的注释被人是JDL的注释,不会被转换为Javadoc。
注意JDL Studio 指定以#
开头也会忽略。
另一种形式的注释如下:
entity A {name String /** My super field */count Integer /** My other super field */
}
这里A的name会被注释为 My super field
,第二个字段会注释为My other super field
。逗号是不强制使用的,但是你要注意不要因为逗号出问题:
entity A {name String, /** My comment */count Integer
}
这里A的name不会有注释,而count会。
All the relationships
解释如何使用JDL创建关系。
One-to-One
定义一个双向关系,例如车有一个司机,司机有一个车。
entity Driver {}
entity Car {}
relationship OneToOne {Car{driver} to Driver{car}
}
一个单向的例子,Citizen有一个Passpport,但是Passport没有权限获知他的拥有者。
entity Citizen {}
entity Passport {}
relationship OneToOne {Citizen{passport} to Passport
}
One-to-Many
一个双向的例子,Owner有没有,一个或者多个Car对象,Car知道自己的拥有者。
entity Owner {}
entity Car {}
relationship OneToMany {Owner{car} to Car{owner}
}
JHipster不支持这个关系单向的情况,但是如果有,会长这样
entity Owner {}
entity Car {}
relationship OneToMany {Owner{car} to Car
}
Many-to-One
One-to-Many 关系倒过来和前面一样。这种关系单向的例子,例如Ca知道自己的拥有者
entity Owner {}
entity Car {}
relationship ManyToOne {Car{owner} to Owner
}
Many-to-Many
最后这个例子,Car知道自己的拥有者,Driver对象可以获取自己的cars。
entity Driver {}
entity Car {}
relationship ManyToMany {Car{driver} to Driver{car}
}
注意拥有关系的对象,在to的左边。
Constants
对于JHipster Core v1.2.7,JDL支持数值型常量。例子:
DEFAULT_MIN_LENGTH = 1
DEFAULT_MAX_LENGTH = 42
DEFAULT_MIN_BYTES = 20
DEFAULT_MAX_BYTES = 40
DEFAULT_MIN = 0
DEFAULT_MAX = 41entity A {name String minlength(DEFAULT_MIN_LENGTH) maxlength(DEFAULT_MAX_LENGTH)content TextBlob minbytes(DEFAULT_MIN_BYTES) maxbytes(DEFAULT_MAX_BYTES)count Integer min(DEFAULT_MIN) max(DEFAULT_MAX)
}
Annexes
Here is the types supported by JDL:
SQL | MongoDB | Cassandra | Validations |
---|---|---|---|
String | String | String | required, minlength, maxlength, pattern |
Integer | Integer | Integer | required, min, max |
Long | Long | Long | required, min, max |
BigDecimal | BigDecimal | BigDecimal | required, min, max |
Float | Float | Float | required, min, max |
Double | Double | Double | required, min, max |
Enum | Enum | required | |
Boolean | Boolean | Boolean | required |
LocalDate | LocalDate | required | |
Date | required | ||
ZonedDateTime | ZonedDateTime | required | |
UUID | required | ||
Blob | Blob | required, minbytes, maxbytes | |
AnyBlob | AnyBlob | required, minbytes, maxbytes | |
ImageBlob | ImageBlob | required, minbytes, maxbytes | |
TextBlob | TextBlob | required, minbytes, maxbytes |
Issues and bugs
JDL的github连接:https://github.com/jhipster/jhipster-core
JHipster贡献指南:https://github.com/jhipster/generator-jhipster/blob/master/CONTRIBUTING.md
提交issue和Requsts请使用我们的项目
- JDL issue tracker :https://github.com/jhipster/jhipster-core/issues
- JDL Pull Requests :https://github.com/jhipster/jhipster-core/pulls
JHipster Domain Language (JDL)相关推荐
- JHipster翻译资料(1)[原创翻译]JHipster Domain Language (JDL)
本文翻译自官网:https://jhipster.github.io/jdl/ 功力有限,部分不会翻译的地方直接写译文了.由于目前JHipster的资料还比较少,先找一些英文的资料翻译翻译. 上来就找 ...
- JHipster | 03.编写JDL文件定义实体
文章目录 1.什么是JDL 2.编写JDL定义实体与联关系 2.1.编写JDL定义实体 2.2.编写JDL定义实体间的关系 3.下载.上传.执行JDL文件 1.什么是JDL JDL是JHipster特 ...
- JHipster | 02.编写JDL文件创建JHipster项目
文章目录 1.什么是JDL 2.简单上手 - 编写JDL文件,并通过jhipster创建单体项目 2.1.编写.下载.上传并执行JDL文件 3.进阶使用 - 编写JDL文件,并通过jhipster搭建 ...
- jhipster 工具之 JDL
使用jhipster JDL,我们可以快速的生产实体,业务逻辑层等类,这样大大提高我们的工作效率,下面就是具体的操作流程 1.用jhipster新建一个项目,这里就直接跳过了,不懂的直接看文档 2.然 ...
- jhipster 配置 mysql_JHipster 基础使用
认识JHipster JHipster又称Java Hipster,是一个Java代码脚手架,hipster 的意思是时髦的 意思是最时髦的Java解决方案 他的主要核心是Spring Boot,还有 ...
- JHipster 基础使用
认识JHipster JHipster又称Java Hipster,是一个Java代码脚手架,hipster 的意思是时髦的 意思是最时髦的Java解决方案 他的主要核心是Spring Boot,还有 ...
- DDD as Code:如何用代码诠释领域驱动设计?
简介: 相较于常规的MVC架构,DDD更抽象.更难以理解,各个开发者对DDD的解释也不尽相同.那么哪种设计方式才更好?在学习时如何知道哪种DDD更正统,没有被别人带歪?本文尝试使用"DDD ...
- 使用jdl的方式来生成JHipster项目
使用jdl的方式来生成JHipster项目 生成一个JDL测试文件 使用JHipster运行生成项目 上篇博客利用JHipster生成的项目tryagain目录 前提条件: 已经生成了一个JHipst ...
- JHipster中文文档(一)
介绍 技术栈 客户端技术栈 服务端技术栈 微服务技术栈 生产环境支持 环境设置 安装JHipster 安装方式 使用Yarn进行本地安装 当使用Angular时的快速配置 当使用AngularJS 1 ...
最新文章
- 入链、出链、反向链接、内链、外链的关系
- 对国内云计算三个现象的思考
- Bootstrap源码分析之transition、affix
- SpringBoot中使用thymeleaf时点击按钮触发事件失败
- 吊打一切现有开源OCR项目:效果再升7%,速度提升220%
- 涨姿势,一个通信项目从开始到结束,原来还包括这些工作
- USACO-Section1.6 Prime Palindromes(素数和回文数)
- poj 3714 Raid
- SqlDbx 个人版本使用指定的instant client
- vue3+el-table表格表头增加斜线
- python gui测试工具_GitHub - Github-Benjamin/LeChu: Python GUI工具 二次开发
- c#Winform自定义控件-目录
- script for kettle send mail contect
- VueI18n国际化vm._watchers[0].constructor未定义的解决办法:修改源代码吧
- python怎么启动mne_mne-python 安装大法
- aws rds mysql 连接_解决连接到 Amazon RDS 数据库实例的问题
- 前端sku-spu详解
- (跨平台UI)单片机用MTF HMI串口屏 UART通信易用兼容
- 使用python turtle库绘制一个三角形_使用turtle库绘制一个叠加等边三角形,图形效果如下:...
- stdlib函数编译linux,stdlib.h:没有这样的文件或目录
热门文章
- 织梦熊掌号提交urls.php,织梦后台如何接入熊掌号教程
- 北威州启动无线充电电动出租车试点项目
- CSDN第一博主ooduil再现博客江湖,hnlwt就是ooduil的马甲
- Unity动画系统详解3:如何播放、切换动画?
- 基于物联网方案的环保工况用电解决方案
- 最快速一键批量转换Excel里面的“文本形式数字”为“数值型数字”
- 机器学习——英文特征提取,中文特征提取
- 门禁系统一定要服务器吗,谈谈门禁系统在地铁中的应用
- java工具类-bean转map
- d0901-计算机应用指哪些,0901电子计算机及其外部设备