SoFunction
Updated on 2025-04-09

Tutorial on using built-in ORM function of Android data caching framework

The usage tutorial is as follows

Configuration Initialization

(this, ()
                .database("dcache_sample")
                .tables(Account::)
                .version(1)
                .build())

Add a row of configuration to the entrance of the custom Application class. The database is the database name. The version starts from 1 and increments by 1 each time. Tables are used to configure the tables that need to be initialized. All tables in dcache need to implement the OrmTable interface.

Detailed explanation of the annotation

Table and column related

@Table

This annotation is configured on the class name of the OrmTable implementation class and is used to specify the name of the class mapped to the table

@Column

This annotation is configured on the member properties of the OrmTable implementation class and is used to specify the name of the property mapped to the field

@Ignore

The priority of this annotation is higher than @Column, and is configured on the member attribute of the implementation class of OrmTable. The member attribute of this annotation is configured and will not be mapped as a field of the table.

Constraint related

@NotNull

This annotation is configured on the member properties of the OrmTable implementation class and is used to specify that this field is a non-empty field

@PrimaryKey

This annotation is configured on the member attribute of the OrmTable implementation class and is used to specify that this field is the primary key of the table

@Id

This annotation is configured on the member properties of the OrmTable implementation class, and works similar to @PrimaryKey, and

On its basis, the field name is "_id", which is equivalent to @PrimaryKey+@Column("_id")

@Unique

This annotation is configured on the member properties of the OrmTable implementation class, indicating that the value of this field is never repeated in this table

@Default

This annotation is configured on the member properties of the OrmTable implementation class, through which you can specify the default value to the field

CRUD operation

Insert data

(Account::).insert(Account(generateAccKey(),
                    "D"+generateAccKey(), "P"+generateAccKey()))

Insert can be used not only to insert a single piece of data, but also to insert a List data

Delete data

val selectOne = (Account::)
                    .selectOne(().orderBy(OrmTable.INDEX_ID))
            if (selectOne != null) {
                (Account::).delete(selectOne)
            }

Update data

(Account::).update(Account("This is the key",
                    "D"+generateAccKey(), "P"+generateAccKey()))

Query data

Condition

selection: where clause, without where, can be provided with "?" placeholder

selectionArgs: "?" all values ​​of placeholder

WhereBuilder

The construction class of the where clause is created by ()

public WhereBuilder addWhereEqualTo(String column, Object value) {
        return append(null, column + EQUAL_HOLDER, value);
    }

You can add the key=value condition by calling addWhereEqualTo.

QueryBuilder

Support where, orderBy, limit, groupBy, etc.

Query records

val count = (Account::).selectCount()

Query the number of records that meet the query conditions through selectCount.

Other precautions

Complex data type field mapping

@Convert(converter = , columnType = )
@Column("acc_child_values")
private List<String> accChildValues;

Use the @Convert annotation to save complex data types, such as ArrayList. Generally, complex data types are converted into formatted string types and saved to the database, and automatic decoding operations are performed when reading data.

The converter type converter can be defined by itself, and the columnType is the actual data type you save to the database.

Table structure upgrade

  @Override
  public boolean isUpgradeRecreated() {
      return false;
  }

You only need to upgrade the database version by 1 in the configuration to automatically upgrade the table structure. The implementation class of OrmTable isUpgradeRecreated() is to determine whether the previously saved data should be cleared after the table is upgraded. If return true, the data will be cleared after the table is upgraded.

Transaction operations

(Account::) {
                val selectOne = 			(().orderBy(OrmTable.INDEX_ID))
                if (selectOne != null) {
                    (selectOne)
                }
            }

Use() to perform transaction operations in a code block, and it refers to OrmDao<Account>.

The above is the detailed content of the tutorial on using the built-in ORM function of the Android Data Cache Framework. For more information about the Android Data Cache Framework ORM, please pay attention to my other related articles!