简述:
由于对sql语句老是记不住(”只知道简单的增删改查“),所以得找个办法弥补。想到安卓中有很多数据库框架,决定找个合适的,常见的有OrmLite,GreenDao,DBFlow都有自己的优点,看到GreenDao目前性能最好,内存消耗很小,高度优化,这不正是安卓最需要的吗?在网上搜了搜资料,看到已经有同学做了很多Demo,就按照步骤自己试了试,用起来的确很爽,所以把在项目中遇到的错误及使用方法分享个大家。
在项目中的配置
本人喜欢新的东西,所以在就直接用的最新版
在main下新建文件夹java-gen
在项目build.gradle中添加sourceSets { main { java.srcDirs = ['/src/main/java', '/src/main/java-gen'] } }
添加GreenDao如图下图所示compile 'org.greenrobot:greendao:2.2.0'
![]()
在项目中添加Module(Java项目)
![]()
Library name:Java项目名称
Java package name:java项目包名,可以自己定义
Java class name:Java类,我用来写main方法
![]()
在java项目的build.gradle中添加compile 'org.greenrobot:greendao-generator:2.2.0'
![]()
在GreenDaoMain中添加main方法,java程序员应该都会写吧public static void main(String[] args) throws Exception { }
在main方法中操作package com.example; import de.greenrobot.daogenerator.DaoGenerator; import de.greenrobot.daogenerator.Entity; import de.greenrobot.daogenerator.Schema; public class GreenDaoMain { public static void main(String[] args) throws Exception { Schema schema = new Schema(1, "com.zhoubigbo.greendaoexampleone");//版本,java-gen的包名 Entity entity = schema.addEntity("User");//实体类 entity.setTableName("USER");//表名 entity.addIdProperty().primaryKey().autoincrement();//ID,主键,自动增长 entity.addStringProperty("NAME").notNull();//姓名,不可为空 entity.addIntProperty("AGE");//年龄 entity.addLongProperty("PHONE_NUMBER");//电话号码 entity.addDoubleProperty("HEIGHT");//身高 entity.addBooleanProperty("MARRY");//婚否 entity.addByteArrayProperty("PWD");//密码 entity.addDateProperty("BIRTHDAY");//生日 new DaoGenerator().generateAll(schema, "../GreenDaoExampleOne/app/src/main/java-gen"); } }
![]()
右键运行java项目
得到如下结果说明你就成功了![]()
![]()
在项目写一个GreenDaoApp类,让它继承Application,并在清单文件中注册在App类中添加如下代码package com.zhoubigbo.greendaoexampleone; import android.app.Application; import android.content.Context; public class GreenDaoApp extends Application { private static DaoMaster daoMaster = null; private static DaoSession daoSession = null; //DaoMaster public static DaoMaster getDaoMaster(Context context) { if (daoMaster == null) { DaoMaster.OpenHelper helper = new DaoMaster.DevOpenHelper(context, "GreenDao_DB", null); daoMaster = new DaoMaster(helper.getWritableDatabase()); } return daoMaster; } //DaoSession public static DaoSession getDaoSession(Context context) { if (daoSession == null) { if (daoMaster == null) { daoMaster = getDaoMaster(context); } daoSession = daoMaster.newSession(); } return daoSession; } }
在MainActivity中有4个按钮,分别是增删改查,代码如下
package com.zhoubigbo.greendaoexampleone; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import java.util.Date; import de.greenrobot.dao.query.QueryBuilder; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private Button btnInsert, btnSearch, btnDelete, btnUpdate; private TextView txtMsg; private UserDao userDao; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); txtMsg = (TextView) findViewById(R.id.txt_msg); btnInsert = (Button) findViewById(R.id.btn_insert); btnSearch = (Button) findViewById(R.id.btn_search); btnDelete = (Button) findViewById(R.id.btn_delete); btnUpdate = (Button) findViewById(R.id.btn_update); btnInsert.setOnClickListener(this); btnSearch.setOnClickListener(this); btnDelete.setOnClickListener(this); btnUpdate.setOnClickListener(this); userDao = GreenDaoApp.getDaoSession(getApplicationContext()).getUserDao(); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_insert: User user = new User(); user.setNAME("张三"); user.setAGE(15); user.setPHONE_NUMBER(130123454678l); user.setHEIGHT(166.6); user.setMARRY(false); user.setPWD(new byte[]{12, 34, 56}); user.setBIRTHDAY(new Date()); userDao.insert(user); break; case R.id.btn_search: QueryBuilder queryBuilder = userDao.queryBuilder(); queryBuilder.where(UserDao.Properties.Id.eq("1")); User qUser = (User) queryBuilder.unique(); if (qUser != null) { txtMsg.setText(qUser.getNAME() + qUser.getAGE()); } else { txtMsg.setText("没找到数据"); } break; case R.id.btn_delete: userDao.deleteByKey(1l); userDao.deleteInTx(); break; case R.id.btn_update: User upUser = new User(); upUser.setId(1l); upUser.setNAME("李四"); upUser.setAGE(15); upUser.setPHONE_NUMBER(130123454678l); upUser.setHEIGHT(166.6); upUser.setMARRY(false); upUser.setPWD(new byte[]{12, 34, 56}); upUser.setBIRTHDAY(new Date()); userDao.update(upUser); break; } } }
配置及 简单的操作就到这里,下一次讲解GreenDao高级用法。欢迎大家指正