GreenDAO配置保姆教程
首先,创建一个新工程,切换到Project视图
选中build.gradle,在依赖中添加以下语句并应用
implementation 'org.greenrobot:greendao:3.0.1'
选中工程名右键->New->Module,选择Java Library,点击Next,再点击Finish,就会看到多了一个lib文件夹,其内部有一个MyClass.java文件
创建成功之后要给这个java项目配置依赖
接下来编写MyClass.java的代码
import org.greenrobot.greendao.generator.DaoGenerator;
import org.greenrobot.greendao.generator.Entity;
import org.greenrobot.greendao.generator.Property;
import org.greenrobot.greendao.generator.Schema;
import java.io.IOException;
public class MyClass {
public static void main(String[] args){
Schema schema=new Schema(1,"com.example.greeandaodemo2");
Entity son=schema.addEntity("Son");//创建表Son
son.addStringProperty("name");//给表Son增加属性
son.addIntProperty("age");
son.addIdProperty();
Property fatherId=son.addLongProperty("fatherId").getProperty();
Entity father=schema.addEntity("Father");//表名首字母要大写,否则报错!!!
father.addIdProperty();
father.addStringProperty("name");
father.addIntProperty("age");
son.addToOne(father,fatherId);//建立一对一关系
try{
new DaoGenerator().generateAll(schema, "app/src/main/java");
}catch (Exception e){}
}
}
右键选中Run ‘MyClass.main()’,得到运行结果,同时在app目录下可以看到生成的类
在MainActivity编写如下代码
import androidx.appcompat.app.AppCompatActivity;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import com.example.greeandaodemo.DaoMaster;
import com.example.greeandaodemo.DaoSession;
import com.example.greeandaodemo.Father;
import com.example.greeandaodemo.FatherDao;
import com.example.greeandaodemo.Son;
import com.example.greeandaodemo.SonDao;
import java.util.Iterator;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private DaoMaster master;
private DaoSession session;
private SQLiteDatabase db;
private SonDao sonDao;
private FatherDao fatherDao;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
openDb();
addPerson();
queryAll();
}
public void queryAll() {
/*List<Son> list=sonDao.queryBuilder().list();
for(Son son:list){
Log.d("son", "queryAll() called with:"+son);
}*/
Iterator iterator=sonDao.queryBuilder().listIterator();
while(iterator.hasNext()){
Son son=(Son)iterator.next();
Log.d("son", "queryAll() called with:"+son);
}
}
private void openDb(){
db=new DaoMaster.DevOpenHelper(MainActivity.this, "person.db",null).getWritableDatabase();
master=new DaoMaster(db);
session=master.newSession();
sonDao=session.getSonDao();
fatherDao=session.getFatherDao();
}
private void addPerson(){
Son son = new Son();
son.setName("hbn");
son.setAge(20);
Father father=new Father();
father.setAge(45);
father.setName("zy");
long fatherId=fatherDao.insert(father);
son.setFatherId(fatherId);
sonDao.insert(son);
Son son2 = new Son();
son2.setName("zhouying");
son2.setAge(23);
Father father2=new Father();
father2.setAge(50);
father2.setName("ly");
fatherId=fatherDao.insert(father2);
son2.setFatherId(fatherId);
sonDao.insert(son2);
}
}
运行!
然后查看一下数据库的数据
打开Device File Explorer,打开data/data/com.example.greendaodemo2/databases,选中person.db,右键选择Save as,为了方便,暂时把它保存到桌面。打开Sqlite查看工具Sqlite Expert(关于这个工具,可以到这个网址http://www.sqliteexpert.com/download.html下载,我这边下的是老版本的个人版,新版本安装被Windows拦截了),打开person.db,可以分别查看到表Son和表Father的记录
收工
菜鸡遁走…