greenDao基本使用步驟
- API文檔 鏈接
- 第一步:接入jar包
- app的build.gradle中
dependencies 的節(jié)點(diǎn)下添加依賴
compile 'org.greenrobot:greendao:3.2.2'
compile 'org.greenrobot:greendao-generator:3.2.2'
- 在project的build.gradle中
buildscript 下的 dependencies中添加
classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2'
allprojects 下的 repositories 中添加
mavenCentral() // add repository
- 新建的AndroidLib庫中
lib 的build.gradle 中最上面添加:
apply plugin: 'org.greenrobot.greendao'
在android節(jié)點(diǎn)下添加:greendao配置文件
greendao {
schemaVersion 2
daoPackage 'com.mrko.dao'
targetGenDir 'src/main/java'
}
dependencies 節(jié)點(diǎn)下添加:greenDao依賴
compile 'org.greenrobot:greendao:3.2.2'
compile 'org.greenrobot:greendao-generator:3.2.2'
- 將新建的AndroidLib庫依賴到APP項(xiàng)目中、
- 第二步:新建AndroidLib。實(shí)現(xiàn)封裝Utils
只有建立完成之后才能去進(jìn)行build.gradle配置(也就是第一步的3.4點(diǎn))
- 新建一個(gè)module-->選擇Android jar。
- 進(jìn)行第一步中的序號(hào)3進(jìn)行g(shù)radle的配置。
- 新建一個(gè)DBApplication。代碼如下
DBApplication這繼承的是app的Application
本身AndroidManifest.xml不做處理
package com.mrko;
import android.app.Application;
import com.mrko.dao.DaoMaster;
import com.mrko.dao.DaoSession;
import org.greenrobot.greendao.database.Database;
/**
* Created by Mrko on 2017/9/19-10:16.
* Email:mrko0630@163.com
*/
public class DBApplication extends Application {
//定義數(shù)據(jù)庫名稱
public static final String DB_NAME = "Stuent.db";
private DaoSession session;
//獲取本地的一個(gè)實(shí)例
private static DBApplication DBApplication;
@Override
public void onCreate() {
super.onCreate();
DBApplication = this;
initDB();
}
/**
* 初始化
* 1.先獲取DexOpenHelper的實(shí)例。新建一個(gè)數(shù)據(jù)庫表
* 2.獲取dataBase的寫入權(quán)限
* 3.新建一個(gè)DaoMaster類曹宴。將dataBase交給其管理
* 4.獲取到Session的實(shí)例(session就是操作數(shù)據(jù)庫的直接類)
*/
public void initDB() {
DaoMaster.DevOpenHelper devOpenHelper = new DaoMaster.DevOpenHelper(this, DB_NAME);
Database database = devOpenHelper.getWritableDb();
DaoMaster daoMaster = new DaoMaster(database);
session = daoMaster.newSession();
}
public static DBApplication getAPPlication() {
return DBApplication;
}
public synchronized DaoSession getDaoSession() {
if (session == null) {
initDB();
}
return session;
}
}
- 第三步:新建實(shí)體類、也就是數(shù)據(jù)庫表中所需要字段。
Student 類。
- 開始新建時(shí)候代碼如下:
package com.mrko.mydb;
import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Id;
import org.greenrobot.greendao.annotation.Property;
/**
* Created by Mrko on 2017/9/19-10:00.
* Email:mrko0630@163.com
*/
@Entity
public class Student {
//注意 是Long 不是long
@Id(autoincrement = true)
private Long id;
@Property(nameInDb = "name")
private String name;
@Property(nameInDb = "age")
private int age;
}
- 然后在Make project (點(diǎn)擊小錘子)
- 新生成代碼如下:并會(huì)在build.gradle中配置的地址(targetGenDir 'src/main/java')的目錄下生成三個(gè)實(shí)體類(DaoMaster、DaoSession、StudentDao)(一般添加新的字段【诨溃可以把之前生成的三個(gè)實(shí)體類刪除魁巩,重新生成)
package com.mrko.mydb;
import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Id;
import org.greenrobot.greendao.annotation.Property;
import org.greenrobot.greendao.annotation.Generated;
/**
* Created by Mrko on 2017/9/19-10:00.
* Email:mrko0630@163.com
*/
@Entity
public class Student {
//注意 是Long 不是long
@Id(autoincrement = true)
private Long id;
@Property(nameInDb = "name")
private String name;
@Property(nameInDb = "age")
private int age;
@Generated(hash = 352757281)
public Student(Long id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
@Generated(hash = 1556870573)
public Student() {
}
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return this.age;
}
public void setAge(int age) {
this.age = age;
}
}
-
第四步:新建數(shù)據(jù)庫工具類
DBUtils 數(shù)據(jù)庫統(tǒng)一管理實(shí)現(xiàn)封裝
package com.mrko.helper;
import android.util.Log;
import com.mrko.DBApplication;
import com.mrko.dao.DaoSession;
import com.mrko.dao.StudentDao;
import com.mrko.mydb.Student;
import org.greenrobot.greendao.query.QueryBuilder;
import java.util.List;
import static android.content.ContentValues.TAG;
/**
* Created by Mrko on 2017/9/19-10:35.
* Email:mrko0630@163.com
*/
public class DBStudentUtils {
private static DaoSession mDaoSession;
private static StudentDao mStuentDao;
private static DBStudentUtils instance = null;
private DBStudentUtils() {
}
/**
* 雙重校驗(yàn)鎖單例模式
* 實(shí)例化Session和Dao類
* @return
*/
public static DBStudentUtils getInstance() {
if (instance == null) {
synchronized (DBStudentUtils.class) {
if (instance == null) {
instance = new DBStudentUtils();
}
mDaoSession = DBApplication.getAPPlication().getDaoSession();
mStuentDao = mDaoSession.getStudentDao();
}
}
return instance;
}
public Student makeStudent(String name, int age) {
Student student = null;
if (student == null) {
student = new Student();
Log.e(TAG, "makeStudent: " + student.getId());
student.setName(name);
student.setAge(age);
insertStudent(student);
}
return student;
}
public void insertStudent(Student student) {
mStuentDao.insert(student);
}
public void updateStudent(Student student) {
mStuentDao.update(student);
}
public void deleteStudent(Long id) {
mStuentDao.deleteByKey(id);
}
public List<Student> queryStudent(int age) {
QueryBuilder<Student> queryBuilder = mStuentDao.queryBuilder();
queryBuilder.where(StudentDao.Properties.Age.gt(age));
List<Student> queryStudents = queryBuilder.list();
return queryStudents;
}
}
- 第五步:如何調(diào)用:
package com.mrko.testone.dao;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import com.mrko.helper.DBStudentUtils;
import com.mrko.mydb.Student;
import com.mrko.testone.R;
import com.socks.library.KLog;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
public class DaoTestTwoActivity extends Activity {
@BindView(R.id.et_name)
EditText etName;
@BindView(R.id.et_age)
EditText etAge;
@BindView(R.id.btn_save)
Button btnSave;
@BindView(R.id.btn_update)
Button btnUpdate;
@BindView(R.id.btn_delete)
Button btnDelete;
@BindView(R.id.btn_query)
Button btnQuery;
@BindView(R.id.listview)
ListView listview;
private Unbinder unbinder;
private String mName;
private int mAge;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dao_test_two);
unbinder = ButterKnife.bind(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
unbinder.unbind();
}
@OnClick({R.id.btn_save, R.id.btn_update, R.id.btn_delete, R.id.btn_query})
public void onViewClicked(View view) {
mName = etName.getText().toString().trim();
String temp = etAge.getText().toString().trim();
mAge = Integer.parseInt(temp == null || temp.equals("") ? "0" : temp);
switch (view.getId()) {
case R.id.btn_save:
DBStudentUtils.getInstance().makeStudent(mName, mAge);
break;
case R.id.btn_update:
DBStudentUtils.getInstance().makeStudent(mName, mAge);
break;
case R.id.btn_delete:
List<Student> queryStudent = DBStudentUtils.getInstance().queryStudent(mAge);
List<Long> deleteStudents= new ArrayList<>();
if (queryStudent != null) {
for (Student student : queryStudent) {
KLog.e("mrko--->",student.getId() + "--" + student.getAge() + "--" + student.getName());
deleteStudents.add(student.getId());
}
if (deleteStudents!=null){
for(Long id : deleteStudents) {
DBStudentUtils.getInstance().deleteStudent(id);
KLog.e("mrko--->","delete id-->"+id);
}
}
}
break;
case R.id.btn_query:
List<Student> queryStudents = DBStudentUtils.getInstance().queryStudent(mAge);
if (queryStudents != null) {
for (Student student : queryStudents) {
KLog.e("mrko--->" + student.getId() + "--" + student.getAge() + "--" + student.getName());
}
}
break;
}
}
}
- 第六步:注意事項(xiàng):
DBApplication需要在app的AndroidManifest.xml中實(shí)現(xiàn)android:name.. 如果重寫application,繼承DBAapplication、
基本接入步驟如上踏枣、當(dāng)然greenDao很多探究躬厌、這里只是入門操作、笛谦、班門弄斧而已懦冰、