greenDao使用进阶

在之前的一遍greenDao基础使用中学习了greenDao的集成及简单的使用,现在开启进阶之路。学习下使用中的细节。

Annotations

在之前的配置中我们使用到了@Entity@Id注解,除了这俩greenDao中还有其他注解来方便我们配置数据库。下面一一介绍


@Entity
public class Student {
    @Id
    private String id;
    @NotNull
    private String name;
    @Property(nameInDb = "student_age")
    private int age;
    @Transient
    private String address;
}

@Entity

我们使用Entity注解来指定 为某个类创建一个表。
下面是Entity的源码,我们可以从中看到在制定表的时候我们可以设置很多参数。

public @interface Entity {

    /**
     * Specifies the name on the DB side (e.g. table name) this entity maps to. By default, the name is based on the entities class name.
     */
    String nameInDb() default "";

    /**
     * Indexes for the entity.
     * 

* Note: To create a single-column index consider using {@link Index} on the property itself */ Index[] indexes() default {}; /** * Advanced flag to disable table creation in the database (when set to false). This can be used to create partial * entities, which may use only a sub set of properties. Be aware however that greenDAO does not sync multiple * entities, e.g. in caches. */ boolean createInDb() default true; /** * Specifies schema name for the entity: greenDAO can generate independent sets of classes for each schema. * Entities which belong to different schemas should not have relations. */ String schema() default "default"; /** * Whether update/delete/refresh methods should be generated. * If entity has defined {@link ToMany} or {@link ToOne} relations, then it is active independently from this value */ boolean active() default false; }

一般来说我们只需要使用@Entity就可以满足我们的需求了。

@Id

将某个属性设置为主键(primary key)。也可以为Long/long类型的值制定自增长。如下:

 @Id(autoincrement = true)
    private Long id;

autoincrement只有在类型为Long/long的时候起作用。

@NotNull

指定某个字段不能为空。

@Property

通过该注解可以为设置在数据库中的列名

@Property(nameInDb = "student_age")
    private int age;

通过上面的代码就会把age对应的数据存到student_age列中。

@Transient

配置了该注解的属性将不会存到数据库中。

CRUD

通过上面的几个注解我们已经可以让greenDao自动生成操作数据库的各种语句了。接下来就是最重要的数据库CRUD操作了。以下的CRUD操作使用这个类作为例子。

@Entity
public class Question {
    @Id
    private String id;
    @NotNull
    private String content;
    private Long start;  
}
 DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this,"greendao.db",null);
 SQLiteDatabase db = helper.getWritableDatabase();
 DaoMaster daoMaster = new DaoMaster(db);
 DaoSession daoSession = daoMaster.newSession();

Create


上图显示了插入数据库的方法。Tx结尾的是使用事务提交的。
插入到数据库时我们是直接使用生成的Dao文件来操作的。
以下的实例展示了向数据库中插入一条数据:

QuestionDao questionDao = daoSession.getQuestionDao();
Question question = new Question(UUID.randomUUID().toString(), "这是题目的内容", System.currentTimeMillis());
questionDao.insert(question);

Update

Delete


上图展示了删除的方法。
delete(Question entity)方法是根据查询entitykey之后调用deleteByKey(String key)方法删除的。

Retrieve

接下来就是数据库中最重要的查找方法了。在greenDao中查找数据库也很方便,在基础中我们也看到了。下面详细的来看下。

QueryBuilder


从图中我们看出QueryBuilder有很多的方法供我们选择。方法太多了就不一一介绍了。详细的文档看着里吧

Query build = questionDao.queryBuilder()
                .where(QuestionDao.Properties.Id.eq("1001"))
                .build();
for (Question question : build.list()) {
            Log.i(TAG, "onCreate: "+question.toString());
}

where()是用来配置我们的查询条件的。可以传递的多查询条件,将会使用AND语句查询。
在上面的例子中我们看到了QuestionDao.Properties.Id.eq("1001")这是用来配置查询条件的。
主要有:(详细的文档看着里:官方文档)

  • eq: equals 相等

  • notEq: 不等

  • like: SQL语句中的LIKE ?语句。 想要学习有关LIKE语法可以看这里

  • between SQL语句中的BETWEEN ... AND ... BETWEEN 语法学习看这里

  • in: IN语法学习看这里

  • notIn:

  • gt: greater than ('>')

  • lt: less than ('<')

  • ge: greater or equal ('>=')

  • le: less or equal ('<=')

  • isNull:

  • isNotNull

你可能感兴趣的:(greendao,android)