GreenDao 是一款操作数据库的神器,经过了2.0版本的升级后,已经被广泛的开发者使用。
GitHub 地址:greenDAO
优点- 最佳性能 (可能是 Android 中最快的 ORM) ,基准测试也是开源的;
- 易于使用的功能强大的 api,涵盖关系和连接;
- 最小的内存消耗;
- 小型库大小(< 100KB) ,以保持较低的构建时间,并避免65k 方法限制;
- 数据库加密:greenDAO 支持 SQLCipher 来保证用户数据的安全;
- 强大而活跃的社区交流支持。
1. 需要在工程(Project)的 build.gradle 中添加依赖:
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath "com.android.tools.build:gradle:4.2.1"
classpath 'org.greenrobot:greendao-gradle-plugin:3.3.0'
}
}
2. 在 app 目录下 build.gradle 添加依赖:
plugins {
id 'com.android.application'
id 'org.greenrobot.greendao'
}
--------------------------------------
// greendao
implementation 'org.greenrobot:greendao:3.3.0'
// RecyclerAdapter
implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.30'
----------------------------------------------
greendao {
schemaVersion 1 //当前数据库版本
}
三、GreenDao 的使用
3.1 效果图
GreenDao 需要创建 Bean 对象之后,该 Bean 对象就是表名,而它的属性值就是字段名,其实现是通过注释的方式来实现的,下面是购物车的 Bean 对象(每个 Bean 对象对应一张表)
/**
* Created on 2022/6/7 14:58
*
* @author Gong Youqiang
*/
@Entity
public class Shop {
public static final int TYPE_CART = 0x01;
public static final int TYPE_LOVE = 0x02;
//不能用int
@Id(autoincrement = true)
private Long id;
@Unique
private String name;
@Property(nameInDb = "price")
private String price;
private int sell_num;
private String image_url;
private String address;
private int type;
@Generated(hash = 1304458862)
public Shop(Long id, String name, String price, int sell_num, String image_url,
String address, int type) {
this.id = id;
this.name = name;
this.price = price;
this.sell_num = sell_num;
this.image_url = image_url;
this.address = address;
this.type = type;
}
@Generated(hash = 633476670)
public Shop() {
}
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;
}
public String getPrice() {
return this.price;
}
public void setPrice(String price) {
this.price = price;
}
public int getSell_num() {
return this.sell_num;
}
public void setSell_num(int sell_num) {
this.sell_num = sell_num;
}
public String getImage_url() {
return this.image_url;
}
public void setImage_url(String image_url) {
this.image_url = image_url;
}
public String getAddress() {
return this.address;
}
public void setAddress(String address) {
this.address = address;
}
public int getType() {
return this.type;
}
public void setType(int type) {
this.type = type;
}
}
这里需要注意的是,创建完成之后,(点击锤子图标)需要 build gradle 来完成我们的代码自动生成:
- Bean 实体的构造方法和 get、set 方法
- DaoMaster、DaoSession、DAOS类
/**
* Created on 2022/6/7 15:01
*
* @author Gong Youqiang
*/
public class MyApp extends Application {
private static DaoSession daoSession;
@Override
public void onCreate() {
super.onCreate();
//配置数据库
setupDatabase();
}
/**
* 配置数据库
*/
private void setupDatabase() {
DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "shop.db", null);
SQLiteDatabase db = helper.getWritableDatabase();
DaoMaster daoMaster = new DaoMaster(db);
daoSession = daoMaster.newSession();
}
public static DaoSession getDaoInstant() {
return daoSession;
}
}
3.3 GreenDao 增删改查
/**
* Created on 2022/6/7 15:00
*
* @author Gong Youqiang
*/
public class LoveDao {
/**
* 添加数据
*
* @param shop
*/
public static void insertLove(Shop shop) {
MyApp.getDaoInstant().getShopDao().insert(shop);
}
/**
* 删除数据
*
* @param id
*/
public static void deleteLove(long id) {
MyApp.getDaoInstant().getShopDao().deleteByKey(id);
}
/**
* 更新数据
*
* @param shop
*/
public static void updateLove(Shop shop) {
MyApp.getDaoInstant().getShopDao().update(shop);
}
/**
* 查询条件为Type=TYPE_LOVE的数据
*
* @return
*/
public static List queryLove() {
return MyApp.getDaoInstant().getShopDao().queryBuilder().where(ShopDao.Properties.Type.eq(Shop.TYPE_LOVE)).list();
}
}
四、小结
关于 GreenDao 的的基本概念与基本操作就讲到这里,更多对于 GreenDao 的数据库操作还需要多多从实战中去探索,这里只是一个快速入门的引导 .GreenDao 高级操作还包括有:多表查询、多表关联、session 缓存等用法,可以到 GreenDao 的官网进行学习。