jhipster代码生成器可以生成Resource,Repository,domain,Mapper,changtlog,csv,mast.xml
一、在mode目录下的jhipster-jdl.jh 推荐清空其他entity
entity NewCustomerRequest {
id UUID
name String
country String
}
别忘了加以下字段能生成dto 和service(默认是不生成的)
// Use Data Transfert Objects (DTO)
dto * with mapstruct
// Set service options to all except few
//service all with serviceImpl except Employee, Job
// Set an angular suffix
// angularSuffix * with mySuffix
要求:entity 必须大写开头
否则报错:The entity name must match: /^[A-Z][A-Za-z0-9]*$/
二、jhipster import-jdl src\model\jhipster-jdl.jhimport-jdl src\model\jhipster-jdl.jh
三、拷贝到别的项目
Resource,Repository,domain,Mapper,changtlog,csv,mast.xml
changtlog配置文件changeSet id 不能重复
四、修改类 extends AbstractAuditingEntity implements Serializable
继承创建人和创建时间
五、根据实际情况修改以上文件
备注:
1.domain类中修改如下:
@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
private String id;
@OneToOne
@JoinColumn(unique = true)
private Quote quote;
注意:domain类中没有关联字段 quote_id
2.changtlog类中新增如下
<column name="quote_id" type="varchar(36)">
<constraints nullable="true" />
</column>
loadData 中新增如下
<column name="quote_id" type="string"/>
3.修改mapper文件
@Mapper(componentModel = "spring", uses = {QuoteMapper.class})
public interface RequestMapper extends EntityMapper<RequestDTO, Request> {
@Mapping(source = "quote.id", target = "quoteId")
@Mapping(source = "quote.name", target = "quoteName")
RequestDTO toDto(Request Request);
@Mapping(source = "quoteId", target = "quote")
Request toEntity(RequestDTO RequestDTO);
}
注意:
@Mapper(componentModel = “spring”),表示把当前Mapper类纳入spring容器。可以在其它类中直接注入了
@Mapper( uses = { BooleanStrFormat.class}),注意,这里的users属性用于引用之前定义的转换规则的类
mapping属性 将quote对象的id字段转换为quoteId
当报错如下
Error:(18, 5) java: Can't map property "java.lang.String quoteId" to "com.lenovo.quotation.domain.Quote quote". Consider to declare/implement a mapping method: "com.lenovo.quotation.domain.Quote map(java.lang.String value)".
改为 @Mapper(componentModel = "spring", uses = {QuoteMapper.class})
4.当存在one同many时,one的一方放弃维护关系 多的一方维护 @OneToOne(mappedBy = "quote")
5.忽略类中不存在的字段
使用@JsonIgnoreProperties、@JsonIgnore、@JsonFormat。
@JsonIgnore注解用来忽略某些字段,可以用在变量或者Getter方法上,用在Setter方法时,和变量效果一样。这个注解一般用在我们要忽略的字段上。
@JsonIgnoreProperties(ignoreUnknown = true),将这个注解写在类上之后,就会忽略类中不存在的字段。这个注解还可以指定要忽略的字段,例如@JsonIgnoreProperties({ “password”, “secretKey” })
@JsonFormat可以帮我们完成格式转换。例如对于Date类型字段,如果不适用JsonFormat默认在rest返回的是long,如果我们使用@JsonFormat(timezone = “GMT+8”, pattern = “yyyy-MM-dd HH:mm:ss”),就返回"2018-11-16 22:58:15"