美文网首页
GreenDao总结

GreenDao总结

作者: sk_lxf | 来源:发表于2017-11-06 11:23 被阅读0次

    内容依据GreenDao开发文档http://greenrobot.org/greendao/

    android工程中添加GreenDao依赖


    创建表模型


    (一) @Entity 定义实体

        @nameInDb 在数据库中的名字,如不写则为实体中类名,如果引入外库,有查询不到的异常

        @indexes 索引

        @createInDb 是否创建表,默认为true,false时不创建

        @schema 指定架构名称为实体

        @active 无论是更新生成都刷新

    (二) @Id

    (三) @NotNull 不为null

    (四) @Unique 唯一约束

    (五) @ToMany 一对多

    (六) @OrderBy 排序

    (七) @ToOne 一对一

    (八) @Transient 不存储在数据库中

    (九) @generated 由greendao产生的构造函数或方法

        创建javaBean,通过注解@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 {

    ...

    }

    数据模型中field的属性介绍


    @Id数据模型的主键

    @NotNull数据非空

    @Index主键约束

    @Entity

    public class User {

        @Id private Long id;

        @Index(unique = true)

        private String name;

    }

    数据模型创建完成之后操作


    编译当前项目,Idea会在bin目录文件为我们自动创建DaoMaster,DaoSission,XXXDao

        DaoMaster:拥有数据库对象,可以对数据库中表有增删方法。

        DaoSission:Daos的管理工具类

        XXXDao:数据库模型,类似表,对表进行增、删、改、查 。

    相关文章

      网友评论

          本文标题:GreenDao总结

          本文链接:https://www.haomeiwen.com/subject/bkzkmxtx.html