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 绝对值得一试!

相关推荐
Slow菜鸟5 小时前
Codex CLI 教程(五)| AI 驱动项目从零到一:面向 Java 全栈工程师打造个人 ECC(V2版)
java·开发语言·人工智能
月落归舟5 小时前
java基础之拷贝、单例
java·单例·拷贝
鬼蛟5 小时前
什么是 Git
java
直奔標竿5 小时前
SpringAI + RAG + MCP + Agent 零基础全栈实战(完结篇)| 27课完整汇总,Java开发者AI转型必看
java·开发语言·人工智能·spring boot·后端·spring
云烟成雨TD5 小时前
Spring AI 1.x 系列【31】向量数据库:进阶使用指南
java·人工智能·spring
万邦科技Lafite6 小时前
京东开放API接口:item_get返回参数指南
java·前端·javascript·api·电商开放平台
曹牧6 小时前
Java:处理 HTTP 请求的 Content-Type
java·开发语言
SamDeepThinking6 小时前
第1篇-开篇词:几亿用户规模下,我们是怎么做C端高并发商品系统的
java·后端·架构
weisian1516 小时前
Java并发编程--47-分布式ID生成器:雪花算法(Snowflake)与时钟回拨问题
java·算法·时钟回拨·雪花算法id
itzixiao6 小时前
L1-066 猫是液体(5分)[java][python]
java·开发语言·python·算法