簡介
- quick, lite, easy,GreenRobot 團(tuán)隊(duì)開發(fā)維護(hù)
- NoDB類型的數(shù)據(jù)庫
- 跨平臺
快速使用
- Project - build.gradle
buildscript {
ext.objectboxVersion = '2.9.1'
dependencies {
classpath "io.objectbox:objectbox-gradle-plugin:$objectboxVersion"
}
}
- Module(app) - build.gradle
apply plugin: 'io.objectbox' // after applying Android plugin
- Entity class
@Entity
data class Student(@Id var id: Long, var name: String, var age: Int) {
override fun toString(): String {
return "Student(id=$id, name='$name', age=$age)"
}
}
- Make(Build) Project to generate
MyObjectBox.java
path Mudule(app) - build - generated - source - kapt - debug - com.xx.xx.xx.MyObjectBox - Samle code in kotlin
class MainActivity : AppCompatActivity() {
private lateinit var boxStore: BoxStore
private lateinit var studentBox: Box<Student>
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// CommonUtil.requestAppPermissions(this)
boxStore = MyObjectBox.builder().androidContext(this).build()
studentBox = boxStore.boxFor(Student::class.java)
objectxTest()
}
private fun objectxTest() {
Log.d(TAG, "state: init")
queryAll()
// 0 for insert
var student = Student(0, "lunix", 27)
studentBox.put(student)
Log.d(TAG, "state: add")
val all = queryAll()
student = if (all.isEmpty()) return else all.first()
student.age = 18
studentBox.put(student)
Log.d(TAG, "state: update")
queryAll()
studentBox.remove(student)
Log.d(TAG, "state: remove")
queryAll()
}
private fun queryAll(): List<Student> {
val students = studentBox.all
if (students.isEmpty()) {
Log.d(TAG, "queryAll: NONE")
} else {
studentBox.all.forEach {
Log.d(TAG, "queryAll: $it")
}
}
return students
}
companion object {
private val TAG = "MainActivity"
}
}
QA
1. 生成 MyObjectBox.java 失敗
操作:Build - rebuild project - view build error log
...
Objectx generate MyBoxObjext failed:
> Task :app:compileDebugJavaWithJavac
The following annotation processors are not incremental: jetified-objectbox-processor-2.3.3.jar (io.objectbox:objectbox-processor:2.3.3).
Make sure all annotation processors are incremental to improve your build speed.
Note: [ObjectBox] Starting ObjectBox processor (debug: false)
...
原因:kotlin(1.3.61) 和 objectbox(2.3.3) 的版本不匹配
解決:解決不匹配的問題,將 objectbox 版本提升到 2.9.1
觸發(fā)問題:jdk版本需要在1.8+,也就是在 Module 層次的 build.gradle 中添加jdk版本指定
android {
...
compileOptions {
sourceCompatibility 1.8
targetCompatibility 1.8
}
}
進(jìn)階使用
一些注解
@Entity:對象持久化;
@Id:非負(fù)數(shù),默認(rèn):主鍵,自增,ObjectX管理;@Id(assignable = true)開啟手動管理唯一,負(fù)數(shù)和最值;id=0時(shí),默認(rèn)為新紀(jì)錄插入
@Index:這個(gè)對象中的索引。經(jīng)常大量進(jìn)行查詢的字段創(chuàng)建索引,用于提高查詢性能;
@Transient:某個(gè)字段不想被持久化,可以使用此注解,字段將不會保存到數(shù)據(jù)庫;
@NameInDb:數(shù)據(jù)庫中的字段自定義命名;
@ToOne:做一對一的關(guān)聯(lián)注解 ,此外還有一對多,多對多的關(guān)聯(lián)
@ToMany:做一對多的關(guān)聯(lián)注解;
@Backlink:表示反向關(guān)聯(lián)。(正向:老師有多個(gè)學(xué)生,反向:學(xué)生有多個(gè)老師)
數(shù)據(jù)庫存儲
默認(rèn)的存儲路徑: /data/data/com.konka.testproject/files/objectbox/objectbox/data.mdb
使用 FlatBuffer 來實(shí)現(xiàn)
事務(wù)
- 在數(shù)據(jù)操作方法中已有使用
- 優(yōu)化點(diǎn):頻繁多次操作數(shù)據(jù)庫 使用 顯形事務(wù)
數(shù)據(jù)庫瀏覽
注意Application建議只打開一次 BoxStore 也就是一個(gè)數(shù)據(jù)庫連接,建議打開的 BoxStore 封裝成單例
Module(app) build.gradle 在前面的基礎(chǔ)上添加
dependencies {
debugImplementation "io.objectbox:objectbox-android-objectbrowser:$objectboxVersion"
releaseImplementation "io.objectbox:objectbox-android:$objectboxVersion"
}
// move: behind of dependencies
apply plugin: 'io.objectbox'
MainActivity
class MainActivity : AppCompatActivity() {
// ...
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
CommonUtil.requestAppPermissions(this)
boxStore = MyObjectBox.builder().androidContext(this).build()
studentBox = boxStore.boxFor(Student::class.java)
if (BuildConfig.DEBUG) {
val started = AndroidObjectBrowser(boxStore).start(this)
Log.i("ObjectBrowser", "Started: $started")
}
}
// ...
}
運(yùn)行該模塊的apk的Android終端打開瀏覽器 http://localhost:8090/index.html 進(jìn)行瀏覽。電腦端需要進(jìn)行adb橋接后可以訪問adb forward tcp:8090 tcp:8090
查詢/結(jié)果排序
sqlite語句接口化,使用 userBox.query() 來構(gòu)建查詢語句,builder.build().find() 來獲取結(jié)果
- 多表查詢
builder.link(xxx.class).equal(xx)