首先是greendao的配置
在工程的build.grdle中,如下:
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
classpath 'org.greenrobot:greendao-gradle-plugin:3.2.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
allprojects {
repositories {
google()
jcenter()
mavenCentral ()
}
}
}
在build.gradle(Module:app)
中添加下面代码:
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
defaultConfig {
applicationId "com.example.administrator.myapplication"
minSdkVersion 15
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
javaCompileOptions { annotationProcessorOptions { includeCompileClasspath = true } }//butterKnife
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'org.greenrobot:greendao-gradle-plugin:3.2.1'
}
}
apply plugin: 'org.greenrobot.greendao'
greendao {
schemaVersion 1//数据库版本号
daoPackage 'com.aa'//设置DaoMaster、DaoSession、Dao包名
targetGenDir 'src/main/java'//设置DaoMaster、DaoSession、Dao目录
//targetGenDirTest:设置生成单元测试目录
//generateTests:设置自动生成单元测试用例
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
//butterknife
//动态申请权限
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
compile 'io.reactivex.rxjava2:rxjava:2.0.5'
compile 'com.tbruyelle.rxpermissions2:rxpermissions:0.9.3@aar'
implementation files('libs/jxl.jar')
//greendao数据库
compile 'org.greenrobot:greendao:3.2.0'
}
Error:(41, 22) 错误: 找不到符号
符号: 方法 clearIdentityScope()
位置: 类型为DaoConfig的变量 dataDaoConfig
Error:(185, 5) 错误: 方法不会覆盖或实现超类型的方法
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details
原因是你的greendao对应的包版本不一致,DaoMaster,DaoSession都是自动生成的,有报错,一般是jar包和依赖版本不一致。按照我上面的是可以的。
GreenDao的基本配置就已经完成了,下面我们编写一个实体类。
@Entity
public class User {
@Id(autoincrement = true)
private Long id;
private String name;
private int age;
}
相关注解说明:
实体@Entity注解
schema:告知GreenDao当前实体属于哪个schema
active:标记一个实体处于活跃状态,活动实体有更新、删除和刷新方法
nameInDb:在数据库中使用的别名,默认使用的是实体的类名
indexes:定义索引,可以跨越多个列
createInDb:标记创建数据库表
工具类
package com.bestgo.callshow.db.greendao;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import com.bestgo.callshow.bean.BlackInfoCallMIssedBean;
import com.bestgo.callshow.db.greendao.gen.BlackInfoCallMIssedBeanDao;
import com.bestgo.callshow.db.greendao.gen.DaoMaster;
import com.bestgo.callshow.db.greendao.gen.DaoSession;
import org.greenrobot.greendao.query.QueryBuilder;
import java.util.List;
/**
* 作者:jiuqunZhang on 2018/4/28 21:12
* 邮箱:[email protected]
*/
public class DBManager {
private final static String dbName = "test_db";
private static DBManager mInstance;
private DaoMaster.DevOpenHelper openHelper;
private Context context;
public DBManager(Context context) {
this.context = context;
openHelper = new DaoMaster.DevOpenHelper(context, dbName, null);
}
/**
* 获取单例引用
*
* @param context
* @return
*/
public static DBManager getInstance(Context context) {
if (mInstance == null) {
synchronized (DBManager.class) {
if (mInstance == null) {
mInstance = new DBManager(context);
}
}
}
return mInstance;
}
private SQLiteDatabase getReadableDatabase() {
if (openHelper == null) {
openHelper = new DaoMaster.DevOpenHelper(context, dbName, null);
}
SQLiteDatabase db = openHelper.getReadableDatabase();
return db;
}
private SQLiteDatabase getWritableDatabase() {
if (openHelper == null) {
openHelper = new DaoMaster.DevOpenHelper(context, dbName, null);
}
SQLiteDatabase db = openHelper.getWritableDatabase();
return db;
}
/**
* 插入一条记录
*
* @param BlackInfoCallMIssedBean
*/
public void insertUser(BlackInfoCallMIssedBean BlackInfoCallMIssedBean) {
DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
DaoSession daoSession = daoMaster.newSession();
BlackInfoCallMIssedBeanDao userDao = daoSession.getBlackInfoCallMIssedBeanDao();
userDao.insert(BlackInfoCallMIssedBean);
}
public void insertUserList(List BlackInfoCallMIssedBeans) {
if (BlackInfoCallMIssedBeans == null || BlackInfoCallMIssedBeans.isEmpty()) {
return;
}
DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
DaoSession daoSession = daoMaster.newSession();
BlackInfoCallMIssedBeanDao userDao = daoSession.getBlackInfoCallMIssedBeanDao();
userDao.insertInTx(BlackInfoCallMIssedBeans);
}
public void deleteUser(BlackInfoCallMIssedBean BlackInfoCallMIssedBean) {
DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
DaoSession daoSession = daoMaster.newSession();
BlackInfoCallMIssedBeanDao userDao = daoSession.getBlackInfoCallMIssedBeanDao();
userDao.queryBuilder()
.where(BlackInfoCallMIssedBeanDao.Properties.PhoneNumber.eq(BlackInfoCallMIssedBean.getPhoneNumber()))
.buildDelete()
.executeDeleteWithoutDetachingEntities();
}
public void updateUser(BlackInfoCallMIssedBean BlackInfoCallMIssedBean) {
DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
DaoSession daoSession = daoMaster.newSession();
BlackInfoCallMIssedBeanDao userDao = daoSession.getBlackInfoCallMIssedBeanDao();
userDao.update(BlackInfoCallMIssedBean);
}
public List queryUserList() {
DaoMaster daoMaster = new DaoMaster(getReadableDatabase());
DaoSession daoSession = daoMaster.newSession();
BlackInfoCallMIssedBeanDao userDao = daoSession.getBlackInfoCallMIssedBeanDao();
QueryBuilder qb = userDao.queryBuilder().orderDesc(BlackInfoCallMIssedBeanDao.Properties.PhoneNumber);
List list = qb.list();
return list;
}
/**
* 查询用户列表
*/
public List queryUserList(String path) {
DaoMaster daoMaster = new DaoMaster(getReadableDatabase());
DaoSession daoSession = daoMaster.newSession();
BlackInfoCallMIssedBeanDao userDao = daoSession.getBlackInfoCallMIssedBeanDao();
QueryBuilder qb = userDao.queryBuilder();
qb.where(BlackInfoCallMIssedBeanDao.Properties.PhoneNumber.gt(path)).orderAsc(BlackInfoCallMIssedBeanDao.Properties.PhoneNumber);
List list = qb.list();
return list;
}
}
这是有关增删改查的管理办法。
在实体类中这样用:
DBManager.getInstance(getApplicationContext()).insertUser(blackInfoCallMIssedBean);
其他的语句类似。
实体类的生成比较简单,就是如下:
@Entity
public class BlackInfoBean {
/**
* 自增长的id
*/
@Id
private Long id;
/**
* 联系人id,相当于姓名
*/
private long contactsId;
/**
* 联系人姓名
*/
private String name;
/**
* 电话号码
*/
private String phoneNumber;
类似这个,@Entity,@Id就是GreeenDao的标志。写完属性之后就直接点击Make Project。就是自动的生成Get,Set方法还有toString。
每个人应用的场景不一样,欢迎留言, 一起 讨论。