如果你在添加GreenDao依賴的過程中出現(xiàn)了如下錯誤:
那么可以參考一下如下網(wǎng)址:
https://blog.csdn.net/u013472738/article/details/72895747
我并沒有配置Project下的build.gradle炒刁,而是只配置了app下的build.gradle改览,代碼如下所示:
apply plugin: 'com.android.application'
apply plugin: 'org.greenrobot.greendao'
android {
compileSdkVersion 27
buildToolsVersion "27.0.1"
defaultConfig {
applicationId "com.example.liang.renyibo"
minSdkVersion 16
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 {
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'
})
testCompile 'junit:junit:4.12'
compile 'org.greenrobot:greendao:3.2.2'
}
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'org.greenrobot:greendao-gradle-plugin:3.2.1'//注意這個greendao的插件得比greendao庫的版本號小1才行了,我試了3.2.2版本的插件但是還是報錯杂彭,3.2.1版本的插件不報錯滥嘴。
}
}
示例代碼如下:
在MyApplication文件中
public class MyApplication extends Application {
private static DaoSession daoSession;
@Override
public void onCreate() {
DaoMaster.DevOpenHelper devOpenHelper=new DaoMaster.DevOpenHelper(this,"mydatabase");
SQLiteDatabase sqLiteDatabase=devOpenHelper.getWritableDatabase();
DaoMaster daoMaster=new DaoMaster(sqLiteDatabase);
daoSession=daoMaster.newSession();
}
public static DaoSession getDaoSession() {
return daoSession;
}
}
在MainActivity文件中
...
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.save:
Student student=new Student(2L,"laowang",24,"清徐");
getDaoSession().insert(student);
break;
case R.id.select:
List<Student>list=MyApplication.getDaoSession().loadAll(Student.class);
for(Student student1:list){
Toast.makeText(this, student1.getName()+student1.getDiZhi(), Toast.LENGTH_SHORT).show();
}
break;
case R.id.update:
Student student1=new Student(2L,"laofu",24,"孝義");
MyApplication.getDaoSession().update(student1);
break;
case R.id.delete:
Student student2=new Student(2L,"laofu",24,"孝義");
MyApplication.getDaoSession().delete(student2);//刪除數(shù)據(jù)的時候是根據(jù)主鍵的值進(jìn)行刪除的木蹬,與其他值沒有關(guān)系。
break;
default:
break;
}
...
在Student文件中
@Entity//標(biāo)記該實體類為greendao的一個表
public class Student {
@Id(autoincrement = true)//該字段為主鍵
private Long id;
@Unique//該字段的值不能重復(fù)
private String name;
@NotNull//該字段不能為空
private int age;
@Property(nameInDb = "address")//給該字段在表中起了一個列名
private String diZhi;
//**********************只需要給實體類設(shè)置好字段后若皱,然后make project镊叁,即可生成以下代碼***************************
@Generated(hash = 814485105)
public Student(Long id, String name, int age, String diZhi) {
this.id = id;
this.name = name;
this.age = age;
this.diZhi = diZhi;
}
@Generated(hash = 1556870573)
public Student() {
}
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return this.age;
}
public void setAge(int age) {
this.age = age;
}
public String getDiZhi() {
return this.diZhi;
}
public void setDiZhi(String diZhi) {
this.diZhi = diZhi;
}
}
在寫好實體類的字段后,然后build project走触,就會生成如下圖所示的文件:
參考文章:
https://blog.csdn.net/qq_30379689/article/details/54410838
官方的github地址如下:
https://github.com/greenrobot/greenDAO
另外晦譬,這個人的博客寫的文章都很好,可以看一看互广,網(wǎng)址如下:https://blog.csdn.net/qq_30379689/article/list/5