GreenDao 使用教程 一
来源:互联网 发布:江西师大网络教学平台 编辑:程序博客网 时间:2024/04/29 17:30
GreenDao orm数据库框架
优势:
1:性能最大化
2:内存开销最小
3:API 简单好用
4:对android 高度优化
5:2.2版本以上还支持加密数据库
6:支持protobuf协议存储(protobuf 比json更快,google的优秀产品,因此greendao能很好地与retrofit 2.0结合protobuf网络请求并缓存)
greendao的性能远胜于ORMLite ActiveAndroid Xutils等(我亲自测试了的)
greendao是两级数据库,分为持久的内存sqlite数据库与内存数据库,如果频繁操作数据库的话,建议是用内存数据库,然后持久到sqlite中,这样的性能远胜于原生的sqlite,即使不使用内存数据库,也几乎与原生sqlite的效率几乎接近。
第一步:添加依赖并创建独立session空间
compile 'org.greenrobot:greendao-encryption:2.2.0'compile 'net.zetetic:android-database-sqlcipher:3.4.0'
第二步骤:创建代码自动生成器 的java工程:
并添加生成依赖:
compile 'org.greenrobot:greendao-generator-encryption:2.2.0'
dao的生成器
能自动生成模板
orm数据库的模型和表是一一对应的,也就是说通常情况下模型的字段就是表的列:
创建表需要依赖实体,但是greendao框架的生成器已经能自动创建模型
创建模型或者说创建表 需要用到Schema类:也就是实体添加的容器
Schema schema = new Schema(1, "de.greenrobot.daoexample"); 可以创建一张表对应的版本与实体存放的包
接着需要创建实体:需要用到生成器中的一个Entity类,通过schema.addEntity(name) 可以实例化一个指定类名的实体类:
如:Entity entity=schema.addEntity("Note");
添加完实体,总得需要字段或者说表的列吧
添加列也就是实体的字段名称:
entity.addIdProperty();//主键
entity.addStringProperty("title");//列名
DaoGenerator可以帮我们自动生成entity-表的java代码;
完整代码:
执行java main方法,可以在控制台看到如下,表示创建成功,否则您需要检查你的路径是否存在package com.example;import de.greenrobot.daogenerator.DaoGenerator;import de.greenrobot.daogenerator.Entity;import de.greenrobot.daogenerator.Schema;public class NoteDaoGenerator { public static final int version=1;//数据库版本号 public static final String entityPackageName="com.xuan.db.entity";//实体生存的包名 public static final String entityClassName="Note";//实体的类名 public static final String daoPackageName="com.xuan.db.dao";//指定dao层模板的包 //自动生成模板类存放的绝对地址,也就是你的module创建的session文件夹 也就是java-gen public static final String autoGenerateJavaPath="E:\\Programing\\androidstudio\\workspace\\GitOsc_StudyProject\\db_greendao\\src\\main\\java-gen"; public static void main(String[] args) throws Exception { Schema schema = new Schema(version, entityPackageName); schema.setDefaultJavaPackageDao(daoPackageName);//如果不指定 默认与entityPackageName一致 Entity entity=schema.addEntity(entityClassName); entity.addIdProperty();//主键 entity.addStringProperty("title");//表的地2列 列名 entity.addStringProperty("content");//表的地3列 列名 entity.addStringProperty("createTime");//表的地4列 列名 entity.setClassNameDao("NoteDao");//设置dao类的名称 entity.setJavaDoc("auto greenDao generate javaBean by xuan"); entity.setTableName("tb_note");//设置表名,默认是entityClassName(NOTE)的大写形式 //自动生成java模板类 new DaoGenerator().generateAll(schema,autoGenerateJavaPath); }}
可以看到在我们指定的文件夹下面已经创建好了这些模型与dao的java类
框架的sqlite语句控制台打印 控制
package com.xuan.greendaoencryp;import android.app.Activity;import android.app.Application;import android.os.Bundle;import de.greenrobot.dao.query.QueryBuilder;/** * @author xuanyouwu * @email xuanyouwu@163.com * @time 2015-12-31 10:33 */public class BaseApplication extends Application { @Override public void onCreate() { super.onCreate(); QueryBuilder.LOG_SQL = true; QueryBuilder.LOG_VALUES =true; }}
dao已经生存好了,接下来就是Orm的增删查改操作了
package com.xuan.greendaoencryp.presenter;import android.content.Context;import com.xuan.db.dao.DaoMaster;import com.xuan.db.dao.DaoSession;import com.xuan.db.entity.Note;import java.util.List;/** * @author xuanyouwu * @email xuanyouwu@163.com */public class NotePresenter { public static final String dbName = "mynote.db"; DaoSession daoSession; DaoMaster daoMaster; DaoMaster.DevOpenHelper helper; public NotePresenter(Context context) { helper = new DaoMaster.DevOpenHelper(context, dbName);//创建数据库 daoMaster = new DaoMaster(helper.getWritableDatabase()); daoSession = daoMaster.newSession(); } public long add(Note note) throws Exception { return daoSession.insertOrReplace(note); } public void update(Note note) throws Exception { daoSession.update(note); } public List<Note> loadAll() throws Exception { return daoSession.getNoteDao().loadAll(); } public void delete(Note note) throws Exception { daoSession.delete(note); } public void close() { daoSession.clear(); daoSession = null; helper.close(); helper = null; }}
<pre name="code" class="java">package com.xuan.greendaoencryp;import android.os.Bundle;import android.support.annotation.Nullable;import android.support.v7.app.AppCompatActivity;import android.text.format.DateUtils;import android.view.View;import com.xuan.db.entity.Note;import com.xuan.greendaoencryp.presenter.NotePresenter;import com.xuan.greendaoencryp.utils.LogUtils;import java.util.Calendar;/** * @author xuanyouwu * @email xuanyouwu@163.com * @time 2016-01-07 17:50 */public class NoteActivity extends AppCompatActivity { NotePresenter presenter; Note note; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_note); presenter = new NotePresenter(this); note = new Note(); note.setTitle("这是第一个笔记"); note.setContent("笔记的内容是greendao"); note.setCreateTime(DateUtils.formatElapsedTime(Calendar.getInstance().getTimeInMillis())); } public void onAdd(View v) { try { presenter.add(note); } catch (Exception e) { e.printStackTrace(); } } public void onQuery(View v) { try { LogUtils.d("query all:" + presenter.loadAll()); } catch (Exception e) { e.printStackTrace(); } } public void onUpdate(View v) { note.setCreateTime(DateUtils.formatElapsedTime(Calendar.getInstance().getTimeInMillis())); try { presenter.update(note); } catch (Exception e) { e.printStackTrace(); } try { LogUtils.d("update after query all:" + presenter.loadAll()); } catch (Exception e) { e.printStackTrace(); } } public void onDelete(View v) { try { presenter.delete(note); } catch (Exception e) { e.printStackTrace(); } try { LogUtils.d("delete after query all:" + presenter.loadAll()); } catch (Exception e) { e.printStackTrace(); } } @Override protected void onDestroy() { super.onDestroy(); presenter.close(); }}
下一篇就详细介绍greendao 的api
- GreenDao 使用教程 一
- Android greenDao 使用教程(一)源码编译与使用
- greenDao的使用教程
- GreenDao框架使用教程
- GreenDao 使用教程 二
- GreenDao 使用教程
- greendao使用教程
- GreenDAO 学习笔记-小白教程(一)GreenDAO的简介和基本使用
- Android 配置使用 GreenDao 教程
- GreenDao 3.2.0使用教程
- greenDao的简单使用教程
- greenDao Orm 基本使用<一>
- GreenDao官方文档翻译之(一)开始使用GreenDao
- Android 关于greenDao的使用教程
- Android ORM框架GreenDao使用教程
- Android greenDao入门以及使用教程
- GreenDao 3.2.0 的基本使用教程
- Android GreenDao使用教程3.2.2
- java的一些简单题
- FC光纤通道
- 一个对前端模板技术的全面总结
- tcpdump and libpcap
- 2015河南科技学院CSDN高校俱乐部---见面会
- GreenDao 使用教程 一
- 高效率http retrofit2.0.2 最新版本
- 修改linux最大连接数
- 选项菜单
- digit-counts
- 进程与进程,进程与内核间通信(netlink)
- led灯点亮
- IntelliJ IDEA 使用心得与常用快捷键
- 链表删除节点--算法复杂度o(1)