Note: for new apps we recommend ObjectBox, a new object-oriented database that is much faster than SQLite and easier to use. For existing apps based on greenDAO we offer DaoCompat for an easy switch (see also the announcement).
1.上代码(项目跟目录脚本)
// Top-level build file where you can add configuration options common to all sub-projects/modules. apply plugin: 'java' apply plugin: 'io.objectbox' buildscript { // ext.objectboxVersion = '1.2.1' repositories { jcenter() maven { url "http://objectbox.net/beta-repo/" } } dependencies { classpath 'com.android.tools.build:gradle:2.2.0' classpath "io.objectbox:objectbox-gradle-plugin:1.2.1" // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } allprojects { repositories { jcenter() maven { url "http://objectbox.net/beta-repo/" } } } task clean(type: Delete) { delete rootProject.buildDir }2.APP 目录的
apply plugin: 'com.android.application' android { compileSdkVersion 26 buildToolsVersion "26.0.2" defaultConfig { applicationId "com.org.gsc.opengldemo" minSdkVersion 15 targetSdkVersion 26 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } configurations.all { resolutionStrategy.force 'com.google.code.findbugs:jsr305:1.3.9' } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) // all below should be added automatically by the plugin compile "io.objectbox:objectbox-android:1.2.1" // some useful Kotlin extension functions // compile "io.objectbox:objectbox-kotlin:$objectboxVersion" annotationProcessor "io.objectbox:objectbox-processor:1.2.1" // When using Kotlin use kapt instead: // kapt "io.objectbox:objectbox-processor:$objectboxVersion" compile 'com.android.support:appcompat-v7:26.0.0-alpha1' testCompile 'junit:junit:4.12' }3. 初始化盒子
package com.org.gsc.opengldemo; import android.app.Application; import io.objectbox.BoxStore; /** * Created by qundui on 2017/11/20. */ public class MyAPP extends Application { private BoxStore boxStore; public BoxStore getBoxStore() { return boxStore; } @Override public void onCreate() { super.onCreate(); // boxStore = MyObjectBox.builder().androidContext(App.this).build(); boxStore = MyObjectBox.builder().androidContext(MyAPP.this).build();//初始化ORM 盒子 do this in your activities/fragments to get hold of a Box // notesBox = ((MyAPP) geta).getBoxStore().boxFor(Student.class); } }
4.act 中数据库操作
package com.org.gsc.opengldemo; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import java.util.List; import io.objectbox.Box; import io.objectbox.query.Query; public class MainActivity extends AppCompatActivity { private Box<Student> notesBox; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); notesBox = ((MyAPP) getApplication()).getBoxStore().boxFor(Student.class);//获取Bean 模型 } public void addData(View view){ Student student=new Student(); // student.id=0; student.name="fdy"; notesBox.put(student);//添加 Log.d("gsc",""+student.getId()); // Student student1 = notesBox.get(8); // System.out.println("----读取--"+student1.getName());//查询 // student1.setName("gsc");//修改 // System.out.println("----读取修改后的--"+student1.getName()); //删除 // notesBox.remove(student1);//删除 Query query = notesBox.query() .equal(Student_.name,"fdy").build(); List<Student> persons = query.find(); for(int i=0;i<persons.size();i++){ Log.d("gsc",""+persons.get(i).getId()); } } }4 项目Bean 表参考
package com.org.gsc.opengldemo; import io.objectbox.annotation.Entity; import io.objectbox.annotation.Id; /** * Created by qundui on 2017/11/20. */ @Entity public class Student { @Id long id; String name; public long getId() { return id; } public void setId(long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Student{" + "id=" + id + ", name='" + name + '\'' + '}'; } }