简介
- quick, lite, easy,GreenRobot 团队开发维护
- NoDB类型的数据库
- 跨平台
快速使用
- 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
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 {
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
触发问题:jdk版本需要在1.8+,也就是在 Module 层次的 build.gradle 中添加jdk版本指定
android {
...
compileOptions {
sourceCompatibility 1.8
targetCompatibility 1.8
}
}
进阶使用
一些注解
@Entity:对象持久化;
@Id:非负数,默认:主键,自增,ObjectX管理;@Id(assignable = true)开启手动管理唯一,负数和最值;id=0时,默认为新纪录插入
@Index:这个对象中的索引。经常大量进行查询的字段创建索引,用于提高查询性能;
@Transient:某个字段不想被持久化,可以使用此注解,字段将不会保存到数据库;
@NameInDb:数据库中的字段自定义命名;
@ToOne:做一对一的关联注解 ,此外还有一对多,多对多的关联
@ToMany:做一对多的关联注解;
@Backlink:表示反向关联。(正向:老师有多个学生,反向:学生有多个老师)
数据库存储
默认的存储路径: /data/data/com.konka.testproject/files/objectbox/objectbox/data.mdb
使用 FlatBuffer 来实现
事务
- 在数据操作方法中已有使用
- 优化点:频繁多次操作数据库 使用 显形事务
数据库浏览
注意Application建议只打开一次 BoxStore 也就是一个数据库连接,建议打开的 BoxStore 封装成单例
Module(app) build.gradle 在前面的基础上添加
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")
}
}
// ...
}
运行该模块的apk的Android终端打开浏览器 http://localhost:8090/index.html
进行浏览。电脑端需要进行adb桥接后可以访问adb forward tcp:8090 tcp:8090
查询/结果排序
sqlite语句接口化,使用 userBox.query()
来构建查询语句,builder.build().find()
来获取结果
- 多表查询
builder.link(xxx.class).equal(xx)
后续
https://docs.objectbox.io/