GreenDao

项目导包

classpath'org.greenrobot:greendao-gradle-plugin:3.2.1'

model导包

compile 'org.greenrobot:greendao:3.2.0' compile 'org.greenrobot:greendao-generator:3.2.0'

接着在模块build.gradle的顶部添加 
apply plugin: ‘org.greenrobot.greendao’ 

Bean 类的生成(快捷键ctrl+F9)

@Entity

publicclassUser {

@Id(autoincrement = true)

private Long id;

private String name;

private String sex;

privateint age;

privateint salary;

@Generated(hash = 257789183)

public class MyApplication extends Application {
    public static DaoSession daoSession;
    public static boolean ENCRYPTED;

    @Override
    public void onCreate() {
        super.onCreate();
        DaoMaster.DevOpenHelper helper = new  DaoMaster.DevOpenHelper(this, ENCRYPTED ? "users-db-encrypted" : "users-db");
        Database db =  helper.getWritableDb();
        daoSession = new DaoMaster(db).newSession();
    }
    public DaoSession getDaoSession() {
        return daoSession;
    }
}
//得到GreenDao的查询和修改的对象
private UserBeanDao userDao;
private Query<UserBean> userBeanQuery;

DaoSession daoSession = ((App) getApplication()).getDaoSession();
userDao = daoSession.getUserBeanDao();
userBeanQuery = userDao.queryBuilder().orderAsc(UserBeanDao.Properties.Id).build();
@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.add:
            userDao.insert(new UserBean( null, "张三", 18));
            userDao.insert(new UserBean( null, "李四", 20));
            break;
        case R.id.del:
            userDao.deleteByKey((long) 1);
            break;
        case R.id.update:
            UserBean userBean = userDao.load((long) 1);
            userBean.setName("王五");
            userDao.update(userBean);
            break;
        case R.id.query:
            List<UserBean> list = userBeanQuery.list();
            query_result.setText(list.toString());
            break;
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要配置GreenDao,你需要按照以下步骤进行操作: 1. 在module级别的build.gradle文件中,将以下代码添加到文件的顶部: ``` apply plugin: 'org.greenrobot.greendao' ``` 2. 在同一个build.gradle文件中,添加以下配置到android模块下的greendao块中: ``` greendao { schemaVersion 1 // 设置数据库版本号,升级时可修改 daoPackage 'com.example.greendaodemo.db' // 设置生成的DAO、DaoMaster和DaoSession的包路径,默认与表实体所在的包路径相同 targetGenDir 'src/main/java' // 设置生成的源文件的路径,默认在build目录下的build/generated/source/greendao目录中 } ``` 3. 添加GreenDao的依赖库,将以下代码添加到dependencies块中: ``` implementation 'org.greenrobot:greendao:3.2.2' ``` 这样,你就完成了GreenDao的配置。如果需要生成GreenDao的代码,请确保你还添加了以下依赖库: ``` implementation 'org.greenrobot:greendao-generator:3.2.2' ``` 参考链接:<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [GreenDao的简单配置](https://blog.csdn.net/qq_42061290/article/details/82586342)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [GreenDao配置](https://download.csdn.net/download/a511341250/9593713)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [GreenDao配置文档](https://blog.csdn.net/chentaishan/article/details/105263937)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值