objectbox 在android开发中的配置和使用

介绍:ObjectBox是一个超快的面向对象数据库,专为物联网和移动设备而构建。我们为小型设备提供边缘计算,允许在本地存储和处理数据,以实现高效,快速和安全的数据管理。

ObjectBox是nosql数据库,如果接触过java开发,那么ObjectBox有点类似redis。

本文主要介绍一下在安卓环境下怎么使用ObjectBox。

一、配置:

首先在android工程的 build.gradle 文件中加入

classpath "io.objectbox:objectbox-gradle-plugin:2.5.1"

objectbox 在android开发中的配置和使用_第1张图片

在 module的 build.gradle 文件中引用

apply plugin: 'io.objectbox'

objectbox 在android开发中的配置和使用_第2张图片

引用ObjectBox 的jar包

implementation "io.objectbox:objectbox-android:2.4.1"

到这里基本上配置就做好了。

 

二、使用

先初始化ObjectBox

import android.content.Context;
import com.gettyio.gim.db.entity.MyObjectBox;
import io.objectbox.BoxStore;

public class ObjectBox {

    private static BoxStore boxStore;

    public static void init(Context context){
        boxStore = MyObjectBox.builder().androidContext(context).build();
    }

    public static BoxStore getBoxStore() {
        return boxStore;
    }
}

在工程的 Application中初始化

public class MyApplication extends Application {

    private static Context mContext;


    @Override
    public void onCreate() {
        super.onCreate();
        mContext = getApplicationContext();
        ObjectBox.init(this);
    }

    public static Context getContext() {
        return mContext;
    }

}

这里提供一个增删改查的公共工具类,方便使用

import io.objectbox.Box;
import io.objectbox.Property;
import io.objectbox.query.QueryBuilder;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class CommonDao {

    private static CommonDao commonDao;


    public static CommonDao getInstance() {
        if (commonDao == null) {
            synchronized (CommonDao.class) {
                commonDao = new CommonDao();
            }
        }
        return commonDao;
    }

    private CommonDao() {

    }


    /**
     * 获取列表
     *
     * @return java.util.List
     * @params [tClass]
     */
    public  List getList(Class tClass) {
        List list = new ArrayList<>();
        Box box = ObjectBox.getBoxStore().boxFor(tClass);
        list.addAll(box.query().build().find());
        return list;
    }


    /**
     * 获取单个对象
     *
     * @return T
     * @params [tClass]
     */
    public  T getFirst(Class tClass) {
        Box box = ObjectBox.getBoxStore().boxFor(tClass);
        return box.query().build().findFirst();
    }


    /**
     * 多条件查询
     *
     * @param tClass            查询对象
     * @param propertyObjectMap 查询条件集合 key查询条件,value具体值
     **/
    public  List getList(Class tClass, Map propertyObjectMap) {

        if (propertyObjectMap == null) {
            throw new NullPointerException("propertyObjectMap is not null and value is not null.");
        }

        if (propertyObjectMap.isEmpty()) {

            throw new IllegalStateException("propertyObjectMap is not empty and value is not empty.");
        }
        List list = new ArrayList<>();
        Box box = ObjectBox.getBoxStore().boxFor(tClass);
        QueryBuilder queryBuilder = box.query();

        Iterator> iterator = propertyObjectMap.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry entry = iterator.next();
            queryBuilder.equal(entry.getKey(), entry.getValue().toString());
        }

        //PropertyQuery propertyQuery=queryBuilder.build().property();

        list.addAll(queryBuilder.build().find());
        return list;
    }


    /**
     * 统计
     *
     * @return java.lang.Long
     * @params [tClass, propertyObjectMap]
     */
    public  Long count(Class tClass, Map propertyObjectMap) {

        if (propertyObjectMap == null) {
            throw new NullPointerException("propertyObjectMap is not null and value is not null.");
        }

        if (propertyObjectMap.isEmpty()) {

            throw new IllegalStateException("propertyObjectMap is not empty and value is not empty.");
        }
        Box box = ObjectBox.getBoxStore().boxFor(tClass);
        QueryBuilder queryBuilder = box.query();
        Iterator> iterator = propertyObjectMap.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry entry = iterator.next();
            queryBuilder.equal(entry.getKey(), entry.getValue().toString());
        }

        return queryBuilder.build().count();
    }


    /**
     * 多条件模糊查找
     *
     * @param tClass            查询对象
     * @param propertyObjectMap 查询条件集合 key查询条件,value具体值
     **/
    public  List getListContains(Class tClass, Map propertyObjectMap) {

        if (propertyObjectMap == null) {
            throw new NullPointerException("propertyObjectMap is not null and value is not null.");
        }

        if (propertyObjectMap.isEmpty()) {

            throw new IllegalStateException("propertyObjectMap is not empty and value is not empty.");
        }

        List list = new ArrayList<>();
        Box box = ObjectBox.getBoxStore().boxFor(tClass);

        QueryBuilder queryBuilder = box.query();
        Iterator> iterator = propertyObjectMap.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry entry = iterator.next();
            queryBuilder.contains(entry.getKey(), entry.getValue().toString());
        }

        list.addAll(queryBuilder.build().find());
        return list;
    }


    /**
     * 保存一个数据
     *
     * @return void
     * @params [clazz, value]
     */
    public  void putData(Class clazz, Object value) {
        Box box = ObjectBox.getBoxStore().boxFor(clazz);
        box.put((T) value);

    }


    /**
     * 删除数据
     *
     * @return void
     * @params [clazz, value]
     */
    public  void deleteData(Class clazz, Object value) {
        Box box = ObjectBox.getBoxStore().boxFor(clazz);
        box.remove((T) value);
    }


    /**
     * 删除全部
     *
     * @return void
     * @params [clazz]
     */
    public  void deleteAllData(Class clazz) {
        Box box = ObjectBox.getBoxStore().boxFor(clazz);
        box.removeAll();
    }
}

 

三、需要注意的问题:

使用 objectbox 之前要先创建对应的实体类,实体类必须要用 @Entity 注解标明,且必须要指定一个 long类型的id,用@Id 注解标明。且属性必须要有对应的get/set方法。

import io.objectbox.annotation.Entity;
import io.objectbox.annotation.Id;

//必须要加注解@Entity
@Entity
public class MessageInfo {

    public MessageInfo(){

    }

    //必须要指定@id
    @Id
    private long id;

    private String identify;


    //属性必须要有对应的get/set方法,否则无法生成MyObjectBox
     public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getIdentify() {
        return identify;
    }

    public void setIdentify(String identify) {
        this.identify = identify;
    }

}

最后点击 Build -> build project ,即可生成对应的配置正常使用。

 

 

 

 

你可能感兴趣的:(Android)