一、greenDao简介
greenDAO是一个开源的Android ORM(对象-关系映射),它让SQLite数据库的开发再次变得有趣。它使开发人员免于处理低级数据库要求,同时节省了开发时间。SQLite是一个很棒的嵌入式关系数据库。尽管如此,编写SQL和解析查询结果仍然是非常繁琐且耗时的任务。通过将Java对象映射到数据库表(称为ORM,“对象/关系映射”),greenDAO使您摆脱了这些麻烦。这样,您可以使用简单的面向对象的API来存储,更新,删除和查询Java对象。
二、greenDAO优点
最佳性能 (可能是 Android 中最快的 ORM) ,基准测试也是开源的;
易于使用的功能强大的 api,涵盖关系和连接;
最小的内存消耗;
小型库大小(< 100KB) ,以保持较低的构建时间,并避免65k 方法限制;
数据库加密:greenDAO 支持 SQLCipher 来保证用户数据的安全;
强大而活跃的社区交流支持,相关资料比较完善;
许多顶级的Android应用程序都依赖greenDAO,其中一些应用程序的安装量超过1000万,更证明了其可靠性
三、greenDao 的简单使用
1.project的build.gradle 下配置
dependencies { classpath 'org.greenrobot:greendao-gradle-plugin:3.3.0' }
2.app的build.gradle 下配置
plugins { id 'com.android.application' id 'org.greenrobot.greendao' //新加入 } android { compileSdk 31 defaultConfig { applicationId "com.example.greendao" minSdk 29 targetSdk 31 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } //新加入 greendao { schemaVersion 1 //数据库版本号 每次升级数据库都需要改变版本,只能增加 daoPackage 'com.example.greendao' //设置DaoMaster、DaoSession、Dao包名 targetGenDir 'src/main/java' //设置DaoMaster、DaoSession、Dao目录 } } dependencies { //新加入 implementation 'org.greenrobot:greendao:3.2.2' implementation 'androidx.appcompat:appcompat:1.4.2' implementation 'com.google.android.material:material:1.6.1' implementation 'androidx.constraintlayout:constraintlayout:2.1.4' testImplementation 'junit:junit:4.+' androidTestImplementation 'androidx.test.ext:junit:1.1.3' androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' }
3、创建实体类
@Entity public class Student { @Id private Long Id; private String name; private String major; private int age; }
点击 make project按键生成代码
4.初始化数据库
public class MyApplication extends Application { //实例化DaoMaster对象 private DaoMaster mDaoMaster; //实例化DaoSession对象 private static DaoSession sDaoSession; private DaoMaster.DevOpenHelper mDevOpenHelper = null; @Override public void onCreate() { super.onCreate(); // 1、获取需要连接的数据库 mDevOpenHelper = new DaoMaster.DevOpenHelper(getApplicationContext(),"student.db"); // 2、创建数据库连接 mDaoMaster = new DaoMaster(mDevOpenHelper.getWritableDb()); // 3、创建数据库会话 sDaoSession = mDaoMaster.newSession(); } //通过此方法,操作数据库,进行增删改查 public static DaoSession getDaoSession() { return sDaoSession; } }
5.实现增删改查
mMainBinding = DataBindingUtil.setContentView(this,R.layout.activity_main); StudentDao userDao = MyApplication.getDaoSession().getStudentDao(); // insert mMainBinding.insert.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { userDao.insert(new Student(null,mMainBinding.name.getText().toString(), mMainBinding.major.getText().toString() ,Integer.parseInt(mMainBinding.age.getText().toString()))); } }); mMainBinding.delete.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { userDao.deleteAll(); } }); mMainBinding.query.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { List<Student> students = userDao.loadAll(); StringBuilder sb = new StringBuilder(); for (Student s : students) { sb.append(s.toString()).append("\n"); } mMainBinding.content.setText(sb.toString()); } }); mMainBinding.update.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { List<Student> list = userDao.queryBuilder().where( StudentDao.Properties.Id.eq(1L)).list(); if (list.size()>0){ list.get(0).setAge(35); userDao.update(list.get(0)); } } });
6.贴下 layout 布局
<?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools"> <data> </data> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/name" android:hint="请输入姓名" app:layout_constraintTop_toTopOf="parent" /> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/major" android:hint="请输入专业" app:layout_constraintTop_toBottomOf="@id/name" /> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入年龄" app:layout_constraintTop_toBottomOf="@id/major" android:id="@+id/age" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/insert" android:text="插入数据" app:layout_constraintTop_toBottomOf="@id/age" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" tools:ignore="MissingConstraints" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/delete" android:text="删除数据" tools:ignore="MissingConstraints" android:layout_marginTop="30dp" app:layout_constraintTop_toBottomOf="@id/insert" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/update" android:text="修改数据" tools:ignore="MissingConstraints" app:layout_constraintTop_toBottomOf="@+id/delete" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/query" android:text="查询数据" app:layout_constraintTop_toBottomOf="@+id/update" tools:ignore="MissingConstraints" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/content" app:layout_constraintTop_toBottomOf="@id/query" /> </androidx.constraintlayout.widget.ConstraintLayout> </layout>