GreenDao数据库

博客介绍了导入依赖的相关位置,包括在总的部分、Model里、最上面、Appliction中,需在需要的bean头部加入,还提及了MainActivity中。

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

先导入依赖

在总的里面

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

Model里面

最上面

apply plugin: 'org.greenrobot.greendao' // apply plugin
greendao {
        schemaVersion 1 //数据库版本号
        daoPackage 'com.example.weekone.db'
// 设置DaoMaster、DaoSession、Dao 包名
        targetGenDir 'src/main/java'//设置DaoMaster、DaoSession、Dao目录
        generateTests false //设置为true以自动生成单元测试。
        targetGenDirTests 'src/main/java' //应存储生成的单元测试的基本目录。默认为 src / androidTest / java。
    }
implementation 'org.greenrobot:greendao:3.2.2' // add library

Appliction中

public class MApp extends Application {

    private static DaoSession daoSession;
    public static DaoSession getDaoSession() {
        return daoSession;
    }
    @Override
    public void onCreate() {
        super.onCreate();
        DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "aserbao.db");
        SQLiteDatabase db = helper.getWritableDatabase();
        DaoMaster daoMaster = new DaoMaster(db);
        daoSession = daoMaster.newSession();

    }
}

在需要的bean头部加入

@Entity

MainActivity中

private DaoSession daoSession = MApp.getDaoSession();
查询
for (int i = 0; i < results1.size(); i++) {
            ResultsBean resultsBean = new ResultsBean();
            resultsBean.setCover(""+results1.get(i).getCover());
            resultsBean.setTitle(""+results1.get(i).getTitle());
            this.daoSession.insertOrReplace(resultsBean);
        }
//增加
List<ResultsBean> resultsBeans = daoSession.loadAll(ResultsBean.class);
        for (ResultsBean bean:resultsBeans){
            Log.e("zlx",""+bean.getTitle());
        }

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值