public class GreenDaoUtil { private static DaoSession daoSession = null; public static DaoSession getDaoSession(Context context) { if (daoSession == null) { daoSession = DaoMaster.newDevSession(context, "bw.db"); } // DaoSession daoSession = GreenDaoUtil.getDaoSession(this); // GreenDaoTableDao greenDaoTableDao = daoSession.getGreenDaoTableDao(); // 通过greenDaoTableDao来进行数据库的操作 return GreenDaoUtil.daoSession; } }
//项目build.gradle文件中添加 classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2'
//app gradle 中 放到 dependencies 的外边 greendao { schemaVersion 3 //数据库版本号 daoPackage 'com.example.项目名.database' //设置时生成代码的目录 targetGenDir 'src/main/java' //设置DaoMaster、DaoSession、Dao目录 } //app 的build.gradle文件中添加
apply plugin: 'org.greenrobot.greendao'
//greenDao依赖 implementation 'org.greenrobot:greendao:3.2.2' implementation 'org.greenrobot:greendao-generator:3.2.2'