Android整合GreenDao数据存储
Android整合GreenDao数据存储
一、添加依赖
1、project下的settings.gradle
pluginManagement {
repositories {
// gradlePluginPortal()
// google()
// mavenCentral()
maven { url 'https://maven.aliyun.com/repository/google'}
maven { url 'https://maven.aliyun.com/repository/gradle-plugin'}
maven { url 'https://maven.aliyun.com/repository/public'}
maven { url 'https://maven.aliyun.com/repository/central'}
}
}
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
// google()
// mavenCentral()
// jcenter()
maven { url 'https://maven.aliyun.com/repository/google'}
maven { url 'https://maven.aliyun.com/repository/gradle-plugin'}
maven { url 'https://maven.aliyun.com/repository/public'}
maven { url 'https://maven.aliyun.com/repository/central'}
maven { url "https://jitpack.io" }
}
}
rootProject.name = "assetApplication"
include ':app'
2、build.gradle中
buildscript {
dependencies {
classpath 'org.greenrobot:greendao-gradle-plugin:3.3.0'
}
}
3、app中的build.gradle中
//最上方
apply plugin: 'org.greenrobot.greendao'
//greendao配置
greendao {
//数据库版本号,升级时修改
schemaVersion 1
//生成的DAO,DaoMaster和DaoSession的包路径。默认与表实体所在的包路径相同
daoPackage 'com.xln.greendao'
//生成源文件的路径。默认源文件目录是在build目录中的(build/generated/source/greendao)
targetGenDir 'src/main/java'
}
//添加依赖
implementation 'org.greenrobot:greendao:3.3.0'
二、配置DaoMaster和DaoSession
//新增MyApplication文件
public class MyApp extends Application {
public InventoryDao inventoryDao=null;
/**
* 用户名
*/
private String username;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@Override
public void onCreate() {
super.onCreate();
//数据库名称
DaoMaster.DevOpenHelper devOpenHelper = new DaoMaster.DevOpenHelper(this,"asset.db",null);
SQLiteDatabase sqLiteDatabase = devOpenHelper.getWritableDatabase();
DaoMaster daoMaster = new DaoMaster(sqLiteDatabase);
DaoSession daoSession = daoMaster.newSession();
//自己的实体(第三步完成后可添加)
inventoryDao = daoSession.getInventoryDao();
}
三、配置实体类
@Entity(nameInDb = "inventory",active = true )
public class Inventory {
@Id(autoincrement = true)
@Unique
private Long id;
}
四、使用build->Make project重写构建项目
五、实现增删改查
参考:https://blog.csdn.net/qq_21154101/article/details/90679067