Kite:Kotlin/Java 通用的全自动 ORM 框架

Kite:Kotlin/Java 通用的全自动 ORM 框架

Kite 是一个高效的轻量级 ORM 框架,基于 Kotlin 编写,开箱即用,内置分页查询、增删改查等常用功能,支持多表操作。它支持 PostgreSQL、MySQL、Derby 等多种数据库,旨在通过简化数据库操作,减少代码量,提升开发效率。

框架特点

  • 全自动映射:无需手动编写 SQL,Kite 会自动根据实体类生成相应的数据库操作语句
  • 支持自定义 SQL:在需要时,可以编写自定义 SQL 语句,满足复杂查询需求,还可以像写代码一样写流程控制语句
  • 多数据库支持:支持 PostgreSQL、MySQL、Derby 等主流关系型数据库
  • Kotlin/Java 双语言支持:既可以在 Kotlin 项目中使用,也可以在 Java 项目中无缝集成
  • 轻量级设计:无过多依赖,性能优秀
  • 丰富的 API:提供简洁直观的 API,支持各种复杂查询和操作
  • Spring Boot 集成:提供 Spring Boot Starter,便于在 Spring Boot 项目中快速集成

使用方法(Spring Boot 集成示例)

Maven 中央仓库: kite-spring-boot-starter

  1. 向项目添加以下依赖:
  • Maven
xml 复制代码
<dependency>
   <groupId>io.github.tangllty</groupId>
   <artifactId>kite-spring-boot-starter</artifactId>
   <version>${kite.version}</version>
</dependency>
  • Gradle
kts 复制代码
implementation("io.github.tangllty:kite-spring-boot-starter:${kite.version}")
  1. 在数据库中创建表

使用 MySQL 演示

sql 复制代码
create table account (
  id          bigint not null auto_increment,
  username    varchar(32)     default '',
  password    varchar(32)     default '',
  balance     decimal(10,2)   default '0.00',
  create_time datetime        default null,
  update_time datetime        default null,
  primary key (`id`)
);

insert into account (username, password, create_time, balance) values
('admin', 'admin123', '2020-01-01 12:00:00', 1000.10),
('user', 'user123', '2024-05-02 8:30:00', 101.00),
('guest', 'guest123', '2022-03-03 15:00:00', 10.00),
('tang', 'tang123', '2019-06-01 21:30:30', 1.88),
('jeo', 'jeo123', '2024-07-01 5:59:59', 0.10);
  1. 在 application.yml 文件中配置数据库连接信息
yaml 复制代码
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/kite-test
    username: root
    password: password
  1. 为 account 表创建模型类
  • Java
java 复制代码
import com.tang.kite.annotation.id.Id;
import com.tang.kite.annotation.id.IdType;
import java.math.BigDecimal;
import java.time.LocalDateTime;

public class Account {

    @Id(type = IdType.AUTO)
    private Long id;
    private String username;
    private String password;
    private BigDecimal balance;
    private LocalDateTime createTime;
    private LocalDateTime updateTime;

    // Getters and Setters
}
  • Kotlin
kotlin 复制代码
import com.tang.kite.annotation.id.Id
import com.tang.kite.annotation.id.IdType
import java.math.BigDecimal
import java.time.LocalDateTime

class Account (

    @Id(type = IdType.AUTO)
    var id: Long? = null,
    var username: String? = null,
    var password: String? = null,
    var balance: BigDecimal? = null,
    var createTime: LocalDateTime? = null,
    var updateTime: LocalDateTime? = null

)
  1. 继承 BaseMapper 接口创建 Mapper 接口
  • Java
java 复制代码
import com.tang.kite.mapper.BaseMapper;
import com.tang.kite.spring.annotation.Mapper;

@Mapper
public interface AccountMapper extends BaseMapper<Account> {
}
  • Kotlin
kotlin 复制代码
import com.tang.kite.mapper.BaseMapper
import com.tang.kite.spring.annotation.Mapper

@Mapper
interface AccountMapper : BaseMapper<Account>
  1. 在 Spring Boot 应用类上添加 @MapperScan 注解
  • Java
java 复制代码
import com.tang.kite.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@MapperScan("com.tang.application.mapper")
@SpringBootApplication
public class KiteApplication {

    public static void main(String[] args) {
        SpringApplication.run(KiteApplication.class, args);
    }

}
  • Kotlin
kotlin 复制代码
import com.tang.kite.spring.annotation.MapperScan
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication

@MapperScan(["com.tang.application.mapper"])
@SpringBootApplication
class KiteApplication

fun main(args: Array<String>) {
	runApplication<KiteApplication>(*args)
}
  1. 测试 Mapper 接口
  • Java
java 复制代码
import com.tang.demo.mapper.AccountMapper;
import com.tang.kite.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@MapperScan("com.tang.application.mapper")
@SpringBootApplication
public class KiteApplication {

    public static void main(String[] args) {
        var context = SpringApplication.run(KiteApplication.class, args);
        var accountMapper = context.getBean(AccountMapper.class);
        var accounts = accountMapper.select();
        accounts.forEach(System.out::println);
    }

}
  • Kotlin
kotlin 复制代码
import com.tang.demo.mapper.AccountMapper
import com.tang.kite.spring.annotation.MapperScan
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication

@MapperScan(["com.tang.application.mapper"])
@SpringBootApplication
class KiteApplication

fun main(args: Array<String>) {
	val context = runApplication<KiteApplication>(*args)
	val accountMapper = context.getBean(AccountMapper::class.java)
	val accounts = accountMapper.select()
	accounts.forEach { println(it) }
}

文档与社区

官方文档

详细的使用文档请参考:

源码

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

总结

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

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

相关推荐
哦虎!4 小时前
【JavaEE】网络初识
java
for_ever_love__4 小时前
MySQL ORDER BY 排序优化:从 Using filesort 到索引有序扫描(含双路 / 单路排序原理)
java·mysql·优化·存储·by·order·filesort
程序猿追5 小时前
kotlin-retry 接入鸿蒙后,怎样确认它真的会停止重试
开发语言·kotlin·harmonyos
我是人✓5 小时前
Java文件上传三方式对比:从传统到SpringBoot
java·开发语言
欣栀♡5 小时前
JVM 频繁 Full GC 先看日志括号里的 cause,jstat 的 FGC 在 JDK 8 上会骗人
java·jvm
IT研究室5 小时前
最新计算机毕业设计选题推荐-基于spring boot的葡萄酒文化宣传网站-网站-文档指导-Java-springboot
java·spring boot·课程设计
慧都小项5 小时前
MyEclipse 中创建和测试 JAX-RS REST 接口:从项目配置到 REST Explorer
java·restful·myeclipse·jax-rs
Wang's Blog5 小时前
Java 项目实战: 交友项目 - 入门案例:编写 Dubbo 服务提供者
java·dubbo·交友
秋饼6 小时前
Spring AI 2.0 结构化输出企业级实战:从 BeanOutputConverter 源码到生产级解析容错
java·ai·技术分享·后端开发
摇滚侠6 小时前
《计算机是如何工作的:人人都能懂的计算机软硬件工作原理》第 9 章 机器码与汇编语言 阅读笔记 9
java·开发语言·笔记