KtOrm is a lightweight and efficient ORM Framework for Kotlin directly based on pure JDBC. It provides strong typed and flexiable SQL DSL and many convenient extension functions to reduce our duplicated effort on database operations. All the SQLs are generated automaticlly, of course. For more documentation, go to our site: https://ktorm.liuwj.me.
🇺🇸 English | 🇨🇳 简体中文
- No configuration files, no xml, lightweight, easy to use.
- Strong typed SQL DSL, exposing low-level bugs at compile time.
- Flexiable query, exactly control the generated SQLs as you wish.
- Extensible design, write your own extensions to support more data types, SQL functions, etc.
- Dialects supports, MySQL, Oracle, PostgreSQL, or you can write your own dialect support by implementing the
SqlDialect
interface.
KtOrm was deployed to maven central and jcenter, so you just need to add a dependency to your pom.xml
file if you are using maven:
<dependency>
<groupId>me.liuwj.ktorm</groupId>
<artifactId>ktorm-core</artifactId>
<version>${ktorm.version}</version>
</dependency>
Or Gradle:
compile "me.liuwj.ktorm:ktorm-core:${ktorm.version}"
Then create a Kotlin object to describe your table schema:
object Employees : Table<Nothing>("t_employee") {
val id by int("id").primaryKey()
val name by varchar("name")
val job by varchar("job")
val managerId by int("manager_id")
val hireDate by date("hire_date")
val salary by long("salary")
val departmentId by int("department_id")
}
Now connect to your database and write a simple query:
fun main() {
Database.connect("jdbc:mysql://localhost:3306/ktorm", driver = "com.mysql.jdbc.Driver")
for (row in Employees.select()) {
println(row[Employees.name])
}
}
When you run this program, KtOrm will generate a SQL select * from t_employee
, selecting all employees in the table and printing their names.