ObjectBox使用簡記

這兩天聽朋友說起了最近Android出來了一個NoSql數(shù)據(jù)庫ObjectBox,懷揣著好奇就建了一個Android程序進行了簡單的運用和測試,感覺效果很好,特此記~

  • 此文主要供 Kotlin使用ObjectBox

1.添加依賴

  • 在project的build.gradle添加如下配置
buildscript {
   //目前版本是1.4.1
    ext.objectboxVersion = '1.4.1'
    ext.kotlin_version = '1.2.10'
    repositories {
        google()
        jcenter()
      maven { url "http://objectbox.net/beta-repo/" }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.1'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
        classpath "io.objectbox:objectbox-gradle-plugin:$objectboxVersion"
    }
}

allprojects {
    repositories {
        google()
        jcenter()
        maven { url "http://objectbox.net/beta-repo/" }
    }
}
  • 在app的build.gradle中需要添加如下配置
apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

apply plugin: 'kotlin-kapt'


//此依賴主要用于browser,可以在瀏覽器中查看app的數(shù)據(jù)
dependencies {
  debugImplementation "io.objectbox:objectbox-android-objectbrowser:$objectboxVersion"
  releaseImplementation "io.objectbox:objectbox-android:$objectboxVersion"
}

//一定要在 上面兩個依賴之下 此plugin
apply plugin: 'io.objectbox' // after applying Android plugin


//在正常的dependencies中添加如下依賴
dependencies{
//主要是針對kotlin
compile "io.objectbox:objectbox-kotlin:$objectboxVersion"
}
  • 在應用中的正常使用(建議在Applicaion中初始化),以下是初始化(我也是初學kotlin可能存在一些語法不夠簡潔,不喜勿噴哈):
class App : Application() {
  override fun onCreate() {
    super.onCreate()
    initObjectBox()
  }

  //定義靜態(tài)塊 靜態(tài)方法
  companion object {
    lateinit var boxStore: BoxStore
    fun getBoxStoreInstance() = boxStore
  }

  private fun initObjectBox() {
    boxStore = MyObjectBox.builder().androidContext(this).build()
    if (BuildConfig.DEBUG) {
      boxStore?.let {
        //可以理解為初始化連接瀏覽器(可以在瀏覽器中查看數(shù)據(jù),下面再說)
        val started = AndroidObjectBrowser(boxStore).start(this)
        Log.i("ObjectBrowser", "Started: " + started)
      }
    }

  }
}
  • 創(chuàng)建實體類
//實體類學生
@Entity
class Student{
  @Id var id:Long = 0
  lateinit var name:String

  //這里是假設學生和老師的關(guān)系(relation)是一對一
  lateinit var teacher:ToOne<Teacher>
}

//實體類老師
@Entity
class Teacher{
  @Id var  id:Long =0

  lateinit var name:String

  //這里假設老師和學生的關(guān)系是一對多(一個老師有多個學生)
  @Backlink
  lateinit var students: ToMany<Student>
}
  • 基本使用方法
//此處是我在Application里面做了初始化操作
var boxStore = App.getBoxStoreInstance()

//如果沒有在Application里面初始化,使用的地方初始化可以這樣操作,MyObjectBox需要build工程之后才會出來
//var boxStore = MyObjectBox.builder().androidContext(this).build()
var studentBox: Box<Student> = boxStore.boxFor<Student>()
var teacherBox: Box<Teacher> = boxStore.boxFor<Teacher>()

//創(chuàng)建一個老師對象和一個學生對象
var teacher = Teacher()
teacher.name = "我是老師"
//學生和老師的relation是1對1 如果沒有關(guān)系則不用這一步
//student.teacher.target = teacher

var student = Student()
student.name = "我是學生"


//創(chuàng)建一個學生列表
var allStudents: MutableList<Student> = mutableListOf()

//老師和學生是一對多的關(guān)系,不用relation則可以不用次關(guān)系
teacher.students.addAll(allStudents)

//以下是對數(shù)據(jù)的基本操作
//- 添加數(shù)據(jù)(可以單個數(shù)據(jù)添加,也可以添加一個列表數(shù)據(jù))
studentBox.put(student)
studentBox.put(mutableListOf())

//查詢數(shù)據(jù)的幾種方式
1.get方式查詢數(shù)據(jù)(可以傳入id,id列表等等,具體查看api)
var student=studentBox.get(id)

2.find方式(傳入你Entity對應的屬性和值進行查詢,Student_構(gòu)建會自動生成)
var findStudents: List<Student> = studentBox.find(Student_.name, "張三")

3.query方式(適用于復雜查詢可以拼接各種條件,如下簡單拼接)
var queryStudents: List<Student> = studentBox.query()
        .equal(Student_.name, "張三")
        .between(Student_.id, 1, 10)
        .build()
        .find();

//更新數(shù)據(jù)的操作和插入的操作相同都是put
studentBox.put(student)

//刪除數(shù)據(jù)的操作(可以單個id,多個id,單個對象,對象列表等)
studentBox.remove(id)
studentBox.remove(id1,id2...)
studentBox.remove(student)
studentBox.remove(mutableListOf())
  • 其他操作(browser查看數(shù)據(jù)),上面依賴其實已經(jīng)添加過也加了注釋,但是在這還是做個補充吧.
//1.app的build.gradle中加入如下依賴
dependencies {
    debugImplementation "io.objectbox:objectbox-android-objectbrowser:$objectboxVersion"
    releaseImplementation "io.objectbox:objectbox-android:$objectboxVersion"
}
//一定要在 上面兩個依賴之下 此plugin
apply plugin: 'io.objectbox'



//2.代碼中加入如下代碼(加入到初始化之后)
if (BuildConfig.DEBUG) {
      boxStore?.let {
        val started = AndroidObjectBrowser(boxStore).start(this)
        Log.i("ObjectBrowser", "Started: " + started)
      }


//3.在manifest中添加網(wǎng)絡訪問權(quán)限
<uses-permission android:name="android.permission.INTERNET" />

4.以上已經(jīng)完成基本瀏覽數(shù)據(jù)的配置,如果Android手機當前引用的消息通知是打開的會打開的,
則會收到一條通知點擊通知則瀏覽器會打開數(shù)據(jù)瀏覽。如果需要瀏覽器打開則需要轉(zhuǎn)發(fā)端口

adb forward tcp:8090 tcp:8090

瀏覽器可以訪問 http://localhost:8090/index.html 直接打開
  • 溫馨提示
1.@Id var  id:Long =0 表示ID從1開始 自增長 且必須指定為Long類型

2.數(shù)據(jù)的存儲位置為/data/data/com.xx.xx(應用包名)/objectbox/

如上即學習ObjectBox一點簡記,如有錯誤歡迎指正~

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,937評論 25 709
  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 47,262評論 6 342
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,538評論 19 139
  • 蟲鳴的夜晚, 是獨一無二的旋律。 輕緩的步子, 帶動拂面的春風。 一股春的氣息, 活躍著每一寸神經(jīng)。 窗外的夜晚,...
    小葵葵的大愿望閱讀 285評論 0 1
  • 三原則:圖原創(chuàng),文原創(chuàng),詩原創(chuàng)。 大夢誰先覺 平生我自知 吾非諸葛亮 圓滾滾一只 本居山野外 家鄉(xiāng)在四川 奈何成國...
    秋水飲馬閱讀 617評論 22 22

友情鏈接更多精彩內(nèi)容