You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Agree.
I'm using Ktorm in my Spring Boot project where all my entity classes have createAt and updateAt fields.
I added the relevant autofill to BaseDao and I'll share my approach:
I defined an interface that represents the time, like
interfaceBaseDateTimeColumn {
var createAt:LocalDateTime?var updateAt:LocalDateTime?
}
Now, modify the add and update functions of BaseDao,
abstractclassBaseDao<E:Entity<E>, T : Table<E>>(privatevaltableObject:T) {
@Autowired
protectedlateinitvar database:Database/** * Insert the given entity into the table and return the effected record number.*/openfunadd(entity:E): Int {
if (entity isBaseDateTimeColumn) {
val now =LocalDateTime.now()
entity.createAt = now
entity.updateAt = now
}
return database.sequenceOf(tableObject).add(entity)
}
/** * Update properties of the given entity to the table and return the effected record number.*/openfunupdate(entity:E): Int {
if (entity isBaseDateTimeColumn) {
entity.updateAt =LocalDateTime.now()
}
return database.sequenceOf(tableObject).update(entity)
}
// ...
}
Okey, now your calls to add and update will automatically populate the updates.
I hope that helps. I can't think of a better way, just sharing my humble opinion lol, if you have a better way, please share it with me, thanks!
不管是使用ksp方式,还是BaseTable,在进行操作的时候,如何自动填充公共字段,比如createTime . updateTime, 这类的公共字段,而不用每次操作都需要手动去写一遍,框架是否有拦截器 或者 注解之类的方式?
The text was updated successfully, but these errors were encountered: