目前最新版本是3.3.0
github地址https://github.com/greenrobot/greenDAO
在project gradle中 加入mavenCentral()
在dependencies 下加入classpath 'org.greenrobot:greendao-gradle-plugin:3.3.0'
完整配置如下
buildscript {
repositories {
//GreenDao
mavenCentral()
}
dependencies {
classpath "com.android.tools.build:gradle:4.0.1"
//GreenDao
classpath 'org.greenrobot:greendao-gradle-plugin:3.3.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
然后在module build.gradle中顶部添加
apply plugin: 'org.greenrobot.greendao'
依赖添加
implementation 'org.greenrobot:greendao:3.3.0'
以上gradle配置完成以后就可以使用greendao了
新建一个bean目录,目录下新建一个bean文件
@Entity
class MySports {
//@Id:主键,通过这个注解标记的字段必须是Long类型的,这个字段在数据库中表示它就是主键,并且它默认就是自增的
@Id(autoincrement = true)
private Long id;
private String longitude;
private String latitude;
private String loction;
private long distance;
}
构建(Make Project)一下,然后就可以看到自动扩充了,生成了greendao目录
在App中初始化greendao
public class App extends Application {
private static final String DB_NAME = "mysports.db";
private static DaoSession mDaoSession;
@Override
public void onCreate() {
super.onCreate();
initGreenDao();
}
private void initGreenDao() {
DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, DB_NAME);
SQLiteDatabase db = helper.getWritableDatabase();
DaoMaster daoMaster = new DaoMaster(db);
mDaoSession = daoMaster.newSession();
}
public static DaoSession getmDaoSession() {
return mDaoSession;
}
}
增,删,改,查操作
/**
* 插入1w条数据
*/
public void greenDaoInsert() {
List<MySports> mySportsList = new ArrayList<>();
for (int i = 0; i < 10000; i++) {
MySports mySports = new MySports();
mySports.setId((long)(1000000000+i));
mySports.setLatitude("40.60190744137565");
mySports.setLongitude("116.397128");
mySports.setLoction("上海市浦东区"+i);
mySports.setDistance(10000+i);
mySportsList.add(mySports);
}
long startTime = System.currentTimeMillis();
App.getmDaoSession().getMySportsDao().insertInTx(mySportsList);
long endTime = System.currentTimeMillis();
long takeTime = endTime - startTime;
tv_title.setText("greendao插入1w条数据耗时:"+takeTime);
}
/**
* 查询数据
*/
public void greenDaoQuery() {
long startTime = System.currentTimeMillis();
List<MySports> datas = App.getmDaoSession().getMySportsDao().queryBuilder().list();
long endTime = System.currentTimeMillis();
long takeTime = endTime - startTime;
tv_title.setText("greendao查询1w条数据耗时:"+takeTime+"ms");
}
/**
* 更新数据
*/
public void greenDaoUpdate() {
List<MySports> datas = App.getmDaoSession().getMySportsDao().queryBuilder().list();
for (MySports data : datas) {
data.setLoction("陕西省西安市");
}
long startTime = System.currentTimeMillis();
App.getmDaoSession().getMySportsDao().updateInTx(datas);
long endTime = System.currentTimeMillis();
long takeTime = endTime - startTime;
tv_title.setText("greendao更新1w条数据耗时:"+takeTime+"ms");
}
/**
* 删除
* @param id
* @param mySports
*/
public void greenDaoDelete(long id,MySports mySports) {
App.getmDaoSession().getMySportsDao().deleteByKey(id);
//App.getmDaoSession().getMySportsDao().delete(mySports);
//App.getmDaoSession().getMySportsDao().deleteAll();
}
GreenDao的优点
~增删改查耗时短
~依赖库很小
~支持数据库加密
~自动生成代码
~不必写原始的SQL语句,只需要在bean文件中添加需要的字段即可
网友评论