簡單的寫一個GreenDao
阿新 • • 發佈:2018-11-06
許可權以及依賴
將下面註冊到自己建立的應用的build.gradle裡
apply plugin: 'org.greenrobot.greendao'
implementation 'org.greenrobot:greendao:3.2.2'
將下面的新增到總工程裡的build.gradle
jcenter()
mavenCentral()
classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2'
person
@Entity(nameInDb = "person_table") public class Person { @Id(autoincrement = true) public Long id; @Property(nameInDb = "name") public String mingzi; public int age; @Transient public String identity; @Generated(hash = 244725397) public Person(Long id, String mingzi, int age) { this.id = id; this.mingzi = mingzi; this.age = age; } @Generated(hash = 1024547259) public Person() { } @Override public String toString() { return "Person{" + "id=" + id + ", mingzi='" + mingzi + '\'' + ", age=" + age + ", identity='" + identity + '\'' + '}'; } public Long getId() { return this.id; } public void setId(Long id) { this.id = id; } public String getMingzi() { return this.mingzi; } public void setMingzi(String mingzi) { this.mingzi = mingzi; } public int getAge() { return this.age; } public void setAge(int age) { this.age = age; } }
MainActivity
public class MainActivity extends AppCompatActivity { private static final String TAG ="MainActivity" ; private DaoSession daoSession; private int index; private PersonDao personDao; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); daoSession = DaoManager.getInstance(this).getDaoSession(); personDao =daoSession.getPersonDao(); } public void query(View view){ List<Person> list = personDao.queryBuilder() .where(PersonDao.Properties.Age.lt(500)) .orderDesc(PersonDao.Properties.Age) .build() .list(); for (Person person: list ) { Log.e(TAG, "query: "+person.toString() ); } } public void add(View view){ index++; Person person = new Person(null,"name" + index,index * 10); personDao.insert(person); } public void update(View view){ List<Person> list = personDao.queryBuilder() .where(PersonDao.Properties.Age.lt(500)) .orderDesc(PersonDao.Properties.Age) .build() .list(); for (Person person: list) { if (person.age ==20){ person.age = 100; personDao.update(person); } } } public void delete(View view){ Person person = personDao.queryBuilder() .where(PersonDao.Properties.Id.eq(2)) .build() .unique(); if (person != null){ personDao.delete(person); } } }
DaoManager
public class DaoManager {
private static DaoManager daoManager;
private DaoSession daoSession;
public DaoManager(Context context) {
daoSession = DaoMaster.newDevSession(context,"person.db");
}
public static DaoManager getInstance(Context context){
if (daoManager == null){
synchronized (DaoManager.class){
if (null ==daoManager){
daoManager = new DaoManager(context);
}
}
}
return daoManager;
}
public DaoSession getDaoSession(){
return daoSession;
}
}
xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.bwie.wang.myagreendao.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="query"
android:text="query" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="add"
android:text="add" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="update"
android:text="update" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="delete"
android:text="delete" />
</LinearLayout>