Android Greendao3.0框架的使用

本文详细介绍如何在Android项目中集成GreenDAO数据库,包括添加依赖、创建实体类、数据库初始化及基本的CRUD操作,并涉及数据库升级过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1:添加工程依赖,在Project build.gradle和module build.gradle分别添加

Project build.gradle如下:

 dependencies {
        classpath 'org.greenrobot:greendao-gradle-plugin:3.2.0'
    }

module build.gradle如下:

apply plugin: 'org.greenrobot.greendao'
android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"

    defaultConfig {
        ...
    }
    buildTypes {
        release {
          ...
        }
    }
    greendao{
        schemaVersion 1
        daoPackage 'com.aile.greendao.dao'
        targetGenDir 'src/main/java'
    }
}
dependencies {
    .....
    compile 'org.greenrobot:greendao:3.2.0'
}

2:根据需求新建实体类,比如User类,然后build-make project就会自动在前面定义的路径下生成一些包,比如DaoMaster- DaoSession- UserDao

public class User {
    private Long id;
    private String name;
    private int age;
    }

3:在自定义的Application里进行数据库的初始化并且在AndroidManifest.xml定义MyApplication

public class MyApplication extends Application{
    private UserDao userDao;
    private static MyApplication application = new MyApplication ();
    public static MyApplication getMyApplication () {
		return application;
	}
    public void onCreate() {
        super.onCreate();
        DaoMaster.DevOpenHelper devOpenHelper = new DaoMaster.DevOpenHelper(getApplicationContext(), 
               "user.db", null);
        DaoSession mDaoSession = new DaoMaster(devOpenHelper.getWritableDb()).newSession();
	userDao = mDaoSession.getUserDao();  
    }
    public UserDao getUserdao() {
      return userDao;
     }
    public static Context getContext(){
        return context;
    }
}

4:上面三部配置完毕,下面就是具体的增删改查操作

增加数据:

User user = new User(null,"aile",23);  
MyApplication.getUserdao().insert(user);  

删除数据:

User user = MyApplication.getUserdao().queryBuilder().where(UserDao.Properties.Name.eq("aile")).build().unique();    
if(user != null){    
    MyApplication.getUserdao().deleteByKey(user.getId());    
}

更改数据:

User user = MyApplication.getUserdao().queryBuilder().where(UserDao.Properties.Name.eq("aile")).build().unique();    
if(user != null) {    
    user.setName("mire");    
    MyApplication.getUserdao().update(user);      
} 

查询数据:

List<User> userList = MyApplication.getUserdao().queryBuilder()    
       .where(UserDao.Properties.Id.notEq(1))   
       .limit(5)    
       .build().list();  
for reach遍历出数据

5:数据库简单升级
实体类的更改-这也是升级数据库的原因,比如:
可参考:
https://blog.youkuaiyun.com/qq_35956194/article/details/79167897
https://github.com/yuweiguocn/GreenDaoUpgradeHelper

public class User {
    private Long id;
    private String name;
    private int age;
    private String sex;
    }

修改依赖库版本信息,如下:

 greendao{
        schemaVersion 2
        daoPackage 'com.aile.greendao.dao'
        targetGenDir 'src/main/java'
    }

好了,关于数据库迁移的问题,以后再说

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值