Kite:填充处理器

Kite:填充处理器

填充处理器功能允许你在增删改查时,自动设置某些字段的值,而无需手动指定。

官方实现

Kite 提供了一个时间填充处理器 TimeFillHandler,它可以使用 @CreateTime@UpdateTime 注解自动设置创建时间和更新时间字段的值。

定义注解

注解只能生效在字段上。

你可以添加属性来实现更复杂的功能。

:::tabs key:kite

== Java

java 复制代码
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface CreateTime {}

== Kotlin

kotlin 复制代码
@Target(AnnotationTarget.FIELD)
@Retention(AnnotationRetention.RUNTIME)
@MustBeDocumented
annotation class CreateTime

:::

定义填充处理器

可以根据注解和字段类型来返回不同的值。

:::tabs key:kite

== Java

java 复制代码
import com.tang.kite.handler.fill.FillHandler;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.time.LocalDateTime;

public class TimeFillHandler implements FillHandler {

    @Override
    @Nullable
    public Object fillValue(@NotNull Annotation annotation, @NotNull Field field, @NotNull Object entity) {
        return LocalDateTime.now();
    }

}

== Kotlin

kotlin 复制代码
import com.tang.kite.handler.fill.FillHandler
import java.lang.annotation.Annotation
import java.lang.reflect.Field
import java.time.LocalDateTime

class TimeFillHandler : FillHandler {

    override fun fillValue(annotation: Annotation, field: Field, entity: Any): Any? {
        return LocalDateTime.now()
    }

}

:::

注册填充处理器

你可以在 KiteConfig 中注册填充处理器。

:::tabs key:kite

== Java

java 复制代码
import com.tang.kite.annotation.fill.CreateTime;
import com.tang.kite.config.KiteConfig;
import com.tang.kite.enumeration.SqlType;
import com.tang.kite.handler.fill.FillKey;
import com.tang.kite.handler.fill.TimeFillHandler;

KiteConfig.getFillHandlers().put(new FillKey(CreateTime.class, SqlType.INSERT), new TimeFillHandler());

== Kotlin

kotlin 复制代码
import com.tang.kite.annotation.fill.CreateTime
import com.tang.kite.config.KiteConfig
import com.tang.kite.enumeration.SqlType
import com.tang.kite.handler.fill.FillKey
import com.tang.kite.handler.fill.TimeFillHandler

KiteConfig.fillHandlers[FillKey(CreateTime::class, SqlType.INSERT)] = TimeFillHandler()

:::

使用示例

:::tabs key:kite

== Java

java 复制代码
public class Account {

    @CreateTime
    private LocalDateTime createTime;

    @UpdateTime
    private LocalDateTime updateTime;

}

== Kotlin

kotlin 复制代码
class Account {

    @CreateTime
    var createTime: LocalDateTime? = null

    @UpdateTime
    var updateTime: LocalDateTime? = null

}

:::

官方文档

详细的使用文档请参考:

源码

Kite 的源码托管在 GitHub 和 Gitee 上,您可以 在以下地址查看和贡献:

总结

Kite 是一个功能强大、易于使用的 ORM 框架,它通过全自动映射和简洁的 API,大大简化了数据库操作的开发工作。无论是在 Kotlin 项目还是 Java 项目中,都能提供高效、便捷的数据库访问体验。

如果您正在寻找一个轻量级、高性能的 ORM 框架,Kite 绝对值得一试!

相关推荐
大牧师35 分钟前
TypeORM 学习教程
数据库·sql·学习·node.js·orm·nest.js·typeorm
我命由我123452 小时前
Android Jetpack Compose 开发问题:Unresolved reference ‘Icons‘.
android·java-ee·kotlin·android studio·android jetpack·android-studio·android runtime
坚果的博客5 小时前
CPF-KMP-CMP:在鸿蒙上用 Kotlin 写跨平台应用
华为·kotlin·harmonyos
事圆则缓5 小时前
Kotlin 高阶工程化与 Android 深入实践
android·开发语言·kotlin
FungLeo16 小时前
成为全栈·Node 后端篇·数据迁移:schema 变更如何不弄脏线上数据
sqlite·orm·schema·d1·node 后端·drizzle
沐沐师19 小时前
TypeORM 学习教程
orm
pengyu19 小时前
【Kotlin 协程修仙录 · 渡劫境 · 中阶】 | 造化神兵:自定义 CoroutineDispatcher 与调度器的终极定制
android·kotlin
pengyu20 小时前
【Kotlin 协程修仙录 · 渡劫境 · 初阶】 | 飞升雷劫:CPS 变换与挂起函数字节码终极透视
android·kotlin
hunterandroid1 天前
Android 多渠道打包与 Gradle 构建优化实战
android·前端·kotlin
pengyu1 天前
【Kotlin 协程修仙录 · 大乘境 · 后阶】 | 死锁天劫:协程同步原语与 ThreadLocal 迁移之道
android·kotlin