关闭

android集成greendao 3.3.0

别叨叨 1年前 ⋅ 324 阅读
  1. build.gradle(:app)配置,添加:

    apply plugin: 'org.greenrobot.greendao'

    添加引用:

    implementation 'org.greenrobot:greendao:3.3.0'

    添加greedao配置:

    greendao {
        schemaVersion 1
        daoPackage 'com.test.dao'
        targetGenDir 'src/main/java'
    }

    完整配置:

  2. build.gradle(:My Application)配置,添加:
    buildscript {
        repositories {
            jcenter()
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:7.1.2'
            classpath 'org.greenrobot:greendao-gradle-plugin:3.3.0'
        }
    }​
  3. 在项目中的实体类中添加@Entity注解:
    @Entity
    public class TestUser {
        private Long userId;
        private String nickname;
        private String headIcon;
    }​
  4. 重新构建一下项目第三步中的实体类变为:
    @Entity
    public class TestUser {
        private Long userId;
        private String nickname;
        private String headIcon;
        @Generated(hash = 1965623583)
        public TestUser(Long userId, String nickname, String headIcon) {
            this.userId = userId;
            this.nickname = nickname;
            this.headIcon = headIcon;
        }
        @Generated(hash = 925009630)
        public TestUser() {
        }
        public Long getUserId() {
            return this.userId;
        }
        public void setUserId(Long userId) {
            this.userId = userId;
        }
        public String getNickname() {
            return this.nickname;
        }
        public void setNickname(String nickname) {
            this.nickname = nickname;
        }
        public String getHeadIcon() {
            return this.headIcon;
        }
        public void setHeadIcon(String headIcon) {
            this.headIcon = headIcon;
        }
    }
    ​
    同时会在com.text.dao包下生成DaoMaster,DaoSession,TestUserDao三个类

全部评论: 0

    我有话说: