我正在用maven编写一个Spring Boot应用程序。但最近我将JDK升级到了10,并且Lombok稳定停止使用它。我决定开始使用Kotlin是一个很好的借口。但是我对使用Spring启动应用程序的Kotlin数据类的外观不满意。
示例Java:
@Data
@Entity
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(exclude = {"creator", "content"})
public class Document {
@Id
@GeneratedValue(generator = "uuid2")
@GenericGenerator(name = "uuid2", strategy = "uuid2")
private String id ;
@Size(min = 4, max = 100)
private String name;
@ManyToOne
@Column(updatable = false)
private User creator;
@OneToOne(cascade = CascadeType.ALL)
private Content content;
@ManyToOne
private Project project;
@NotNull
@Column(updatable = false)
@JsonFormat(pattern = "yyyy/MM/dd hh:mm:ss")
private LocalDateTime creationDate;
@NotNull
@LastModifiedDate
@JsonFormat(pattern = "yyyy/MM/dd hh:mm:ss")
private LocalDateTime lastModified;
@ManyToOne
@LastModifiedBy
private User modificator;
}
示例Kotlin:
@Entity
data class Document(@Id
@GeneratedValue(generator = "uuid2")
@GenericGenerator(name = "uuid2", strategy = "uuid2")
val id: String,
@Size(min = 4, max = 100)
val name: String,
@ManyToOne
@JoinColumn(updatable = false)
private val creator: User,
@OneToOne(cascade = arrayOf(CascadeType.ALL))
private val content: Content,
@ManyToOne
@JoinColumn(updatable = false)
val project: Project,
@NotNull
@Column(updatable = false)
@JsonFormat(pattern = "yyyy/MM/dd hh:mm:ss")
val creationDate: LocalDateTime,
@NotNull
@LastModifiedDate
@JsonFormat(pattern = "yyyy/MM/dd hh:mm:ss")
private val lastModified: LocalDateTime,
@ManyToOne
@LastModifiedBy
private val modificator: User
)
这让我的代码真的难以理解。你有任何模板应该如何编写Spring引导数据类吗?或者如何使用Kotlin的所有优势将现有Java类转换为Kotlin?
我正在使用IntelliJ Idea社区版2018.1。或许你有更好的想法像模型类一样坚持Lombock?
答案 0 :(得分:2)
Kotlin数据类允许使用POJO无法获得的简洁性和效率。我在我的工作项目中使用这种模式,你正在寻找 。
而且,是的,它们看起来与此类似。但是,您的代码中存在一些可以轻松绕过的效率低下的问题。例如,当语言内置时,可空性注释没有任何意义。
这是我的一个项目类,作为实际使用的一个例子:
@Entity
@Table(name = "Acks")
data class AckEntity(
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Int = 0,
@Enumerated(EnumType.STRING)
val ackType: AckType,
@OneToOne(cascade = [CascadeType.ALL], fetch = FetchType.EAGER)
@JoinColumn(name = "alert")
val alert: AlertEntity,
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn
val session: SessionEntity? = null,
@Embedded
val sale: SaleEntity?,
val version: Int,
val timestamp: Date?) : DataEntity
与这些类在Java中的外观相比,我认为这是一种更加清晰的方法。
然而,可能只需要一些时间来习惯Kotlin如何做事。一旦你学会了如何快速阅读语言,我想你会发现你的效率提高了很多。我知道我做了。
答案 1 :(得分:1)
如果你不喜欢它的外观,你不必将Model类转换为Kotlin。 Kotlin可以选择.java类。否则你可以做类似的事情,顺便说一下这只是一个智能的自动转换。
@Data
@Entity
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(exclude = { "creator", "content" })
class Document {
@Id
@GeneratedValue(generator = "uuid2")
@GenericGenerator(name = "uuid2", strategy = "uuid2")
var id: String? = null
set(id) {
field = this.id
}
@Size(min = 4, max = 100)
var name: String? = null
set(name) {
field = this.name
}
@ManyToOne
@Column(updatable = false)
var creator: User? = null
set(creator) {
field = this.creator
}
@OneToOne(cascade = CascadeType.ALL)
var content: Content? = null
set(content) {
field = this.content
}
@ManyToOne
var project: Project? = null
set(project) {
field = this.project
}
@NotNull
@Column(updatable = false)
@JsonFormat(pattern = "yyyy/MM/dd hh:mm:ss")
var creationDate: LocalDateTime? = null
set(creationDate) {
field = this.creationDate
}
@NotNull
@LastModifiedDate
@JsonFormat(pattern = "yyyy/MM/dd hh:mm:ss")
var lastModified: LocalDateTime? = null
set(lastModified) {
field = this.lastModified
}
@ManyToOne
@LastModifiedBy
var modificator: User? = null
set(modificator) {
field = this.modificator
}
}