LoginSignup
9
8

More than 5 years have passed since last update.

ORMラッパーDBFlow v3.0-beta1の使い方

Last updated at Posted at 2015-12-28

ORMラッパーDBFlow v3.0-beta1の使い方

インストール

1.gradleの設定
 TOPレベルのbuild.gradleのdependenciesに
 classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'を追加

 allprojectsのrepositoriesに maven { url "https://jitpack.io" }を追加
 
 アプリレベルのbuild.gradleで
 apply plugin: 'com.android.application'の下に
 apply plugin: 'com.neenbedankt.android-apt'を追加

 dependenciesに下記を追加
 apt 'com.github.Raizlabs.DBFlow:dbflow-processor:3.0.0-beta1'
 compile "com.github.Raizlabs.DBFlow:dbflow-core:3.0.0-beta1"
 compile "com.github.Raizlabs.DBFlow:dbflow:3.0.0-beta1"

2.Applicationを継承したExampleApplicationクラスを作成
例としてAlertApplicationクラスを作成。
ここでDBを初期化

AlertApplication

package {your package}.alert;

import android.app.Application;
import com.raizlabs.android.dbflow.config.FlowManager;

/**
 * アプリケーションクラス
 */
public class AlertApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        FlowManager.init(this);
    }
}

3.作成したapplicationクラスをmanifest.xmlに追加


    <application
        android:allowBackup="true"
        ・・・・
        android:name="{your package}.AlertApplication">

4.DBの作成
DB名、versionを指定しDBを作成する。

AlertDb
import com.raizlabs.android.dbflow.annotation.Database;

/**
 * Alert DB
 */
@Database(name = AlertDb.NAME, version = AlertDb.VERSION, generatedClassSeparator = "_")
public class AlertDb {
    public static final String NAME = "AlertDb";
    public static final int VERSION = 1;
}

5.BaseModelを継承しテーブルを作成

Recipient
import com.raizlabs.android.dbflow.annotation.Column;
import com.raizlabs.android.dbflow.annotation.PrimaryKey;
import com.raizlabs.android.dbflow.annotation.Table;
import com.raizlabs.android.dbflow.structure.BaseModel;

/**
 * 宛先テーブル
 */
@Table(database = AlertDb.class)

public class Recipient extends BaseModel {

    @PrimaryKey(autoincrement = true)
    public long id;

    @Column
    public String name;

    @Column
    public String email;

    @Column
    public Integer is_delivery;
}

6.レコードの取得とインサートメソッドの作成

import android.util.Log;
import com.lilly.happylife.alert.model.Recipient;
import com.lilly.happylife.alert.model.Recipient_Table;
import com.raizlabs.android.dbflow.sql.language.SQLite;

/**
 * 宛先リポジトリ
 */
public class RecipientRepository {

    /**
     * Recipientテーブルにインサート
     *
     * @param String name  氏名
     * @param String email メールアドレス
     */
    public void insert(String name, String email) {
        Recipient recipient   = new Recipient();
        recipient.name        = name;
        recipient.email       = email;
        recipient.is_delivery = 1;
        recipient.save();
    }

    /**
     * プライマリーキーで宛先テーブルを検索して取得
     *
     * @param Long _id ID
     *
     * @return Recipient recipient Recipientオブジェクト
     */
    public Recipient getRecipient(Long id) {
        Recipient recipient = SQLite.select().from(Recipient.class)
            .where(Recipient_Table.id.eq(id)).and(Recipient_Table.is_delivery.eq(1)).querySingle();

        String query = SQLite.select().from(Recipient.class)
            .where(Recipient_Table.id.eq(id)).and(Recipient_Table.is_delivery.eq(1)).getQuery();

        Log.d("DbQuery", query);
        return recipient;
    }
}

インサートはサーバサイドのORMと同じような感じで使える
取得は最初の条件はwhereを使い次からはandメソッド
fromメソッドにテーブルのクラスファイルを指定
カラムを条件に使うときはテーブル_Tableのstaticプロパティを使う
実行したクエリはgetQueryで取得
もちろん書いていないが、生SQLもできる
このDBFlowはサーバサイドで使用していたORMラッパーに近い。
楽!!!
Recipient_Table(確認したほうが良い!!!)はコンパイルした後にRecipient.classと同じ階層に自動生成される。
Recipient_Adapter、Recipient_Containerも自動生成される。

9
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
8