Se ha denunciado esta presentación.
Utilizamos tu perfil de LinkedIn y tus datos de actividad para personalizar los anuncios y mostrarte publicidad más relevante. Puedes cambiar tus preferencias de publicidad en cualquier momento.
GreenDao 3.0 輕鬆上手
彥彬
hung_yanbin@hotmail.com
GreenDao Features
1. Performance
2. Small SIze
3. Easy to use
4. Database Encryption
Github repo
https://github.com/hungyanbin/GreenDaoDemo
Gradle setting
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'org.greenrobot:greendao-gradle-plugin:3.1.1'
}
}
a...
Create Entity (init)
Create Entity with Annotation
@Entity
public class Student {
@Id(autoincrement = true)
private Long id;
@NonNull
private S...
DaoSession and DaoMaster
DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(context, DB_NAME);
DaoMaster daoMast...
DB operation (db_operation)
DB operations from Dao
1. Insert
Student student = new Student(null, "name");
long id = studentDao.insert(student);
2. Loa...
And Other Operations...
1. Delete
studentDao.delete(student);
2. Update
studentDao.update(student);
3. InsertOrReplace
stu...
DB upgrade (db_upgrade)
Add schema
@Entity
public class Student {
@Id(autoincrement = true)
private Long id;
@NonNull
private String name;
private...
Gradle setting
greendao {
schemaVersion 2
}
DB query (db_query)
Query by String Property
QueryBuilder<Student> studentQueryBuilder = studentDao.queryBuilder();
Property nameProperty = St...
Order by Property
QueryBuilder<Student> studentQueryBuilder = studentDao.queryBuilder();
Property ageProperty = StudentDao...
Compound query
QueryBuilder<Student> studentQueryBuilder = studentDao.queryBuilder();
Property nameProperty = StudentDao.P...
And Other Queries...
1. or()
2. gt()
3. le()
4. lt()
5. notEq()
6. etc...
Relation (relation)
@ToMany Annotation
@Entity
public class Teacher {
@Id(autoincrement = true)
private Long id;
@NonNull
private String name;...
@ToMany Annotation
@Entity
public class Teacher {
@Id(autoincrement = true)
private Long id;
@NonNull
private String name;...
@ToMany Annotation
@Entity
public class Teacher {
@Id(autoincrement = true)
private Long id;
@NonNull
private String name;...
@ToMany Annotation
@Entity
public class Teacher {
@Id(autoincrement = true)
private Long id;
@NonNull
private String name;...
@ToMany Annotation
@Entity
public class Teacher {
@Id(autoincrement = true)
private Long id;
@NonNull
private String name;...
Auto generate methods
@Generated(hash = 1942392019)
public void refresh() {
if (myDao == null) {
throw new DaoException("E...
Others
Proguard
-keepclass class * extends org.greenrobot.greendao.AbstractDao {
public static java.lang.String TABLENAME;
}
-kee...
3.1 Rx support
1. RxDao
xxDao.rx()
2. RxQuery
xxQueryBuilder.rx()
SQLCypher
還沒研究.....
Thank you!
Próxima SlideShare
Cargando en…5
×

Green dao 3.0

475 visualizaciones

Publicado el

GreenDao 3.0 easy to use

Publicado en: Tecnología
  • Sé el primero en comentar

Green dao 3.0

  1. 1. GreenDao 3.0 輕鬆上手 彥彬 hung_yanbin@hotmail.com
  2. 2. GreenDao Features 1. Performance 2. Small SIze 3. Easy to use 4. Database Encryption
  3. 3. Github repo https://github.com/hungyanbin/GreenDaoDemo
  4. 4. Gradle setting
  5. 5. buildscript { repositories { mavenCentral() } dependencies { classpath 'org.greenrobot:greendao-gradle-plugin:3.1.1' } } apply plugin: 'org.greenrobot.greendao' (一定要放在最上面) dependencies { compile 'org.greenrobot:greendao:3.1.1' }
  6. 6. Create Entity (init)
  7. 7. Create Entity with Annotation @Entity public class Student { @Id(autoincrement = true) private Long id; @NonNull private String name; }
  8. 8. DaoSession and DaoMaster DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(context, DB_NAME); DaoMaster daoMaster = new DaoMaster(helper.getWritableDatabase()); daoSession = daoMaster.newSession(); StudentDao studentDao = daoSession.getStudentDao();
  9. 9. DB operation (db_operation)
  10. 10. DB operations from Dao 1. Insert Student student = new Student(null, "name"); long id = studentDao.insert(student); 2. LoadAll List<Student> students = studentDao.loadAll();
  11. 11. And Other Operations... 1. Delete studentDao.delete(student); 2. Update studentDao.update(student); 3. InsertOrReplace studentDao.insertOrReplace(student);
  12. 12. DB upgrade (db_upgrade)
  13. 13. Add schema @Entity public class Student { @Id(autoincrement = true) private Long id; @NonNull private String name; private int age; }
  14. 14. Gradle setting greendao { schemaVersion 2 }
  15. 15. DB query (db_query)
  16. 16. Query by String Property QueryBuilder<Student> studentQueryBuilder = studentDao.queryBuilder(); Property nameProperty = StudentDao.Properties.Name; List<Student> wangStudents = studentQueryBuilder.where(nameProperty.like("王%")) .list();
  17. 17. Order by Property QueryBuilder<Student> studentQueryBuilder = studentDao.queryBuilder(); Property ageProperty = StudentDao.Properties.Age; List<Student> orderedStudents = studentQueryBuilder.orderAsc(ageProperty) .list();
  18. 18. Compound query QueryBuilder<Student> studentQueryBuilder = studentDao.queryBuilder(); Property nameProperty = StudentDao.Properties.Name; Property ageProperty = StudentDao.Properties.Age; List<Student> students = studentQueryBuilder.where( studentQueryBuilder.and(nameProperty.like("陳%"), ageProperty.ge(16))) .list();
  19. 19. And Other Queries... 1. or() 2. gt() 3. le() 4. lt() 5. notEq() 6. etc...
  20. 20. Relation (relation)
  21. 21. @ToMany Annotation @Entity public class Teacher { @Id(autoincrement = true) private Long id; @NonNull private String name; @ToMany(joinProperties = { @JoinProperty(name = "id", referencedName = "teacherId") }) private List<Student> students; }
  22. 22. @ToMany Annotation @Entity public class Teacher { @Id(autoincrement = true) private Long id; @NonNull private String name; @ToMany(joinProperties = { @JoinProperty(name = "id", referencedName = "teacherId") }) private List<Student> students; }
  23. 23. @ToMany Annotation @Entity public class Teacher { @Id(autoincrement = true) private Long id; @NonNull private String name; @ToMany(joinProperties = { @JoinProperty(name = "id", referencedName = "teacherId") }) private List<Student> students; }
  24. 24. @ToMany Annotation @Entity public class Teacher { @Id(autoincrement = true) private Long id; @NonNull private String name; @ToMany(joinProperties = { @JoinProperty(name = "id", referencedName = "teacherId") }) private List<Student> students; } @Entity public class Student { @Id(autoincrement = true) private Long id; private long teacherId; @ToOne(joinProperty = "teacherId") private Teacher teacher; @NonNull private String name; private int age; }
  25. 25. @ToMany Annotation @Entity public class Teacher { @Id(autoincrement = true) private Long id; @NonNull private String name; @ToMany(joinProperties = { @JoinProperty(name = "id", referencedName = "teacherId") }) private List<Student> students; } @Entity public class Student { @Id(autoincrement = true) private Long id; private long teacherId; @ToOne(joinProperty = "teacherId") private Teacher teacher; @NonNull private String name; private int age; }
  26. 26. Auto generate methods @Generated(hash = 1942392019) public void refresh() { if (myDao == null) { throw new DaoException("Entity is detached from DAO context"); } myDao.refresh(this); } @Generated(hash = 713229351) public void update() { if (myDao == null) { throw new DaoException("Entity is detached from DAO context"); } myDao.update(this); }
  27. 27. Others
  28. 28. Proguard -keepclass class * extends org.greenrobot.greendao.AbstractDao { public static java.lang.String TABLENAME; } -keep class **$Properties #If not integrating with SQLCipher a rule like below (or similar) needs be added: -dontwarn org.greenrobot.greendao.database.**
  29. 29. 3.1 Rx support 1. RxDao xxDao.rx() 2. RxQuery xxQueryBuilder.rx()
  30. 30. SQLCypher 還沒研究.....
  31. 31. Thank you!

×