-
-
Notifications
You must be signed in to change notification settings - Fork 147
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
实现jpa的@CreatedDate、@CreatedBy、@LastModifiedDate、@LastModifiedBy #32
Comments
Ktorm 是一个轻量级的 ORM 框架,只负责做好数据持久层的事情就可以了,不会提供像审计这种与具体业务紧密联系的功能 因此,如果你需要的话,可以自己基于 Ktorm 去扩展实现,在这里我只是给出一点建议:
interface AuditEntity<E : AuditEntity<E>> : Entity<E> {
var createdDate: Instant?
var createdBy: String?
var lastModifiedDate: Instant?
var lastModifiedBy: String?
fun flushChangesAuditing(user: String? = null): Int {
lastModifiedDate = Instant.now()
lastModifiedBy = user
return this.flushChanges()
}
}
open class AuditTable<E : AuditEntity<E>>(
tableName: String,
alias: String? = null,
entityClass: KClass<E>? = null
) : Table<E>(tableName, alias, entityClass) {
val createdDate by timestamp("created_date").bindTo { it.createdDate }
val createdBy by varchar("created_by").bindTo { it.createdBy }
val lastModifiedDate by timestamp("last_modified_date").bindTo { it.lastModifiedDate }
val lastModifiedBy by varchar("last_modified_by").bindTo { it.lastModifiedBy }
fun addAuditing(entity: E, user: String? = null): Int {
entity.createdDate = Instant.now()
entity.createdBy = user
entity.lastModifiedDate = entity.createdDate
entity.lastModifiedBy = user
return this.add(entity)
}
} 有了这两个类,所有的业务表对象和实体类都从这里继承就可以了,假设我们有一个 interface Product : AuditEntity<Product> {
var id: Int
var name: String
var price: BigDecimal?
}
object Products : AuditTable<Product>("t_product") {
val id by int("id").primaryKey().bindTo { it.id }
val name by varchar("name").bindTo { it.name }
val price by decimal("price").bindTo { it.price }
} 可以看到,两个父类中分别增加了 |
以这种方式 |
Products.batchInsert {
for (i in 1..100) {
item {
it.id to i
it.name to "product-$i"
it.price to i.toBigDecimal()
it.createdDate to Instant.now()
it.createdBy to "vince"
it.lastModifiedDate to Instant.now()
it.lastModifiedBy to "vince"
}
}
} |
我考虑的是写在一个公共的地方,不用每个地方都自己处理 |
怎样实现类似jpa的@CreatedDate、@createdby、@LastModifiedDate、@LastModifiedBy功能
The text was updated successfully, but these errors were encountered: