配置参考github官方https://github.com/greenrobot/greenDAO
翻墙之后配置会快很多,否则会很慢且容易出错
首先在build.gradle
repositories {
google()
jcenter()
mavenCentral() // add repository
}
classpath 'org.greenrobot:greendao-gradle-plugin:3.3.0'
在build.gradle(app中)上部
apply plugin: 'org.greenrobot.greendao'
添加依赖
implementation 'org.greenrobot:greendao:3.3.0' // add library
在BaseApplication中的初始化
public class BaseApplication extends Application {
private static Context sContext = null;
private static Handler sHandler = null;
private DaoMaster.DevOpenHelper mDevOpenHelper;
private DaoMaster mDaoMaster;
private DaoSession mDaoSession;
private SQLiteDatabase mDb;
private static BaseApplication sApplication;
@Override
public void onCreate() {
super.onCreate();
sApplication = this;
sContext = getBaseContext();
sHandler = new Handler();
initData();
}
public static BaseApplication getInstance(){
return sApplication;
}
private void initData() {
mDevOpenHelper = new DaoMaster.DevOpenHelper(this,"test-db",null);
mDb = mDevOpenHelper.getWritableDatabase();
mDaoMaster = new DaoMaster(mDb);
mDaoSession = mDaoMaster.newSession();
}
public DaoSession getDaoSession(){
return mDaoSession;
}
public SQLiteDatabase getDb() {
return mDb;
}
public static Context getContext() {
return sContext;
}
public static Handler getHandler() {
return sHandler;
}
}
android:name=".base.BaseApplication"
创建实体类
@Entity
public class GreenDaoItem {
@Id
private Long id;
@Index
private String perNo;
@Property(nameInDb = "USERNAME")
private String name;
@Property(nameInDb = "SEX")
private String sex;
}
然后点击rebuild Project就可以自动补全实体类中的构造方法和getter,setter
@Entity
public class GreenDaoItem {
@Id
private Long id;
@Index
private String perNo;
@Property(nameInDb = "USERNAME")
private String name;
@Property(nameInDb = "SEX")
private String sex;
@Generated(hash = 246239292)
public GreenDaoItem(Long id, String perNo, String name, String sex) {
this.id = id;
this.perNo = perNo;
this.name = name;
this.sex = sex;
}
@Generated(hash = 922739932)
public GreenDaoItem() {
}
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public String getPerNo() {
return this.perNo;
}
public void setPerNo(String perNo) {
this.perNo = perNo;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return this.sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
在代码中调用
mGreenDaoItemDao = BaseApplication.getInstance().getDaoSession().getGreenDaoItemDao();
有四个按钮用来增删查改
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.insert_fg_more:
final GreenDaoItem greenDaoItem = new GreenDaoItem(101l, "123", "Tom", "男");
mGreenDaoItemDao.insertOrReplace(greenDaoItem);
final GreenDaoItem greenDaoItem2 = new GreenDaoItem(102l, "124", "123", "女");
mGreenDaoItemDao.insertOrReplace(greenDaoItem2);
break;
case R.id.delete_fg_more:
mGreenDaoItemDao.deleteByKey(102l);
break;
case R.id.update_fg_more:
final GreenDaoItem greenDaoItem1 = new GreenDaoItem(102l, "1234", "1235", "1235");
mGreenDaoItemDao.update(greenDaoItem1);
break;
case R.id.query_fg_more:
List<GreenDaoItem> greenDaoItems = mGreenDaoItemDao.loadAll();
for (GreenDaoItem daoItem : greenDaoItems) {
Log.e(TAG, "onClick: "+daoItem.getId() +" "+ daoItem.getPerNo() );
}
break;
}
}