greenDAO__가장 인기 있는 데이터베이스 프레임워크

14358 단어 유틸리티
가이드 백
프로젝트의gradle:
buildscript { repositories { jcenter() //1.greenDAO      mavenCentral() }
    dependencies { classpath 'com.android.tools.build:gradle:2.3.3' //2.greenDAO       classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2' }
}

allprojects { repositories { jcenter() }
}

task clean(type: Delete) { delete rootProject.buildDir }

module의gradle:
apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.0"

    defaultConfig {
        applicationId "com.junx.greendao"
        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'
        }
    }
}
//3.         
apply plugin: 'com.android.application'
apply plugin: 'org.greenrobot.greendao'
//4.    
greendao {
    schemaVersion 1   //      
    daoPackage 'com.koma.greendao.gen'  // GreenDao           ,              gen
    targetGenDir 'src/main/java'  //           
}

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'
    })
    compile 'com.android.support:appcompat-v7:26.+'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    testCompile 'junit:junit:4.12'

    //5.   
    compile 'org.greenrobot:greendao:3.1.0'
}

솔리드 클래스 초기화
@Entity//            
public class User {
    @Id(autoincrement = true)//  id,   Long  
    private Long id;
    @Property(nameInDb = "USERNAME")//        
    private String username;
    @Property(nameInDb = "NICKNAME")
    private String nickname;
    @Transient//  ,        
    private String useless;
}
    Build>>make project/module:      
@Entity//            
public class User {
    @Id(autoincrement = true)//  id,   Long  
    private Long id;
    @Property(nameInDb = "USERNAME")//        
    private String username;
    @Property(nameInDb = "NICKNAME")
    private String nickname;
    @Transient//        
    private String useless;
    @Generated(hash = 523935516)
    public User(Long id, String username, String nickname) {
        this.id = id;
        this.username = username;
        this.nickname = nickname;
    }
    @Generated(hash = 586692638)
    public User() {
    }
    public Long getId() {
        return this.id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getUsername() {
        return this.username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getNickname() {
        return this.nickname;
    }
    public void setNickname(String nickname) {
        this.nickname = nickname;
    }
}

코드에서 초기화
public class MyApplcation extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        mInstance = this;
        //     test.db,DevOpenHelper:  SQLite    SQLiteOpenHelper     
        DevOpenHelper helper = new DevOpenHelper(this, "test.db", null);
        //  helper          
        Database db = helper.getWritableDb();
        //        ,,       、         
        DaoMaster master = new DaoMaster(db);
        //       ,DaoSession:     Dao  ,Dao           API
        daoSession = master.newSession();
    }

    static MyApplcation mInstance;

    public static MyApplcation getInstance() {
        return mInstance;
    }


    private DaoSession daoSession;
    public DaoSession getDaoSession() {
        return daoSession;
    }
}

첨삭
Dao 객체 가져오기
//  DaoSession.getXXXDao()        Dao  
userDao = MyApplcation.getInstance().getDaoSession().getUserDao();

데이터 증가
        userDao.insert(new User());//      
        userDao.insertInTx(Iterator);//      list
        userDao.insertOrReplace();//          ,  id         
        userDao.insertOrReplaceInTx();//          

데이터 조회
                //    user
                List users = userDao.loadAll();
                //    id user
                User user = userDao.load(Id);

                //         
                //      
                Query query = userDao.queryBuilder()
                        .where(UserDao.Properties.Nickname.eq("  "),UserDao.Properties.Username.eq("  "))
                        .build();
                List users2 = query.list();
                //      
                List users3 = userDao.queryBuilder()
                        .whereOr(UserDao.Properties.Nickname.eq("  ")
                        , UserDao.Properties.Username.eq("  "))
                        .build().list();

                //             
                List users5 = userDao.queryBuilder()
                        .orderAsc(UserDao.Properties.Id)
                        .build().list();
                 userDao.queryBuilder().orderAsc(UserDao.Properties.Id)
                        .build().list();

                //      
                List user4 = userDao.queryBuilder().limit(5).build().list();
                //          
                long count = userDao.count();

데이터 삭제
                //  key,   id   
                userDao.deleteByKey(user.getId());
                //  key      
                userDao.deleteByKeyInTx();
                //       key    ,  key   id,    
                userDao.delete(new User());
                //         
                userDao.updateInTx();
                //    
                userDao.deleteAll();

                //                 

데이터베이스 업그레이드
         ,          

module gradle :
greendao {
    schemaVersion 2   //      ,                  
    daoPackage 'com.koma.greendao.gen'  // GreenDao           ,              gen
    targetGenDir 'src/main/java'  //           
}

좋은 웹페이지 즐겨찾기