GreenDao3.0学习(一)
来源:互联网 发布:齐鲁证券网上交易软件 编辑:程序博客网 时间:2024/05/16 23:42
本文主要是greendao3.0的快速配置
一.什么是greendao
GreenDao是一个用于Android开发的对象/关系映射(ORM)工具。它向SQLite数据库提供了一个对象导向的接口。主要是将对象映射到SQLite数据库中,GreenDao3.0是greendao的一个新的版本,相对于greendao2.x有更加快捷的配置方式,
二.greendao的特点:
- 性能最大化(对于Android来说,可能是最快的ORM)
- 简单易用API
- 对Android高度优化
- 最小的内存开销
- 较小的文件体积
- 等等
三.greendao的优点与缺点
优点:性能最大化、内存开销最小化、对Android高度优化、操作方便、文件体积比较小
缺点:学习成本大、需要遵循一些规则
greendao官网:
http://greenrobot.org/greendao/
github上的greendao:
https://github.com/greenrobot/greenDAO
greendao3.0配置
前面介绍了这么多,但是好像和本文没有太大的关系,好了,现在我们就开始一步一步的进行greendao3.0的配置。
由于我是使用的android studio开发,所以主要也是按照在android studio中进行配置,
第一步:
在build.gradle
中加入apply plugin: 'org.greenrobot.greendao'
如下图
第二步:
在build.gradle
中加入
buildscript { repositories { mavenCentral() } dependencies { classpath 'org.greenrobot:greendao-gradle-plugin:3.0.0' }}
如下图:
第三步:
在build.gradle
中加入
compile 'org.greenrobot:greendao:3.0.1'compile 'org.greenrobot:greendao-generator:3.0.0'
如下图:
第四步:
在build.gradle
中加入
greendao { schemaVersion 1 daoPackage 'baochen.greendao.dao.gen' targetGenDir 'src/main/java'}
如下图:
这里需要做一下说明: schemaVersion
: 表示的数据库schema的版本号:在升级数据库或者数据迁移的时候需要改变这个值,只要每次都+1就行; daoPackage
: dao的包名,包名默认是entity所在的包; targetGenDir
: 自动生成的数据库文件的目录。
如果上面不指定目录的话,数据库文件就会生成到build/generated/source/greendao下面,还需要拷贝到自己的目录中,所以这里还是指定生成目录为好。
第五步:
在项目中新建一个实体类(如UserBean.java),并加入如下代码:
@Entitypublic class UserBean { @Id private Long id; private String name;}
第六步:
点击bulid下的make project
这时我们会看到UserBean 这个类中自动生成了许多东西,
@Entitypublic class UserBean { @Id private Long id; private String name; public String getName() { return this.name; } public void setName(String name) { this.name = name; } public Long getId() { return this.id; } public void setId(Long id) { this.id = id; } @Generated(hash = 2024802960) public UserBean(Long id, String name) { this.id = id; this.name = name; } @Generated(hash = 1203313951) public UserBean() { }}
同时:在我们刚刚配置的greendao自动生成dao的目录下,生成了如下三个文件:
DaoMaster.java/DaoSession.java/UserBeanDao.java
其中DaoMaster.java/DaoSession.java都是greendao生成的管理数据库的类。比如DaoMaster.java中就会生成数据库schema的版本号:(就是我们刚刚在build.gradle
中设置的版本号:)
public static final int SCHEMA_VERSION = 1;
而自动生成的UserBeanDao.java就是更具我们设置的UserBean生成的,(为什么会生成这个类呢,在下一节中仔细说明)
在UserBeanDao.java这个类中,我们可以看到,生成了一张名为USER_BEAN
的数据表,也是说,只要我们配置好greendao3.0的相关东西,我们不需自己去生写成数据表的sql语句,可以认为UserBeanDao.java就是一张数据表,UserBeanDao.java的代码如下:
/** * DAO for table "USER_BEAN".*/public class UserBeanDao extends AbstractDao<UserBean, Long> { public static final String TABLENAME = "USER_BEAN"; /** * Properties of entity UserBean.<br/> * Can be used for QueryBuilder and for referencing column names. */ public static class Properties { public final static Property Id = new Property(0, Long.class, "id", true, "_id"); public final static Property Name = new Property(1, String.class, "name", false, "NAME"); }; public UserBeanDao(DaoConfig config) { super(config); } public UserBeanDao(DaoConfig config, DaoSession daoSession) { super(config, daoSession); } /** Creates the underlying database table. */ public static void createTable(Database db, boolean ifNotExists) { String constraint = ifNotExists? "IF NOT EXISTS ": ""; db.execSQL("CREATE TABLE " + constraint + "\"USER_BEAN\" (" + // "\"_id\" INTEGER PRIMARY KEY ," + // 0: id "\"NAME\" TEXT);"); // 1: name } /** Drops the underlying database table. */ public static void dropTable(Database db, boolean ifExists) { String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"USER_BEAN\""; db.execSQL(sql); } @Override protected final void bindValues(DatabaseStatement stmt, UserBean entity) { stmt.clearBindings(); Long id = entity.getId(); if (id != null) { stmt.bindLong(1, id); } String name = entity.getName(); if (name != null) { stmt.bindString(2, name); } } @Override protected final void bindValues(SQLiteStatement stmt, UserBean entity) { stmt.clearBindings(); Long id = entity.getId(); if (id != null) { stmt.bindLong(1, id); } String name = entity.getName(); if (name != null) { stmt.bindString(2, name); } } @Override public Long readKey(Cursor cursor, int offset) { return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0); } @Override public UserBean readEntity(Cursor cursor, int offset) { UserBean entity = new UserBean( // cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1) // name ); return entity; } @Override public void readEntity(Cursor cursor, UserBean entity, int offset) { entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0)); entity.setName(cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1)); } @Override protected final Long updateKeyAfterInsert(UserBean entity, long rowId) { entity.setId(rowId); return rowId; } @Override public Long getKey(UserBean entity) { if(entity != null) { return entity.getId(); } else { return null; } } @Override protected final boolean isEntityUpdateable() { return true; }}
到目前为止,我们对greendao3.0的配置基本上完成,虽然配置有点麻烦,但是对于我们后面使用greendao对数据库进行相关操作的时候,会发现很方便。
项目相关地址:https://github.com/mutounaodai/Greendao
- GreenDao3.0学习(一)
- GreenDao3.0学习(二)
- GreenDao3.0学习(三)
- GreenDao3.0使用(一)
- Android GreenDao3.0入门学习
- (一)GreenDao3.0的简单使用
- Android笔记之greenDao3.0学习总结
- GreenDao3.0
- GreenDao3.2入门学习
- greendao3.0以上使用步骤(一):基础使用
- ORM框架之GreenDao3.0集成与注解(一)
- GreenDao3.0 学习笔记——导入以及注解
- GreenDao3.0简单使用
- GreenDao3.0简单使用
- greenDAO3.0使用
- GreenDao3.0升级数据库
- greendao3.0的引入
- GreenDao3.0使用详解
- VS2008编译驱动程序的配置方法
- 高字节低字节大小端模式
- git 常用命令
- [Android] ImageView.ScaleType设置图解
- 三目运算符判断奇数或者是偶数
- GreenDao3.0学习(一)
- java基础的核心技术:多线程(一)
- 第三天学习笔记
- 选择排序法,排序数字或者字符串
- 张明明的cartographer 安装方法 测试可用。
- ROS_Kinetic_x 目前已更新的常用機器人資料 rosbridge agvs pioneer_teleop nao TurtleBot
- 学习Activity的一些注意事项
- 字符串转整数(String to Integer (atoi))
- angular利用$location实现搜索功能。