GreenDao學習筆記(引入GreenDao並建立資料庫)
阿新 • • 發佈:2018-12-17
近期正在學習GreenDao,總結下。
配置GreenDao
- 在工程的build.gradle中新增classpath。
buildscript { repositories { mavenCentral() google() jcenter() } dependencies { classpath 'com.android.tools.build:gradle:3.1.2' //此處新增greendao classpath 'org.greenrobot:greendao-gradle-plugin:3.2.1' // 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的build.gradle中新增apply plugin並在dependencies中新增包。新增greendao的配置
apply plugin: 'com.android.application' apply plugin: 'org.greenrobot.greendao' android { compileSdkVersion 27 defaultConfig { applicationId "demo.ldy.mvpdemo" minSdkVersion 20 targetSdkVersion 27 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:27.1.1' implementation 'com.android.support.constraint:constraint-layout:1.1.3' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' //引入greendao包 implementation 'org.greenrobot:greendao:3.2.0' } greendao{ schemaVersion=1 daoPackage 'demo.ldy.mvpdemo.retrofitmvpmovies.dao.gen' targetGenDir 'src/main/java' }
schemaVersion 為資料庫版本 daoPackage 為dao所在的包名,(在project中可以是空的,會自動建立。也可以自己手動建立) targetGenDir 為自動生成的資料庫檔案的目錄
配置完成後點選sync,至此greendao已經配置完成。
建立資料庫
- 建立實體類
@Entity public class User { @Id (autoincrement = true) private Long id ; private String name; private String age; private String sex; private String salary; }
然後make project自動生成資料庫實體類
@Id註解表明Long型別的id為資料庫表的主鍵。其中id的型別是Long不是long。
autoincrement = true 表示id是自增的。
專案目錄:
包gen及包下DaoMaster、DaoSession、UserDao 三個類為自動生成。
- 建立資料庫管理類
public class GreenDaoManager {
private DaoMaster.DevOpenHelper helper;
private DaoSession daoSession;
private DaoMaster daoMaster;
private static GreenDaoManager greenDaoManager;
private String DBNAME = "TEST_DB";
public GreenDaoManager(){
}
public void init(Context context){
helper = new DaoMaster.DevOpenHelper(context,DBNAME);
daoMaster = new DaoMaster(helper.getWritableDb());
daoSession = daoMaster.newSession();
}
public static GreenDaoManager getInstance(){
if (greenDaoManager == null){
greenDaoManager = new GreenDaoManager();
}
return greenDaoManager;
}
public DaoMaster.DevOpenHelper getDevOpenHelper(){
return helper;
}
public DaoMaster getDaoMaster() {
return daoMaster;
}
public DaoSession getDaoSession() {
return daoSession;
}
public void closeDB(){
if (helper != null){
helper.close();
}
}
}
在Application中初始化。
public class MyApplication extends Application {
private static Context context;
@Override
public void onCreate() {
super.onCreate();
context = this;
GreenDaoManager.getInstance().init(this);
}
}
資料庫建立完成,剩下的就是資料庫操作了。