一. 什么是greenDAO
greenDao是一个将对象映射到SQLite数据库中的轻量且快速的ORM解决方案,采用注解的方式来定义实体类,通过gradle插件生成相应的代码。
二. 为什么使用greenDAO
- 体积小(100k不到)
- 性能最大化
- 内存开销最小化
- 易于使用的 API
- 支持数据库加密
三. 基本使用方法
- 引入greenDAO
//在root build.gradle引入相关插件
dependencies {
classpath 'com.android.tools.build:gradle:2.3.3'
classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2'
}
//In your app projects build.gradle file:
apply plugin: 'com.android.application'
apply plugin: 'org.greenrobot.greendao'
android {
...
greendao{
schemaVersion 1 //表示数据库版本号
daoPackage 'com.example.greendaouse.db'//自动生成代码所在包名,默认在build/generated/source/greendao
targetGenDir 'src/main/java' //生成的DAOMaster和DaoSession的位置
}
}
dependencies {
compile 'org.greenrobot:greendao:3.2.2'//引入greenDAO的类库
// 数据库加密
// compile 'net.zetetic:android-database-sqlcipher:3.5.1'
// 使用数据库升级辅助GreenDaoUpgradeHelper
// compile 'com.github.yuweiguocn:GreenDaoUpgradeHelper:v1.2.0'
}
android-database-sqlcipher Github数据库加密
GreenDaoUpgradeHelper Github数据库升级辅助
- 创建一个
UserEntity
实体类
@Entity
public class UserEntity {
@Id
private Long id;
@Property(nameInDb = "USERNAME")
private String username;
@Property(nameInDb = "NICKNAME")
private String nickname;
}
这里对Bean对象的注释进行解释
@Id:对象的Id,使用Long类型作为EntityId,否则会报错。(autoincrement = true)表示主键会自增,如果false就会使用旧值
@Entity:告诉GreenDao该对象为实体,只有被@Entity注释的类才能被dao类操作
@Property:表示该属性将作为表的一个字段
@Transient:表示该属性不会被存入数据库的字段中
@Unique:表示该属性值在数据库中是唯一值
@NotNull:表示该属性不能为空
@Generated:编译后自动生成的构造函数、方法等的注释,提示构造函数、方法等不能被修改
- 进行数据库的初始化
private DaoSession daoSession;
private void initGreenDao() {
//创建数据库greendao_use.db"
DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "greendao_use.db", null);
//获取可写数据库
SQLiteDatabase db = helper.getWritableDatabase();
//获取数据库对象
DaoMaster daoMaster = new DaoMaster(db);
//获取Dao对象管理者
daoSession = daoMaster.newSession();
}
- 基本操作方法
- 添加数据
public void insertUser() {
userEntityDao.insert(userEntity);
userEntityDao.insertOrReplace(userEntity);//添加数据,如果有重复则覆盖
userEntityDao.insertInTx(userEntityList);
userEntityDao.insertOrReplaceInTx(userEntityList);
}
- 删除数据
public void deleteUser() {
userEntityDao.delete(userEntity);//删除指定单个
userEntityDao.deleteByKey(id);//删除指定单个
userEntityDao.deleteInTx(userEntityList);//删除指定list
userEntityDao.deleteByKeyInTx(userEntityList);//删除指定list
userEntityDao.deleteAll();//删除所有
}
- 更新数据
public void updateUser() {
userEntityDao.update(userEntity);
userEntityDao.updateInTx(userEntityList);
}
- 查询数据
public void queryUser() {
userEntity = userEntityDao.queryBuilder().where(UserEntityDao.Properties.Username.eq("老板")).build().unique();
userEntity = userEntityDao.load(id);
userEntity = userEntityDao.loadByRowId(id);
userEntityList = userEntityDao.queryBuilder().list();
userEntityList = userEntityDao.loadAll();
}
四. 数据库调试Android-Debug-Database
一般我们调试手机中的数据库是很麻烦的,但利用这个库我们可以通过浏览器方便的查看的数据库。
-
特点:
1. 查看应用中所有的数据库;
2. 查看你的应用中所有shared preferences(额外福利);
3. 对你指定的数据库执行SQL语句;
4. 对你指定的数据库中的数据进行可视化的编辑;
5. 将数据库直接下载下来; -
使用方法
- Add this to your app's build.gradle
debugCompile 'com.amitshekhar.android:debug-db:1.0.1'
(这么多就好了,不需要任何其他的代码,是不是很简单) - 调试
当你在App启动的时候,注意你的logcat,会有这么一行:
D/DebugDB: Open http://XXX.XXX.X.XXX:8080,如下图
- Add this to your app's build.gradle

打开图中的链接你就可以看到你的App中的数据库界面如下:

注意:如果是虚拟机需要打开命令执行
adb forward tcp:8080 tcp:8080
,如果是手机直接打开链接即可
网友评论