安装

Kotlin Multiplatform

共享模型、Native 目标与版本目录配置见 📚Kotlin Multiplatform。

运行时和编译插件

添加 kronos-core 作为 ORM 运行时,并启用 Kronos 编译插件来获得编译期 DSL 支持。

可复制依赖片段使用 0.4.0。只有在测试未发布源码改动时才使用 snapshot 版本。

build.gradle.kts

plugins {
    kotlin("jvm") version "2.4.21"
    id("com.kotlinorm.kronos-gradle-plugin") version "0.4.0"
}

kotlin {
    compilerOptions {
        freeCompilerArgs.addAll("-Xcollection-literals", "-Xcontext-sensitive-resolution")
    }
}

dependencies {
    implementation("com.kotlinorm:kronos-core:0.4.0")
}

build.gradle

plugins {
    id 'org.jetbrains.kotlin.jvm' version '2.4.21'
    id 'com.kotlinorm.kronos-gradle-plugin' version '0.4.0'
}

kotlin {
    compilerOptions {
        freeCompilerArgs.addAll("-Xcollection-literals", "-Xcontext-sensitive-resolution")
    }
}

dependencies {
    implementation 'com.kotlinorm:kronos-core:0.4.0'
}

pom.xml

<project>
    <properties>
        <kotlin.version>2.4.21</kotlin.version>
        <kronos.version>0.4.0</kronos.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.kotlinorm</groupId>
            <artifactId>kronos-core</artifactId>
            <version>${kronos.version}</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.jetbrains.kotlin</groupId>
                <artifactId>kotlin-maven-plugin</artifactId>
                <version>${kotlin.version}</version>
                <extensions>true</extensions>
                <configuration>
                    <args>
                        <arg>-Xcollection-literals</arg>
                        <arg>-Xcontext-sensitive-resolution</arg>
                    </args>
                    <compilerPlugins>
                        <plugin>kronos-maven-plugin</plugin>
                    </compilerPlugins>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>com.kotlinorm</groupId>
                        <artifactId>kronos-maven-plugin</artifactId>
                        <version>${kronos.version}</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>
</project>

JDBC wrapper

应用直接使用 JDBC DataSource 时,添加 kronos-jdbc-wrapper。它负责执行生成 SQL,并把查询结果映射回 KPojo 对象。

Gradle

dependencies {
    implementation("com.kotlinorm:kronos-jdbc-wrapper:0.4.0")
}

Maven

<dependency>
    <groupId>com.kotlinorm</groupId>
    <artifactId>kronos-jdbc-wrapper</artifactId>
    <version>${kronos.version}</version>
</dependency>

连接配置见 📚连接到数据库。Spring JDBC 或其他框架的接入见 📚自定义 Wrapper。

Android SQLite

Android/JVM 和 Android SQLiteDatabase 接入请统一阅读 📚Android SQLite 独立章节,其中包含依赖、wrapper、事务、日志和完整示例。

数据库驱动和连接池

JDBC Driver 和连接池版本需要与数据库服务端、JDK 匹配。下面的 <latest-stable> 占位符表示第三方依赖按运行环境选择当前稳定版。

MySQL example

dependencies {
    implementation("org.apache.commons:commons-dbcp2:<latest-stable>")
    implementation("com.mysql:mysql-connector-j:<latest-stable>")
}

最小环境

请确保 IDE 使用的 Kotlin 插件支持 Kotlin 2.4.21 或更高版本。

下一步按 📚第一次查询 完成最小模型、建表、插入和查询流程。