- Kotlin Blueprints
- Ashish Belagali Hardik Trivedi Akshay Chordiya
- 182字
- 2021-07-02 21:50:14
CRUD operations
The following code snippet shows an example of how to do create, read, update, delete (CRUD) operations with Exposed:
transaction {
// Insert new message Messages.insert {
it[name] = "Hello Kotlin Developers!"
}
// Update an existing message
Messages.update({Users.id eq 1}) {
it[name] = "Hello Spring-Kotlin Developers"
}
// Delete the messages table drop(Messages) }
It results in the following queries:
INSERT INTO Messages (name) VALUES ('Hello Kotlin Developers!')
UPDATE Messages SET name='Hello Spring-Kotlin Developers' WHERE
Messages.id = 1
DROP TABLE Messages
Get all the users from the database:
for (message in Messages.selectAll()) {
println("$message[Messages.name]}")
}
It will result in the following query:
SELECT Messages.name FROM Messages;
Now it must be pretty clear how to use the Exposed library for Spring and how it works internally.
The reason for using the Exposed library instead of Spring JPA is because, at the time of writing, there is no official support for Kotlin data classes and JPA, that is Spring Data JPA. JetBrains noticed this and they released their own SQL ORM library called Exposed.
Read more at https://github.com/JetBrains/Exposed.
推薦閱讀
- C# 7 and .NET Core Cookbook
- JavaScript+DHTML語法與范例詳解詞典
- Leap Motion Development Essentials
- Practical Windows Forensics
- Python 3破冰人工智能:從入門到實戰
- 大學計算機基礎(第2版)(微課版)
- SharePoint Development with the SharePoint Framework
- 程序是怎樣跑起來的(第3版)
- Create React App 2 Quick Start Guide
- Procedural Content Generation for C++ Game Development
- Scala編程(第5版)
- RubyMotion iOS Develoment Essentials
- 深度學習:基于Python語言和TensorFlow平臺(視頻講解版)
- TensorFlow+Keras深度學習算法原理與編程實戰
- Learning ClojureScript