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

objectbox 在android开发中的配置和使用,第1张

概述介绍:ObjectBox是一个超快的面向对象数据库,专为物联网和移动设备而构建。我们为小型设备提供边缘计算,允许在本地存储和处理数据,以实现高效,快速和安全的数据管理。ObjectBox是nosql数据库,如果接触过java开发,那么ObjectBox有点类似redis。本文主要介绍一下在安卓环境下怎么使用O

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

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

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

一、配置

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

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

在 module的 build.gradle 文件中引用

apply plugin: 'io.objectBox'

引用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<T>     * @params [tClass]     */    public <T> List<T> getList(Class<T> tClass) {        List<T> List = new ArrayList<>();        Box<T> Box = ObjectBox.getBoxStore().BoxFor(tClass);        List.addAll(Box.query().build().find());        return List;    }    /**     * 获取单个对象     *     * @return T     * @params [tClass]     */    public <T> T getFirst(Class<T> tClass) {        Box<T> Box = ObjectBox.getBoxStore().BoxFor(tClass);        return Box.query().build().findFirst();    }    /**     * 多条件查询     *     * @param tClass            查询对象     * @param propertyObjectMap 查询条件集合 key查询条件,value具体值     **/    public <T> List<T> getList(Class<T> tClass, Map<Property, Object> 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<T> List = new ArrayList<>();        Box<T> Box = ObjectBox.getBoxStore().BoxFor(tClass);        queryBuilder queryBuilder = Box.query();        Iterator<Map.Entry<Property, Object>> iterator = propertyObjectMap.entrySet().iterator();        while (iterator.hasNext()) {            Map.Entry<Property, Object> 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 <T> Long count(Class<T> tClass, Map<Property, Object> 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<T> Box = ObjectBox.getBoxStore().BoxFor(tClass);        queryBuilder queryBuilder = Box.query();        Iterator<Map.Entry<Property, Object>> iterator = propertyObjectMap.entrySet().iterator();        while (iterator.hasNext()) {            Map.Entry<Property, Object> entry = iterator.next();            queryBuilder.equal(entry.getKey(), entry.getValue().toString());        }        return queryBuilder.build().count();    }    /**     * 多条件模糊查找     *     * @param tClass            查询对象     * @param propertyObjectMap 查询条件集合 key查询条件,value具体值     **/    public <T> List<T> getListContains(Class<T> tClass, Map<Property, Object> 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<T> List = new ArrayList<>();        Box<T> Box = ObjectBox.getBoxStore().BoxFor(tClass);        queryBuilder queryBuilder = Box.query();        Iterator<Map.Entry<Property, Object>> iterator = propertyObjectMap.entrySet().iterator();        while (iterator.hasNext()) {            Map.Entry<Property, Object> entry = iterator.next();            queryBuilder.contains(entry.getKey(), entry.getValue().toString());        }        List.addAll(queryBuilder.build().find());        return List;    }    /**     * 保存一个数据     *     * @return voID     * @params [clazz, value]     */    public <T> voID putData(Class<T> clazz, Object value) {        Box<T> Box = ObjectBox.getBoxStore().BoxFor(clazz);        Box.put((T) value);    }    /**     * 删除数据     *     * @return voID     * @params [clazz, value]     */    public <T> voID deleteData(Class<T> clazz, Object value) {        Box<T> Box = ObjectBox.getBoxStore().BoxFor(clazz);        Box.remove((T) value);    }    /**     * 删除全部     *     * @return voID     * @params [clazz]     */    public <T> voID deleteallData(Class<T> clazz) {        Box<T> 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@Entitypublic 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 ,即可生成对应的配置正常使用。

 

 

 

 

点赞收藏分享文章举报

Gogym发布了77 篇原创文章 · 获赞 62 · 访问量 39万+私信 关注 总结

以上是内存溢出为你收集整理的objectbox 在android开发中的配置和使用全部内容,希望文章能够帮你解决objectbox 在android开发中的配置和使用所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1066604.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-26
下一篇2022-05-26

发表评论

登录后才能评论

评论列表(0条)

    保存