說是ORM框架,其實更該說是Android 數據庫領域的 DSL 語言。(領域特定語言)
所以用起來自然超簡約。
不多說了,Github地址:https://github.com/senyuyuan/Gluttony
文檔如下:
Gluttony · 饕餮
Gluttony, a super convenient and simple library to using Android database
饕餮,高效而簡約地使用Android數據庫
Library is compatible with Kotlin 1.0.5-2 build.
饕餮 兼容 kotlin 1.0.5-2 版本(最新版)
Content · 目錄
- Feature · 特性
- Install · 安裝
- How to use · 如何使用
- Configuration · 配置
- Entities · 實體
- Save · 保存
- Find · 查詢
- Update · 更新
- Delete · 刪除
- Condition · 條件
Feature · 特性
* The core concept, friendly to human, Gluttony will give you the perfect experience
* 核心理念,對人類友好,Gluttony將會給您完美的使用體驗
* Automatic table and column naming through reflection
* 通過反射自動建表
* Entities do not need any processing
* 實體類無需任何處理
* The future will provide two kinds of the kernel for your choice,
【sqlite】 and 【realm】, the performance of the pursuit of perfection
* 未來將會提供兩種內核供您選擇,【sqlite】以及【realm】,追求極致的性能
Install · 安裝
as a gradle dependency
作為一個gradle依賴庫
compile 'sen.yuan.dao:gluttony:1.1'
How to use · 如何使用
Configuration · 配置
//configure databaseName,cursorFactory,databaseVersion
//配置 數據庫名稱,cursorFactory,數據庫版本
Gluttony.init(this, GluttonyConfig("gluttony_example",null,1))
In Application or the first Activity, initialize Gluttony.
在Application或是首個Activity中,初始化Gluttony·饕餮。
Entities · 實體
Entities do not need to do any processing.
實體類無需做任何處理。
Gluttony automatically for you to take care of everything.
Gluttony·饕餮 在數據庫中自動為您打理好一切。
Annotation: @PrimaryKey is used to specify a PrimaryKey.
注解:@PrimaryKey 用來指定主鍵。
data class UserData(
@PrimaryKey
var id: Int = -1,
var name: String = "",
var age: Int = -1,
var isChildren: Boolean = false
)
Save · 保存
Save Entity directly · 直接保存實體
val user = UserData()
user.id = 666
user.name = "sen"
user.age = 23
user.isChildren = false
user.save()
//or
UserData(2, "john", 12, true).save()
Find · 查詢
Find Entity based on PrimaryKey · 根據primary key 查詢數據
val user1 = UserData().findOneByKey(666)
Find the first Entity based on Condition · 根據條件 查詢第一個數據
val user2 = UserData().findOne {
condition {
"age" between 7..16
"isChild" Not false
}
orderBy("age", SqlOrderDirection.ASC)
}
Find all Entities based on Condition · 根據條件 查詢所有數據,返回值為一個列表
val userList = UserData().findAll {
condition {
"age" moreThan 11
"name" like "s%" //find names witch is starting with "s"
}
}
Update · 更新
Update Entity directly base on PrimaryKey · 根據primary key 直接更新實體
var user3 = UserData().findOne { condition { "name" equalsData "lucy" } }!!
user3.age += 1
user3.update()
//or
var user4 = UserData(7, "lucy", 10, true) //user4.id == user3.id · 注意primary key相同
user4.update() //user4 will overwrite the old data · 將會覆蓋掉舊數據
Update Entity directly or Save Entity when it doesn't exist · 直接 更新或保存實體 (如果實體是未保存過的話)
var user5 = UserData(90, "white", 77, false) // 90 is a new primary key
user5.updateOrSave() // Gluttony will save a new data
Update Entity based on PrimaryKey · 根據primary key 更新實體
//update user who id is 90 to named black,age 80
//lambda
UserData().updateByKey(90) { arrayOf("name" to "black", "age" to 80) }
//or pairs
UserData().updateByKey(90, "name" to "green", "age" to 82)
Update all Entities based on Condition · 根據條件 更新所有實體
//update user who name is green to name red,age 99
UserData().update("name" to "red", "age" to 99) {
condition {
"name" equalsData "green"
}
}
Delete · 刪除
Delete Entity directly · 直接刪除實體
var user9 = UserData(90) //only need primary key
user9.delete()
Delete Entity based on PrimaryKey · 根據primary key 刪除實體
UserData().deleteByKey(666) //delete user which id is 666
Delete all Entities based on Condition · 根據條件 刪除所有實體
//delete users who is child
UserData().delete {
condition {
"isChild" equalsData true
}
}
Clear one Class's entities · 清空一個類的所有實體
UserData().clear()
Condition · 條件
equalsData · 等于
moreThan · 大于
moreThanOrEquals · 大于等于
lessThan · 小于
lessThanOrEquals · 小于等于
Not · 非
In: Determine whether in the array · 判斷是否在數組中
notIn: Determine whether not in the array
between: Determine whether in the range
like: fuzzy query · 模糊查詢
two marks: % and _ · 兩個通配符:% 和 _
% : indefinite amount content · 不定數量的內容
_ : one amount content · 一個位置的內容
for example: "gluttony%" -> find values witch is starting with "gluttony"
例如,"gluttony%" -> 查詢所有以"gluttony"開頭的數據