Android GreenDao基本使用
8096 ワード
転載先:http://blog.csdn.net/fengltxx/article/details/53708769ステップ1:GreenDaoをプロジェクトに導入する(Studioを例に) 1.プロジェクト->budld.gradleでは、 として構成されています. 2.Modle->build.gradleでの構成:`apply plugin:'com.android.アプリケーション//これは構成する必要がある場所です-1 apply plugin:'org.greenrobot.greendao’//GreenDao の配置が完了すると、同期工事が可能となる.ステップ2:エンティティークラス の作成
Schoolクラス:この普通の実体クラスを作成した後.
ナビゲーションバーのBuild->Make Projectをクリックします.その後、この実体類が変身したのを見ることができます.次のようになります.
コードの一部を生成してくれました
注意:コードを生成した後、このファイルを変更しないでください.
GreenDaoはDAOなどを生成してくれましたDAO位置は以下の第3歩:CRUD の削除を開始
第1種:SqliteOpenHelperオブジェクトを使用して操作する.
SQLiteDatabaseオブジェクトを取得した以上、sql操作を直接使用することができる.GreenDaoが私たちにSQLiteDatabaseを簡単に手に入れるだけだと思ったら、それはあまりにも軽視されています.hibernateのようにオブジェクトを直接操作してCRUDを行うこともできる.第2種:DaoSessionを用いるオブジェクトに直接CRUDを行う. 増: 削除: 改: 調べ:
簡単なGreenDao簡単demoはもうOK!次に、ソリッドクラスの注釈について説明します.
dependencies {
classpath 'com.android.tools.build:gradle:2.0.0'
//
classpath 'org.greenrobot:greendao-gradle-plugin:3.2.1'//GreenDao
}
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
...
}
// -----2
greendao {
schemaVersion 1 //
//daoPackage 'com.greendao.gen' // DAO,DaoMaster DaoSession 。 。
//targetGenDir 'src/main/java' // 。 build (build/generated/source/greendao)。
// targetGenDirTest // 。
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
// -----3 GreenDao
compile 'org.greenrobot:greendao:3.2.0'//GreenDao
compile'org.greenrobot:greendao-generator:3.0.0'
}`
Schoolクラス:
@Entity
public class School {
@Id
private Long id;
private String name;
}
ナビゲーションバーのBuild->Make Projectをクリックします.その後、この実体類が変身したのを見ることができます.次のようになります.
/**
* ONE Goal,ONE Passion!
* Created by ${PK_Night} on 2016/12/12.
* comment:
*/
@Entity
public class School {
@Id
private Long id;
private String name;
@Transient
private int tempUsageCount; // not persisted
@Generated(hash = 2095953704)
public School(Long id, String name) {
this.id = id;
this.name = name;
}
@Generated(hash = 1579966795)
public School() {
}
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;
}
}
コードの一部を生成してくれました
注意:コードを生成した後、このファイルを変更しないでください.
GreenDaoはDAOなどを生成してくれましたDAO位置は以下の
第1種:SqliteOpenHelperオブジェクトを使用して操作する.
DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "mydb", null);
SQLiteDatabase db = helper.getWritableDatabase();
// sql
// String sql = "insert into school values(3,'txx')";
// db.execSQL(sql); // , ,
//db.rawQuery();
SQLiteDatabaseオブジェクトを取得した以上、sql操作を直接使用することができる.GreenDaoが私たちにSQLiteDatabaseを簡単に手に入れるだけだと思ったら、それはあまりにも軽視されています.hibernateのようにオブジェクトを直接操作してCRUDを行うこともできる.
DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "mydb2", null);
SQLiteDatabase db = helper.getWritableDatabase();
DaoMaster daoMaster = new DaoMaster(db);
DaoSession session = daoMaster.newSession();
school = new School((long)1,"fy");
session.insert(school);
school = new School((long)1,"fy");
session.delete(school);
school = new School((long) 1, "fynight");
session.update(school);
//
List schools = session.queryRaw(School.class, "where name = ?", new String[]{"fy"});
for (int i = 0; i < schools.size(); i++) {
System.out.println(" " + schools.get(i).getName());
}
//
List school = session.loadAll(School.class);
String schoolName = "";
for (int i = 0; i < school.size(); i++) {
schoolName += school.get(i).getName() + ",";
System.out.println(" " + schoolName);
}
簡単なGreenDao簡単demoはもうOK!次に、ソリッドクラスの注釈について説明します.