entityクラスの作成
- nameとageをプロパティにもつUserEntityを作成する場合
package com.example.hello.entity;
import org.orman.mapper.Model;
import org.orman.mapper.annotation.Entity;
@Entity
public class UserEntity extends Model<UserEntity> {
@PrimaryKey(autoIncrement=true)
public int id;
public String name;
public int age;
}
Applicationクラスに初期化処理を追記
- Applicationクラスがなければandroid.app.Applicationを継承したクラスを作成してAndroidManifest.xmlのApplication AttributeのNameに登録する
public void onCreate() {
...
Database db = new SQLiteAndroid(this, "dbfile.db");
MappingSession.registerDatabase(db);
MappingSession.registerEntity(UserEntity.class);
MappingSession.start();
...
}
CREATE
UserEntity userEntity = new UserEntity();
userEntity.name = 'Alice';
userEntity.age = 18;
userEntity.insert();
SELECT
all
List<UserEntity> userEntities = Model.fetchAll(UserEntity.class);
all with condition
List<UserEntity> userEntities = Model.fetchQuery(
ModelQuery.select().from(UserEntity.class).where(C.eq("name", "Alice")).getQuery(),
UserEntity.class);
single
UserEntity userEntity = Model.fetchSingle(
ModelQuery.select().from(UserEntity.class).where(C.eq("age", 18)).getQuery(),
UserEntity.class);
UPDATE
userEntity.age = 40;
userEntity.update();
DELETE
userEntity.delete();