objectbox

ObjectBox is a super fast mobile database that persists objects. It lets you avoid many repetitive tasks and offers a simple interface to your data.

object需要 3 以上的Android studio版本,所以开开蓝灯一顿的升级吧,然后大概就没啥问题了
http://objectbox.io/documentation/introduction/

根目录的的build.gradle:

ext.objectboxVersion ='1.5.0'上面的VERSION_CODE需要从ObjectBox的https://github.com/greenrobot/ObjectBox上获取
 
buildscript {
    ext.objectboxVersion = '1.5.0'
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.2.0-alpha13'
        classpath "io.objectbox:objectbox-gradle-plugin:$objectboxVersion"
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

模块下的的build.gradle:

apply plugin: 'com.android.application'
apply plugin: 'io.objectbox'
代码:
@Entity
public class Note {

    @Id
    long id;

    String text;
    String comment;
    Date date;


    public Note(long id, String text, String comment, Date date) {
        this.id = id;
        this.text = text;
        this.comment = comment;
        this.date = date;
    }

    public Note() {
    }

    public long getId() {
        return this.id;
    }

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

    public String getText() {
        return this.text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public String getComment() {
        return this.comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    public Date getDate() {
        return this.date;
    }

    public void setDate(Date date) {
        this.date = date;
    }


}
操作:Build-> make project

 
implementation 'io.objectbox:objectbox-android:1.5.0'
 
public class MyApplication extends Application {
    private BoxStore boxStore;

    @Override
    public void onCreate() {
        super.onCreate();
        // do this once, for example in your Application class
        boxStore = MyObjectBox.builder().androidContext(MyApplication.this).build();
        if (BuildConfig.DEBUG) {
            new AndroidObjectBrowser(boxStore).start(this);
        }
    }
    public BoxStore getBoxStore() {
        return boxStore;
    }
}
BoxStore来存储数据
.java中:

 
 
 BoxStore boxStore =((MyApplication)getApplication()).getBoxStore();
        notesBox = boxStore.boxFor(Note.class);

notesQuery = notesBox.query().order(Note_.date).build(); -----------按日期排序查询出noteQuery
 
 List <Note> notes = notesQuery.find(); ----------查询结果
 
notesBox.put(注); ----------增
notesBox.remove(note); -------- deleteAllAll

notesQuery.count(); -------------条数
notesBox.get(1).getId(); ----------根据id获取单个值
Person person = new Person ( "Joe" , "Green" ) ;
long id = box . put ( person ) ;      // Create
Person person = box . get ( id ) ;    // Read
person . setLastName ( "Black" ) ;
box . put ( person ) ;                // Update
box . remove ( person ) ;           // Delete

增删改查只能对于box维度,修改只需根据此条数据的id查出,修改后put即可,不必先remove。

These are some of the operations offered by the Box class:

  • put: Persist an object, which may overwrite an existing object with the same ID. In other words, use put  to insert or update objects (see also the docs for object IDs). When put returns, an ID will be assigned to the object. The various put overloads support putting multiple objects, which is convenient and more efficient.
  • get: Given an object’s ID, you can get it very efficiently using get. To get all objects of a type, use getAll .
  • remove: Remove a previously put object from its box (deletes it). There are method overloads to remove multiple entities, and removeAll  to remove (delete) all objects of a type.
  • count: Returns the number of objects stored in this box.
  • query: Returns a query builder. See queries for details.


点击打开链接

package test.bjucloud.objectboxtest.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import java.util.List;

import io.objectbox.Box;
import io.objectbox.BoxStore;
import io.objectbox.query.Query;
import test.bjucloud.objectboxtest.MyApplication;
import test.bjucloud.objectboxtest.R;
import test.bjucloud.objectboxtest.entity.GoodsEntity;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btn = findViewById(R.id.btn);
        BoxStore boxStore = ((MyApplication) getApplication()).getBoxStore();
        final Box<GoodsEntity> goodsEntityBox = boxStore.boxFor(GoodsEntity.class);

        if (goodsEntityBox.count() == 0) {
//             String itemName, long skuId, double skuPrice, int inventory
            goodsEntityBox.put(new GoodsEntity("花生1", 123456, 12.5, 100));
            goodsEntityBox.put(new GoodsEntity("花生2", 1234567, 12.51, 1100));
//            GoodsEntity goodsEntity = new GoodsEntity();
//            goodsEntity.setItemName("花生1");
//            goodsEntity.setSkuId(123456);
//            goodsEntity.setSkuPrice(12.5);
//            goodsEntity.setInventory(1000);
//            goodsEntityBox.put(goodsEntity);
        }
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (goodsEntityBox.count() > 0) {
                    Query<GoodsEntity> goodsEntityQuery = goodsEntityBox.query().build();
//                    goodsEntityQuery.find();
                    List<GoodsEntity> goodsEntities = goodsEntityQuery.find();
                    for (int i = 0; i < goodsEntities.size(); i++) {
                        Log.i("qqq", "id:"+goodsEntities.get(i).getId()+" itemName:"+goodsEntities.get(i).getItemName()+" skuId:"+goodsEntities.get(i).getSkuId()+" skuPrice:"+goodsEntities.get(i).getSkuPrice()+" inventory:"+goodsEntities.get(i).getInventory());
                    }
                }
            }
        });
    }
}
package test.bjucloud.objectboxtest.entity;

import java.util.Date;

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

@Entity
public class GoodsEntity {
    @Id
    long id;
    String itemName;//商品名称
    long skuId;//skuId
    double skuPrice;
    int inventory;//库存
    Date date;

    public GoodsEntity() {
    }

    public GoodsEntity(String itemName, long skuId, double skuPrice, int inventory) {
        this.itemName = itemName;
        this.skuId = skuId;
        this.skuPrice = skuPrice;
        this.inventory = inventory;
    }

    public long getId() {
        return id;
    }

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

    public String getItemName() {
        return itemName;
    }

    public void setItemName(String itemName) {
        this.itemName = itemName;
    }

    public long getSkuId() {
        return skuId;
    }

    public void setSkuId(long skuId) {
        this.skuId = skuId;
    }

    public double getSkuPrice() {
        return skuPrice;
    }

    public void setSkuPrice(double skuPrice) {
        this.skuPrice = skuPrice;
    }

    public int getInventory() {
        return inventory;
    }

    public void setInventory(int inventory) {
        this.inventory = inventory;
    }
}

05-09 16:37:21.024 29688-29688/test.bjucloud.objectboxtest I/qqq: id:1 itemName:花生1 skuId:123456 skuPrice:12.5 inventory:100
05-09 16:37:21.024 29688-29688/test.bjucloud.objectboxtest I/qqq: id:2 itemName:花生2 skuId:1234567 skuPrice:12.51 inventory:1100

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值