在项目APP里加入一行代码
classpath 'org.greenrobot:greendao-gradle-plugin:3.2.0'
在app最上面加入代码
apply plugin: 'org.greenrobot.greendao'
在app的Android{}里加入代码
greendao{
schemaVersion 1 //指定数据库schema版本号,迁移等操作会用到
//包名是活的这是变动的
daoPackage 'soexample.umeng.com.greendaodemo.mydao' //dao的包名,包名默认是entity所在的包;
targetGenDir 'src/main/java'//生成数据库文件的目录
}
加入依赖
implementation 'org.greenrobot:greendao:3.2.2'
implementation 'com.jakewharton:butterknife:8.5.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1'
自动生成的三个类
student实体类
@Entity
public class Student {
//主键自增
@Id(autoincrement = true)
private Long id;
private String name;
private String sex;
private int age;
@Generated(hash = 434239779)
public Student(Long id, String name, String sex, int age) {
this.id = id;
this.name = name;
this.sex = sex;
this.age = age;
}
@Generated(hash = 1556870573)
public Student() {
}
public Student(String name, String sex, int age) {
this.name = name;
this.sex = sex;
this.age = age;
}
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 String getSex() {
return this.sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return this.age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", sex='" + sex + '\'' +
", age=" + age +
'}';
}
}
APP
package com.bw.lly.greendaodemo;
import android.app.Application;
import android.database.sqlite.SQLiteDatabase;
import com.bw.lly.greendaodemo.mydao.DaoMaster;
import com.bw.lly.greendaodemo.mydao.DaoSession;
/*
* 用来配置数据库
* */
public class App extends Application{
private static DaoSession daoSession;
@Override
public void onCreate() {
super.onCreate();
initGreenDao();
}
private void initGreenDao() {
//第一步创建openHelper类
DaoMaster.DevOpenHelper openHelper = new DaoMaster.DevOpenHelper(this, "lly.db");
//开启一个可写数据库类
SQLiteDatabase writableDatabase = openHelper.getWritableDatabase();
//通过daoMaster封装
DaoMaster daoMaster = new DaoMaster(writableDatabase);
daoSession = daoMaster.newSession();
}
public static DaoSession getDaoSession(){
return daoSession;
}
}
mainactivity
public class MainActivity extends AppCompatActivity {
@BindView(R.id.inster)
Button inster;
@BindView(R.id.delete)
Button delete;
@BindView(R.id.updata)
Button updata;
@BindView(R.id.select)
Button select;
@BindView(R.id.text)
TextView text;
private StudentDao studentDao;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
studentDao = App.getDaoSession().getStudentDao();
}
@OnClick({R.id.inster, R.id.delete, R.id.updata, R.id.select})
public void onViewClicked(View view) {
switch (view.getId()) {
case R.id.inster:
insertStu();
selectStu();
break;
case R.id.delete:
deleteStu();
selectStu();
break;
case R.id.updata:
updateStu();
selectStu();
break;
case R.id.select:
selectStu();
break;
}
}
//添加数据
private void insertStu() {
Student student = new Student("小红", "女", 19);
long insert = studentDao.insert(student);
if (insert > 0) {
Toast.makeText(this, "添加成功", Toast.LENGTH_SHORT).show();
}
}
//删除数据 删除必须要删除数据库里面存在的数据
private void deleteStu() {
studentDao.deleteByKey(3l);
Toast.makeText(this, "删除成功", Toast.LENGTH_SHORT).show();
}
//修改数据
private void updateStu() {
//修改必须要修改数据库里面存在的数据
Student student = studentDao.load(3l);
student.setAge(18);
student.setName("张三");
student.setSex("男");
studentDao.update(student);
}
//查询数据
private void selectStu() {
//查询所有数据
text.setText("");
List<Student> students = studentDao.loadAll();
text.setText(students.toString());
}
}