美文网首页
GreenDao的使用

GreenDao的使用

作者: 名字_都被占了 | 来源:发表于2018-06-09 23:42 被阅读0次

    如果你在添加GreenDao依赖的过程中出现了如下错误:

    那么可以参考一下如下网址:

    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);//删除数据的时候是根据主键的值进行删除的,与其他值没有关系。
                    break;
                default:
                    break;
            }
    ...
    

    在Student文件中

    @Entity//标记该实体类为greendao的一个表
    public class Student {
        @Id(autoincrement = true)//该字段为主键
        private Long id;
        @Unique//该字段的值不能重复
        private String name;
        @NotNull//该字段不能为空
        private int age;
        @Property(nameInDb = "address")//给该字段在表中起了一个列名
        private String diZhi;
        //**********************只需要给实体类设置好字段后,然后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
    另外,这个人的博客写的文章都很好,可以看一看,网址如下:https://blog.csdn.net/qq_30379689/article/list/5

    相关文章

      网友评论

          本文标题:GreenDao的使用

          本文链接:https://www.haomeiwen.com/subject/clvteftx.html