implementation 'org.greenrobot:greendao:3.2.2' // add library
apply plugin: 'com.android.application'
apply plugin: 'org.greenrobot.greendao' // apply plugin
mavenCentral() // add repository
classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2' // add plugin
App
package com.baway.alex.greendaodemo.data;
import android.app.Application;
import android.database.sqlite.SQLiteDatabase;
import com.baway.alex.greendaodemo.data.beans.DaoMaster;
import com.baway.alex.greendaodemo.data.beans.DaoSession;
public class App extends Application {
private DaoSession daoSession;
private static App app;
@Override
public void onCreate() {
super.onCreate();
app = App.this;
//获取数据库
DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "GreenDao");
SQLiteDatabase db = helper.getWritableDatabase();
//创建对象、调用方法、参数传递
DaoMaster daoMaster = new DaoMaster(db);
//管理了所有的Dao对象
daoSession = daoMaster.newSession();
}
public static App getInstance() {
return app;
}
public DaoSession getDaoSession() {
return daoSession;
}
}
Mainactivity
package com.baway.alex.greendaodemo.ui.activity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.baway.alex.greendaodemo.R;
import com.baway.alex.greendaodemo.data.App;
import com.baway.alex.greendaodemo.data.beans.User;
import com.baway.alex.greendaodemo.data.beans.UserDao;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
public class MainActivity extends AppCompatActivity {
@BindView(R.id.btn_add)
Button btnAdd;
@BindView(R.id.btn_delete)
Button btnDelete;
@BindView(R.id.btn_update)
Button btnUpdate;
@BindView(R.id.btn_query)
Button btnQuery;
@BindView(R.id.tv_showData)
TextView tvShowData;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
}
@OnClick({R.id.btn_add, R.id.btn_delete, R.id.btn_update, R.id.btn_query})
public void onViewClicked(View view) {
UserDao userDao = App.getInstance().getDaoSession().getUserDao();
switch (view.getId()) {
case R.id.btn_add:
for (int i = 0; i < 5; i++) {
User user = new User();
user.setName("司徒" + i);
user.setAge(18 + i);
userDao.insertOrReplace(user);
}
break;
case R.id.btn_delete:
//deleteById(userDao);
//根据主键进行删除
//userDao.deleteByKey((long) 2);
//全部删除
userDao.deleteAll();
break;
case R.id.btn_update:
User user = new User();
user.setAge(28);
user.setId((long) 1);
userDao.update(user);
break;
case R.id.btn_query:
//根据Id进行单体查询
//queryByKey(userDao);
//查询全部数据
List<User> userList = userDao.loadAll();
//定义字符串缓冲区
StringBuffer sb = new StringBuffer();
for (int i = 0; i < userList.size(); i++) {
sb.append("主键ID是:" + userList.get(i).getId() + "姓名是:" + userList.get(i).getName() + "年龄是:" + userList.get(i).getAge() + "\t\n");
}
tvShowData.setText(sb.toString());
break;
}
}
private void deleteById(UserDao userDao) {
//删除年龄为19的User
//模糊删除需指定ID
User user = new User();
user.setAge(19);
user.setId((long) 1);
userDao.delete(user);
}
private void queryByKey(UserDao userDao) {
//判空处理
User user = userDao.load((long) 1);
if (user == null) {
return;
}
Toast.makeText(this, "姓名是:" + user.getName() + "...年龄是;" + user.getAge() + " ", Toast.LENGTH_SHORT).show();
}
}
User
package com.baway.alex.greendaodemo.data.beans;
import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Id;
import org.greenrobot.greendao.annotation.Property;
import org.greenrobot.greendao.annotation.Generated;
@Entity
public class User {
@Id(autoincrement = true)
Long id;
@Property
String name;
@Property
int age;
@Generated(hash = 1309193360)
public User(Long id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
@Generated(hash = 586692638)
public User() {
}
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;
}
}