greenDao的使用步骤:
第一步: 在项目的.gradle文件里面添加
buildscript { repositories { mavenCentral() jcenter() } dependencies { classpath 'com.android.tools.build:gradle:2.3.0' classpath 'org.greenrobot:greendao-gradle-plugin:3.0.0' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } }主要如下图:
第二步:在moddle的.gradle文件里面添加
apply plugin: 'org.greenrobot.greendao'
greendao { schemaVersion 1 daoPackage 'com.example.john.greendaodemo.gen' targetGenDir 'src/main/java' }
依赖里面添加
compile 'org.greenrobot:greendao:3.0.1' compile 'org.greenrobot:greendao-generator:3.0.0'
如图:
第三步: 添加Bean类,用User举例子 , 一定要记得注解@Entity
@Entity public class User { @Id(autoincrement = true) private Long id; private String name; private int age; private String sex; }
第四步: 编译项目,User实体类会自动编译,生成get、set方法并且会在com.example.john.greendaodemo.gen目录下生成三个文件;
如图:
生成一下文件:
第五步:GreenDao的使用
package com.example.john.greendaodemo; import android.app.Application; import android.database.sqlite.SQLiteDatabase; import com.example.john.greendaodemo.gen.DaoMaster; import com.example.john.greendaodemo.gen.DaoSession; public class AppApplication extends Application { private DaoMaster.DevOpenHelper mHelper; private SQLiteDatabase db; private DaoMaster mDaoMaster; private DaoSession mDaoSession; public static AppApplication instances; @Override public void onCreate() { super.onCreate(); instances = this; setDatabase(); } /** * 单例模式 * * @return */ public static AppApplication getInstances() { return instances; } /** * 设置greenDao */ private void setDatabase() { // 通过 DaoMaster 的内部类 DevOpenHelper,你可以得到一个便利的 SQLiteOpenHelper 对象。 // 可能你已经注意到了,你并不需要去编写「CREATE TABLE」这样的 SQL 语句,因为 greenDAO 已经帮你做了。 // 注意:默认的 DaoMaster.DevOpenHelper 会在数据库升级时,删除所有的表,意味着这将导致数据的丢失。 // 所以,在正式的项目中,你还应该做一层封装,来实现数据库的安全升级。 mHelper = new DaoMaster.DevOpenHelper(this, "notes-db", null); db = mHelper.getWritableDatabase(); // 注意:该数据库连接属于 DaoMaster,所以多个 Session 指的是相同的数据库连接。 mDaoMaster = new DaoMaster(db); mDaoSession = mDaoMaster.newSession(); } public DaoSession getDaoSession() { return mDaoSession; } public SQLiteDatabase getDb() { return db; } }
具体的使用:
package com.example.john.greendaodemo; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import com.example.john.greendaodemo.User.User; import com.example.john.greendaodemo.gen.UserDao; import org.greenrobot.greendao.query.QueryBuilder; import java.util.List; public class MainActivity extends AppCompatActivity implements View.OnClickListener { int i = 1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } private void add() { User user = new User(null, "zhangsan", i, "男"); i = i + 1; AppApplication.getInstances().getDaoSession().insert(user); Log.i("-------", "添加成功"); } private void delete() { UserDao userDao = AppApplication.getInstances().getDaoSession().getUserDao(); List布局就不多说了,直接给图,设置点击事件就可以了list = userDao.queryBuilder().list(); if (list == null || list.isEmpty()) { return; } for (int j = 0; j < list.size(); j++) { User user = list.get(j); if ("zhangsan".equals(user.getName())) { userDao.delete(user); Log.i("-------", "删除方法正在删除名字是张三的人"); } } Log.i("-------", "删除成功"); } private void update() { UserDao userDao = AppApplication.getInstances().getDaoSession().getUserDao(); QueryBuilder builder = userDao.queryBuilder(); List zhangsan = builder.where(UserDao.Properties.Age.eq(1)).list(); if (zhangsan == null || zhangsan.isEmpty()) { return; } for (int i = 0; i < zhangsan.size(); i++) { User user = zhangsan.get(i); user.setName("lisi"); user.setSex("女"); AppApplication.getInstances().getDaoSession().update(user); Log.i("-------", "修改成功"); } List list = AppApplication.getInstances().getDaoSession().getUserDao().queryBuilder().list(); if (list == null || list.isEmpty()) { Log.i("-------", "修改方法的暂无数据"); return; } for (int j = 0; j < list.size(); j++) { User user = list.get(j); Log.i("-------修改方法的遍历", "name:[" + user.getName() + "],[age:" + user.getAge() + "],[sex:" + user.getSex() + "],[id" + user.getId() + "]"); } } private void cha() { UserDao userDao = AppApplication.getInstances().getDaoSession().getUserDao(); QueryBuilder builder = userDao.queryBuilder(); List list = builder.list(); if (list == null || list.isEmpty()) { Log.i("-------", "所有数据为空"); return; } for (int i = 0; i < list.size(); i++) { User user = list.get(i); String name = user.getName(); int age = user.getAge(); Log.i("-------", "allName : [name:" + name + "],[age:" + age + "],[sex:" + user.getSex() + "],[id:" + user.getId() + "]"); } } @Override public void onClick(View v) { switch (v.getId()) { case R.id.add: add(); break; case R.id.delete: delete(); break; case R.id.update: update(); break; case R.id.cha: cha(); break; } } }
这就ok了,其实使用起来并不难,只要你肯动手去写Demo,我也只是了解到了皮毛,大家一起努力。