Android GreenDAO3.0——entity建模

引言

在项目中,为了使用GreenDAO的自动生成DAO class的功能,我们必须建立entity,该entity通过java注解标识。

Schema是数据库对象集合,我们可以通过gradle插件配置GreenDAO,除此之外,我们至少需要配置Schema的版本:

// In the build.gradle file of your app project:
android {
...
}

greendao {
    schemaVersion 2
}

我们不仅可以配置schemaVersion,还可以配置其他的属性:

daoPackageDAOs、DAOMaster和DAOSession生成的目录,默认是entity所在的目录

targetGenDir:默认在build/generated/source/greendao

其他......(貌似不重要)

@Entity

在Android项目中,GreenDAO通过注解表征entity:

public class User {
    @Id
    private Long id;

    private String name;

    @Transient
    private int tempUsageCount; // not persisted

   // getters and setters for id and user ...
}

我们通过@Entity对User进行注解,GreenDAO通过识别@Entity在编译期生成支持数据库的对象。

@Entity还支持一些属性,对该对象进行描述,此时我们对上述代码进行扩展:

@Entity(
        // If you have more than one schema, you can tell greenDAO
        // to which schema an entity belongs (pick any string as a name).
        schema = "myschema",

        // Flag to make an entity "active": Active entities have update,
        // delete, and refresh methods.
        active = true,

        // Specifies the name of the table in the database.
        // By default, the name is based on the entities class name.
        nameInDb = "AWESOME_USERS",

        // Define indexes spanning multiple columns here.
        indexes = {
                @Index(value = "name DESC", unique = true)
        },

        // Flag if the DAO should create the database table (default is true).
        // Set this to false, if you have multiple entities mapping to one table,
        // or the table creation is done outside of greenDAO.
        createInDb = false,

        // Whether an all properties constructor should be generated.
        // A no-args constructor is always required.
        generateConstructors = true,

        // Whether getters and setters for properties should be generated if missing.
        generateGettersSetters = true
)
public class User {
  ...
}

基本属性

@Entity
public class User {
    @Id(autoincrement = true)
    private Long id;

    @Property(nameInDb = "USERNAME")
    private String name;

    @NotNull
    private int repos;

    @Transient
    private int tempUsageCount;

    ...
}

@Id:对于数据库来说,在数据表中作为主键,类型默认为long型,autoincrement =true使得id自增。

@Property :将java class中的字段名映射为Property 提供的名字,在上述代码中就是将name映射为USERNAME,默认情况下,如果字段是驼峰命名转为下划线命名,如customName 转换为                                CUSTOM_NAME。

@NotNull: 标记基本类型为非空。

@Transient  :表示java class中的该字段不会存储在数据库中,是一个缓存值。

索引

如果我们不喜欢,GreenDAO定制的默认索引,我们可以自行设置新的索引,这是需要属性@index。

@Entity
public class User {
    @Id private Long id;
    @Index(unique = true)
    private String name;
}

id是数据库的唯一索引,即主键。但我们可以通过unique = true,指定name索引也是唯一的。

时间: 2024-08-10 10:12:12

Android GreenDAO3.0——entity建模的相关文章

android greendao3.0 多表关联关系讲解(转)

转自:http://www.jianshu.com/p/dbec25bd575f 前言 之前用过数据库框架:realm.kjdb,今天准备实践学习一下greendao 3.0.greendao 3.0之前的版本有很大的不同,主要是增加了annotation注解,然后表之间和对象之间的关系也通过注解而变得更加灵活方便了.以前用过旧版本的都知道,对于多表多对象之间的关联,要写的代码不少. 我在学习greendao 3.0的时候,有一个感触,网上的文章很多,但是千篇一律,大多都是翻译官方文档而来,举得

Android GreenDAO3.0——介绍

引言 最近,学东西比较零散,各种知识混杂,于是记下学习记录,免得又忘了. 官方网址:http://greenrobot.org/greendao/documentation/introduction/ 首先介绍一下什么是greenDAO,在java object 对象与SQLite数据库之间加入GreenDAO,使得我们的应用程序不直接操作数据库,而是调用greenDAO提供的接口完成数据库操作. 这样做的好处是可以避免我们写大量的数据库操作代码,避免因为疲劳犯下一些低级的错误. GreenDA

Android 6.0 运行时权限处理完全解析

一.概述 随着Android 6.0发布以及普及,我们开发者所要应对的主要就是新版本SDK带来的一些变化,首先关注的就是权限机制的变化.对于6.0的几个主要的变化,查看查看官网的这篇文章http://developer.android.com/intl/zh-cn/about/versions/marshmallow/android-6.0-changes.html,其中当然包含Runtime Permissions. ok,本篇文章目的之一就是对运行时权限处理的一个介绍,以及对目前权限相关的库

android 6.0 系统获取权限

在Android 6.0 (API 23) 发布之前, 所有的权限都在安装应用的时候显示给用户,用户选择安装则表示全部接受这些权限, 之后无法撤销对这些权限的授权.但Android 6.0开始, 一部分比较危险的权限需要在程序运行时显式弹框,请求用户授权.所以,之前开发应用时,在manifest文件中配置对应的权限这种做法便达不到之前的效果.那么,现在要怎么做呢?提供两种思路,仅供参考: 1:判断android系统的版本,如果不是6.0及以上,按正常逻辑处理,如果是,则去打开系统弹框,请求系统权

Android 5.0特性

虽然此前Google对其已经做过了比较深入的介绍,但作为一个如此重要的升级版本,更新内容自然是海量的. Google今天很贴心地在官网上放出了Android 5.0正式版的完整更新日志,涵盖了升级的方方面面,不过说实话也只是一些重要的.大面上的概略更新介绍,还有更多细节等待挖掘. 1.Material Design 醒目的.多彩的.快速响应的UI设计,可为你所有的设备带来一致的直觉体验. - 响应快速.自然流畅的动作,真实的光照和阴影,熟悉的视觉元素,设备导航更加轻而易举. - 惊艳的新色彩,凸

解决Android 5.0中出现的警告:Service Intent must be explicit

extends:http://www.eoeandroid.com/thread-568853-1-1.html 本帖最后由 469874851 于 2015-3-11 18:15 编辑 有些时候我们使用Service的时需要采用隐私启动的方式,但是Android 5.0一出来后,其中有个特性就是Service Intent  must be explitict,也就是说从Lollipop开始,service服务必须采用显示方式启动.而android源码是这样写的(源码位置:sdk/source

修复android 5.0 Xutils的框架问题retry error, curr request is null

Android 5.0手机对xUtils-2.6.13.jar请求时会出现retry error, curr request is null 情况, 修复解决方式: 找到library/src/com/lidroid/xutils/cache/KeyExpiryMap.java 感兴趣的关注一下微信订阅号爱开发:微信号:aikaifa 或扫描一下以下二维码: jar包下载

Android 5.0 怎样正确启用isLoggable(二)__原理分析

前置文章 <Android 5.0 怎样正确启用isLoggable(一)__使用具体解释> 概要 在上文<Android 5.0 怎样正确启用isLoggable(一)__使用具体解释>中分析了isLoggable的用法,本文主要分析isLoggable实现原理以及user版系统root后永久enable isLoggable的原理,并使用脚本自己主动设置isLoggable相关属性. 本文来自http://blog.csdn.net/yihongyuelan 转载请务必注明出处

Android 7.0 Nougat(牛轧糖)---对开发者来说

android 7.0出来了.让你的app准备迎接最新的android版本吧,支持节省电量和内存,这样新的系统行为.使用多窗口UI.直接恢复通知以及其他操作来扩展你的app. android 7.0介绍了各种各样的新功能给用户和开发者, 本文重点介绍面向开发者的一些新功能. 确保检查android 7.0的行为变化,了解有关平台的变化可能会影响你的app. 如果要了解更多的关于用户的新功能,请查看www.android.com. 1.支持多窗口 在android 7.0中,我们介绍了在支持多窗口