jHipster JDL 教程

写于2017-02-15

JDL jHipster专用的生成实体的语言。(大部分翻译自JDL )

在jHipster的项目,生成实体需要的文件:

  • a database table(表中对应的表)

  • a Liquibase change set(Liquibase 的changelog,在src\main\resources\config\liquibase\changelog目录下的文件)

  • a JPA entity class(JPA实体类 src\main\java\com\ xxx\ xxx\domain目录下,应该是,错了请评论)

  • a Spring Data JpaRepository interface (src\main\java\com\binana\amas\repository 目录下)

  • a Spring MVC RestController class;

  • an AngularJS router, controller and service; -src\main\webapp\app\entities\ 目录下

  • a HTML page.-HTML文件

导入.jh文件命令:
yo jhipster:import-jdl my_file1.jh my_file2.jh

jhipster-uml my_file1.jh my_file2.jh

导入多个文件用空格隔开,需要把对应文件放在项目的根目录,不然会报错,说在根目录找不到对应jh文件。

JDL实体示例:
entity <entity name> {
  <field name> <type> [<validation>*]
}
  • <entity name> 实体名称
  • <field name> 字段名,建议使用驼峰命名,使用下划线后面会出现问题
  • <type> 字段类型(需要jHipster支持的类型)
  • <validation> 字段的验证(是否为空,长度限制,正则等)
例子:
entity A

entity B

entity C {}

entity D {

  name String required,

  address String required maxlength(100),

  age Integer required min(18)

}

说明:A ,B,C实体不包含任何字段,对于空实体这两种定义方式都可以。

D实体包含:

名称为name,类型为string,不为空的字段。

名称为address,类型为string,不为空的字段且字符串最大长度为100的字段。

名称为age,类型为integer,不为空的字段且最小值为18的字段。

如果需要验证,那么就在验证括号内加内容。

验证说明
required是否为空
minlength(10):字符串的最小长度
maxlength(100)字符串的最大长度
pattern(*?[a-z])正则
min(10)数值的最小值
max(100)数值的最大值
minbytes(512)BLOB的最小字节
maxbytes(1024)BLOB的最大字节

JHipster会自动添加ID字段。

下面是JDL支持的各种数据库对应的数据类型,JDL网站上面有给出。

SQLMongoDBCassandraValidations
StringStringStringrequired, minlength, maxlength, pattern
IntegerIntegerIntegerrequired, min, max
LongLongLongrequired, min, max
BigDecimalBigDecimalBigDecimalrequired, min, max
FloatFloatFloatrequired, min, max
DoubleDoubleDoublerequired, min, max
EnumEnumrequired
BooleanBooleanBooleanrequired
LocalDateLocalDaterequired
Daterequired
ZonedDateTimeZonedDateTimerequired
UUIDrequired
BlobBlobrequired, minbytes, maxbytes
AnyBlobAnyBlobrequired, minbytes, maxbytes
ImageBlobImageBlobrequired, minbytes, maxbytes
TextBlobTextBlobrequired, minbytes, maxbytes
关系示例:
relationship (OneToMany | ManyToOne | OneToOne | ManyToMany) {
  <from entity>[{<relationship name>}] to <to entity>[{<relationship name>}]
}
  • (OneToMany | ManyToOne| OneToOne | ManyToMany) 关系类型

  • <from entity> 关系拥有者的实体名

  • <to entity> 目标关系的实体名

  • <relationship name> 关联另一方的那个字段

同时可以在一个关系类型里面添加多个关系
entity A
entity B
entity C
entity D
relationship 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 B
relationship OneToOne {
  A{b} to B{a(name)}
}

枚举类型定义如下:

enum Language {
    FRENCH, ENGLISH, SPANISH
  }

在另外一个实体中引用如下:

  entity Book {
    title String required,
    description String,
    language Language
  }

BLOB(byte[]):

可以存放图片或者其他二进制类型的文件。

  • AnyBlob 或者就叫Blob 存放二进制文件的字段;
  • ImageBlob 意味着存放图片的字段.
  • TextBlob 存放长文本的字段.
各种关系的前台和数据库的体现如下:

例子:

relationship ManyToMany {
 Job{task(title)} to Task{job}
}

意思是:用Task实体的title做关联外键。

JOB前台表示如下,显示的是task的title.实体中查看详细可以直接连接到对应的Task:

2070425-7681f18f3387e1a8.png
image

JOB数据库表现如下,并没有task_title:

2070425-f5cf01a39f40986a.png
image

其他可选的操作:

entity A {
  name String required
}
entity B {}
entity C {}
dto A, B with mapstruct
paginate A, C with infinite-scroll
paginate B with pager
service A with serviceClass
service C with serviceImpl

目前我只知道分页的方式其他不懂。

分页的表现
2070425-ba37595107cbcb80.png
image
  • NO:对应如下,有多少数据就显示多少:


    2070425-bded1e59bb5e39e4.png
    image
  • Simple pager:


    2070425-9a3369b765c2879e.png
    image
  • Pagination links:


    2070425-ffc7be03d820c5f7.png
    image
  • infinite-scroll:
    类似淘宝商品那样,触发到底部就加载接下来的数据。


    2070425-dac5c875e906e741.png
    image
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值