1、使用前的准备
新建Android项目 build.gradle中添加
compile'org.greenrobot:greendao:3.2.2'
新建java项目 build.gradle中添加
compile'org.greenrobot:greendao-generator:3.2.2'
2、生成dbUtil文件
public classMyClass {
/**
*使用步骤
* 1、创建Schema
* 2、通过schema获取实体
* 3、通过实体设置表属性
*@paramargs
*/
public static voidmain(String[] args){
Schema schema =newSchema(1,"example.kuan.com.greendaoutil");//版本 包名
Entity son = schema.addEntity("Son");//表的名称(注意要大写)
son.addIdProperty();
son.addStringProperty("name");//表的列名
son.addIntProperty("age");
Property fatherId = son.addLongProperty("fatherId").getProperty();//外键关联
Entity father = schema.addEntity("Father");
father.addIdProperty();
father.addStringProperty("name");
father.addIntProperty("age");
son.addToOne(father,fatherId);//通过设置外键关联
try{
//之前的配置 生成文件的路径
newDaoGenerator().generateAll(schema,"E:\\MyProgect\\GreenDaoDemo\\app\\src\\main\\java");
}catch(Exception e) {
e.printStackTrace();
}}}
点击运行之后会在E:\\MyProgect\\GreenDaoDemo\\app\\src\\main\\java中的example.kuan.com.greendaoutil包中生成数据库映射文件
3.开始使用
public classMainActivityextendsAppCompatActivity {
@BindView(R.id.btn_insert)
ButtonbtnInsert;
@BindView(R.id.btn_queryall)
ButtonbtnQueryall;
@BindView(R.id.btn_querylike)
ButtonbtnQuerylike;
@BindView(R.id.edit_query)
EditTexteditQuery;
privateDaoMastermaster;
privateDaoSessionsession;
privateSQLiteDatabasedb;
privateSonDaosonDao;
privateFatherDaofatherDao;
private voidopenDb() {
db=newDaoMaster.DevOpenHelper(this,"person.db",null).getWritableDatabase();
master=newDaoMaster(db);
session=master.newSession();
sonDao=session.getSonDao();
fatherDao=session.getFatherDao();
}
//查询全部
private voidqueryAll() {
//查询全部数据并且加载到内存中
// Listlist = sonDao.queryBuilder().list();
// LazyListlist = sonDao.queryBuilder().listLazy();
// for (Son son:list) {
// Log.e("TAG","queryAll:"+son.toString());
// }
//懒加载 只有用到数据时候才会转化为该对象 提高性能 必须手动关闭游标
LazyListlist =sonDao.queryBuilder().listLazy();
for(Son son : list) {
Log.e("TAG","queryAll:"+ son.toString());
}
list.close();//关闭游标
// Iterator itetator = sonDao.queryBuilder().listIterator();
// while(itetator.hasNext()){
// Son son = (Son) itetator.next();
// Log.e("TAG","queryAll:"+son.toString());
// }
}
//SQL语句查询
private voidqueryBySQL(){
Listdata =sonDao.queryBuilder().where(
newWhereCondition.StringCondition("FATHER_ID IN (Select * From FATHER WHERE AGE < 45)")).list();
for(Son son : data) {
Log.e("TAG","queryBySQL:"+ son.toString());
}
}
/**
*
*等于eq
*模糊查询like
*介于中间between
* > gt
* < lt
* noteq
*为空isNull
*不为空isNotNull
*排序orderAsc(升序) orderDesc(降序)
* >= ge
* <= le
*在什么范围内in
*不在什么范围内notIn
*/
//条件查询(where eq)
private voidqueryBySomeThing() {
Son son =sonDao.queryBuilder().where(SonDao.Properties.Name.
eq("nate")).orderAsc(FatherDao.Properties.Age).unique();
Log.e("TAG","queryBySomeThing:"+ son.toString());
}
//通过模糊查询数据(where like)
private voidqueryByLikeSomeThing() {
Listlist =sonDao.queryBuilder().where(SonDao.Properties.Name.
like("%"+editQuery.getText().toString().trim()+"%")).list();
for(Son son : list) {
Log.e("TAG","queryByLikeSomeThing:"+ son.toString());
}
}
//介于中间的年龄(where between)
private voidqueryBetween(){
Listlist =sonDao.queryBuilder().where(SonDao.Properties.Name.
between(10,20)).list();
for(Son son : list) {
Log.e("TAG","queryByLikeSomeThing:"+ son.toString());
}
}
//插入数据
private voidaddPerson() {
Son son =newSon();
son.setName("nate");
son.setAge(18);
Father father =newFather();
father.setName("Tom");
father.setAge(50);
longid =fatherDao.insert(father);
son.setFatherId(id);
sonDao.insert(son);
Son son1 =newSon();
son.setName("na456");
son.setAge(18);
Father father1 =newFather();
father1.setName("Tom2");
father1.setAge(50);
longid1 =fatherDao.insert(father1);
son.setFatherId(id1);
sonDao.insert(son1);
Son Tom =newSon();
son.setName("Tom");
son.setAge(28);
Father Com =newFather();
father.setName("Com");
father.setAge(50);
longfatherId =fatherDao.insert(Com);
Tom.setFatherId(fatherId);
sonDao.insert(Tom);
}
//多线程操作数据库
private voidqueryInThread(){
finalQuery query =sonDao.queryBuilder().build();
newThread(newRunnable() {
@Override
public voidrun() {
query.forCurrentThread();
Listlist =query.list();
for(Son son : list) {
Log.e("TAG","queryInThread:"+ son.toString());
}
}
}).start();
}
private voidqueryOneToOne(){
Listlist =sonDao.queryBuilder().list();
for(Son son : list) {
Log.e("TAG","queryInThread:"+ son.getFather());
}
}
@Override
protected voidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
//打印日志
QueryBuilder.LOG_SQL=true;
QueryBuilder.LOG_VALUES=true;
openDb();
// queryBySomeThing();
}
@OnClick({R.id.btn_insert, R.id.btn_queryall, R.id.btn_querylike})
public voidonViewClicked(View view) {
switch(view.getId()) {
caseR.id.btn_insert:
addPerson();
break;
caseR.id.btn_queryall:
queryAll();
break;
caseR.id.btn_querylike:
queryByLikeSomeThing();
break;
}}}
最后GitHub链接 https://github.com/soulListener/GreenDaoDemo