preface

Recently encountered SQLite requirements, try using ObjectBox, record the usage process, code in Kotlin ~Copy the code

1. Import dependencies

Gradle add dependencies {classpath("io.objectbox:objectbox-gradle-plugin:$objectbox_version"} model Gradle adds plugins {id'io.objectbox'
}
dependencies {
    / / database
    implementation "io.objectbox:objectbox-kotlin:$objectbox_version"
}
Copy the code

2. Define entity classes

@Entity
data class BaseBean(
    @Id
    var id:Long = 0.var name: String? = null
)
Copy the code

Note: If you have made changes to the entity class, you need to rebuild it in order to update the generated ObjectBox code. In objectbox-models, you need to view it in the form of Project ~

3. Initialization

object ObjectBox {
    lateinit var store: BoxStore
        private set

    fun init(context: Context) {
        store = MyObjectBox.builder()
            .androidContext(context.applicationContext)
            .build()
    }
}
Copy the code
class MyApp : Application() {
    override fun onCreate(a) {
        super.onCreate()
        myApp = thismyApp? .let { ObjectBox.init(it)
        }
    }
    companion object {
        var myApp: MyApp? = null}}Copy the code

Create the Store utility class

Package ObjectBox, generally MMKV will also package here ~

/**
 * Created by Priscilla Cheung on 2021年11月11日22:28:04~
 */
object Store {
    val queryMap:HashMap<Class<*>,Class<*>> = hashMapOf(Pair(BaseBean::class.java,BaseBean_::class.java))

    /** * Single entry * By default, the ID of a new object is assigned by the ObjectBox. When a new object is placed, it is assigned to the next highest available ID~ * For example, if you have an object with id 1 and another object with ID 100 in a box, the next new object placed will be assigned an ID 101~ * If you try to assign a new ID and place an object yourself,ObjectBox will throw an error ~ */
    inline fun <reified T> put(bean: T) {
        try {
            ObjectBox.store.boxFor(T::class.java).put(bean)
        } catch (e: IllegalArgumentException) {
            toast("IllegalArgumentException")
            e.printStackTrace()
        }
    }

    /** * multiple entries */
    inline fun <reified T> put(bean: List<T>) =
        ObjectBox.store.boxFor(T::class.java).put(bean)

    /** * Get * by Id single@paramId Id of the query *@returnReturns an entity class */
    inline fun <reified T> get(id: Long): T = ObjectBox.store.boxFor(T::class.java).get(id)

    /** * get all *@returnReturns a collection of entity classes */
    inline fun <reified T> getAll(a): List<T> = ObjectBox.store.boxFor(T::class.java).all

    /** * Delete a single */ by Id
    inline fun <reified T> remove(id: Long) {
        try {
            ObjectBox.store.boxFor(T::class.java).remove(id)
        } catch (e: NullPointerException) {
            e.printStackTrace()
        }
    }

    /** * Delete multiple */ by Id
    inline fun <reified T> remove(id: List<Long>) {
        try {
            ObjectBox.store.boxFor(T::class.java).removeByIds(id)
        } catch (e: NullPointerException) {
            e.printStackTrace()
        }
    }

    /** * delete all */
    inline fun <reified T> removeAll(a) =
        ObjectBox.store.boxFor(T::class.java).removeAll()


Query 
      
       (userBean_.code.equal (myapp.islogin)) */
      
inline fun <reified T> query(propertyQueryCondition: PropertyQueryCondition<T>): List<T> {
    var info: ArrayList<T> = ArrayList<T>()
    ObjectBox.store.boxFor(T::class.java).query(propertyQueryCondition).build().apply {
        info.addAll(find())
        close()
    }
    return info
}

/** * Complex filter * store.queryCreate 
      
       () *.order(acceptancematerialSBean_.recordDate) *.build().start() * /
      

inline fun <reified T> queryCreate(
    propertyQueryCondition: PropertyQueryCondition<T>? = null.): QueryBuilder<T> {
    return if(propertyQueryCondition ! =null)ObjectBox.store.boxFor(T::class.java).query(propertyQueryCondition)
    else ObjectBox.store.boxFor(T::class.java).query()
}

inline fun <reified T> Query<T>.start(a): List<T> {
    var info: ArrayList<T> = ArrayList<T>()
    this.apply {
        info.addAll(find())
        close()
    }
    return info
}

    /**
     * 获取数量
     */
    inline fun <reified T> count(a): Long =
        ObjectBox.store.boxFor(T::class.java).count()

}
Copy the code

Example: