1.先配置项目的builder.gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { repositories { google() jcenter() } dependencies { classpath 'com.android.tools.build:gradle:3.0.0' classpath 'org.greenrobot:greendao-gradle-plugin:3.0.0' // 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 }
2.配置module.app的builder.gradle
apply plugin: 'com.android.application' apply plugin: 'org.greenrobot.greendao' android { compileSdkVersion 26 defaultConfig { applicationId "com.lingdangmao.demo_zidingyi_textview" 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' } } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'com.android.support:appcompat-v7:26.1.0' implementation 'com.android.support.constraint:constraint-layout:1.0.2' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.1' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1' //greendao api 'org.greenrobot:greendao:3.0.1' api 'org.greenrobot:greendao-generator:3.0.0' } greendao { schemaVersion 1 daoPackage 'com.anye.greendao.gen' targetGenDir 'src/main/java' }
3.创建一个用户的实例类,创建完之后,编译一下,会自动生成文件
@Entity public class User { @Id private Long id; private String name; }
4.编写myapplication类继承application
public class MyApplication extends Application { private DaoMaster.DevOpenHelper mHelper; private SQLiteDatabase db; private DaoMaster mDaoMaster; private DaoSession mDaoSession; public static MyApplication instance; public static MyApplication getInstance(){ return instance; } @Override public void onCreate() { super.onCreate(); instance =this; setDataBase(); } private void setDataBase(){ mHelper = new DaoMaster.DevOpenHelper(this, "notes-db", null); db = mHelper.getWritableDatabase(); // 注意:该数据库连接属于 DaoMaster,所以多个 Session 指的是相同的数据库连接。 mDaoMaster = new DaoMaster(db); mDaoSession = mDaoMaster.newSession(); } public DaoSession getmDaoSession() { return mDaoSession; } public SQLiteDatabase getDb() { return db; } }
5.在activity中操作
//根据条件查询 UserDao mUserdao = MyApplication.getInstance().getmDaoSession().getUserDao(); Query<User> query = mUserdao.queryBuilder().where(UserDao.Properties.Id.eq(21)).build(); User u= query.unique(); //没有就是null Log.d(TAG, "onCreate11: "+u); Log.d(TAG, "onCreate11: "+u.getId()); Log.d(TAG, "onCreate11: "+u.getName()); //添加 User mUser=new User((long)2,"mike"); //mUserdao.insert(mUser); //删除 //long id= 2; //mUserdao.deleteByKey(id); //改 mUser = new User((long)2,"anye0803"); mUserdao.update(mUser); //查询 List<User> users = mUserdao.loadAll(); for(int i=0;i<users.size();i++){ Log.d(TAG, "onCreate: "+users.get(i).getId()); Log.d(TAG, "onCreate: 1"+users.get(i).getName()); }
完成,上面是最基本的,实际开发中用到再看