GreenDao 优点:
-
性能高,号称 Android 最快的关系型数据库
-
内存占用小
-
库文件比较小,小于 100K,编译时间低,而且可以避免 65K 方法限制
-
支持数据库加密 greendao 支持 SQLCipher 进行数据库加密 有关 SQLCipher 可以参考这篇博客 Android 数据存储之 Sqlite 采用 SQLCipher 数据库加密实战
-
简洁易用的 API
GreenDao 主要类介绍:
DaoMaster:该类是使用 greenDao 的主要入口。DaoMaster 负责控制数据库对象和管理指定数据库的 DAO 类。DaoMaster 提供了静态的方法去创建数据库表或者删除它们。他的内部类 OpenHelper、DevOpenHelper 是 SQLiteOpenHelper 的实现类,它们在 SQLite 数据库中负责创建数据库。
**DaoSession:**DaoSeesion 对象负责为指定的数据库管理所有的可用的 DAO 对象,你可以通过 getter 方法获取到 DaoSeession 的实例。DaoSession 也针对实体 bean 提供了增删改查等 api 接口,DaoSession 也保持了对标识范围的跟踪。
**DAOs:**数据获取(DAOs)持久化的对象并查询实体。 对于每个实体,greenDAO 为它生成一个 DAO 类。 它具有比 DaoSession 更多的持久化方法,例如:count,loadAll 和 insertInTx。
**Entities:**持久化对象,通常,实体是使用标准 Java 属性(如 POJO 或 JavaBean)表示数据库行的对象。
集成:
1、you need to add the greenDao Gradle plugin to your project's build.gradle
apply from:"config.gradle" buildscript { ... dependencies { ... classpath 'org.greenrobot:greendao-gradle-plugin:3.2.1' } } ...
2、you need to add the greenDao library to your module's build.gradle
apply plugin: 'com.android.application' apply plugin: 'org.greenrobot.greendao' android { ... } //greendao配置 greendao { //版本号,升级时可配置 schemaVersion 1 } dependencies { compile 'org.greenrobot:greendao:3.2.0' }
3、core initialization
// do this once, for example in your Application class helper = new DaoMaster.DevOpenHelper(this, "notes-db", null); db = helper.getWritableDatabase(); daoMaster = new DaoMaster(db); daoSession = daoMaster.newSession(); // do this in your activities/fragments to get hold of a DAO noteDao = daoSession.getNoteDao();
greenDao 注解学习笔记:
@Entity:
@Entity( // If you have more than one schema, you can tell greenDAO // to which schema an entity belongs (pick any string as a name). schema = "myschema", // Flag to make an entity "active": Active entities have update, // delete, and refresh methods. active = true, // Specifies the name of the table in the database. // By default, the name is based on the entities class name. nameInDb = "AWESOME_USERS", // Define indexes spanning multiple columns here. indexes = { @Index(value = "name DESC", unique = true) }, // Flag if the DAO should create the database table (default is true). // Set this to false, if you have multiple entities mapping to one table, // or the table creation is done outside of greenDAO. createInDb = false, // Whether an all properties constructor should be generated. // A no-args constructor is always required. generateConstructors = true, // Whether getters and setters for properties should be generated if missing. generateGettersSetters = true ) public class User { ... }
@Entity 注解标记一个 java 类将作为 greenDAO 的持久化实体。
schema:指定数据库,如果你有多个数据库时可以为表指定对应的数据库。
active:标记一个实体是否时激活的,激活状态下的实体 bean 将获得更新,删除,刷新方法。
nameInDb:指定表在数据库中的表名,默认为实体类的类名。
indexes:定义索引,可以跨越多个列
createInDb:标记是否创建数据库表
generateConstructors:标记是否所有的属性的构造方法都被生成
generateGettersSetters:标记如果属性的 setters 和 getters 方法缺少时是否自动生成。
基本属性:
@Entity public class User { @Id(autoincrement = true) private Long id; @Property(nameInDb = "USERNAME") private String name; @NotNull private int repos; @Transient private int tempUsageCount; ... }
@Id:主键 Long 型,可以通过 @Id(autoincrement = true)设置自增长
@Property:设置一个非默认关系映射所对应的列名,默认是的使用字段名 举例:@Property (nameInDb="name")
@NotNull:设置数据库表当前列不能为空
@Transient:添加次标记之后不会生成数据库表的列
@Index**:**使用 @Index 作为一个属性来创建一个索引,通过 name 设置索引别名,也可以通过 unique 给索引添加约束
@Unique**:**向数据库列添加了一个唯一的约束
**@ToOne:**定义与另一个实体(一个实体对象)的关系
**@ToMany:**定义与多个实体对象的关系
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于