logo头像

勤求古训,博采众方

greendao注解

本文于 1549 天之前发表,文中内容可能已经过时。

本文主要记录greendao中注解中的方法

什么是注解

greendao注解

@Id

画外音:表的主键注解

  • 注解的代码:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    /**
    * Marks field is the primary key of the entity's table
    */
    @Retention(RetentionPolicy.SOURCE)
    @Target(ElementType.FIELD)
    public @interface Id {
    /**
    * Specifies that id should be auto-incremented (works only for Long/long fields)
    * Autoincrement on SQLite introduces additional resources usage and usually can be avoided
    * @see <a href="https://www.sqlite.org/autoinc.html">SQLite documentation</a>
    */
    boolean autoincrement() default false;
    }
  • 可以看到一个方法autoincrement,返回值默认是false,顾名思义,主键是否自增。

  • 那么我们在使用的时候其实都没有设置true,所以这里就有个疑问了,为何实际上使用的时候这个主键是自增的?

    这里其实可以看sqlite自增的官方介绍,它里面描述了插入数据时自动用一个未使用的整数填充它,该整数通常比当前使用的最大ROWID多一