Android Sugar ORM (1)
Android Sugar ORM
比我之前用過的ORM都要簡單許多, 其目的是簡化與Android中SQLite數據庫的交互, 優點如下:
- 消除了編寫SQL查詢以及與SQLite數據庫進行交互
- 由
Sugar ORM
負責創建數據庫 - 由
Sugar ORM
負責管理對象關係 Sugar ORM
提供了用戶數據庫操作的簡單清晰的api
Android Sugar ORM 的使用
1. 下載
官方文檔上寫的是需要在build.gradle
中添加, 如果使用的Android Studio
, 需要在build.gradle:(Module: app)
下的dependencies
中添加即可, 官網示例:
compile 'com.github.satyan:sugar:1.3'
但是貌似compile
已經被棄用了, 現在使用的是implementation
, 而且有目前我瞭解到的, 有1.4和1.5兩個版本可以使用
implementation 'com.github.satyan:sugar:1.4'
implementation 'com.github.satyan:sugar:1.5'
本篇採用的Sugar ORM: 1.5
2. 配置
開始使用Sugar ORM
前需要在AndroidManifest.xml
中進行簡單的配置, 有一個比較需要注意的點, 就是需要設置<application>
標籤下的android:name
屬性, 以及幾項元數據, 你可以配置爲你想要的包, 這時有兩種方式, 比如:
android:name="com.orm.SugarApp"
使用這種方式時, 只需進行配置元數據即可android:name=".App"
使用這種方式時需要另外做下面幾項修改, 其中.App
是任意你自定義的包
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:name=".App">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data android:name="DATABASE" android:value="data.db" />
<meta-data android:name="VERSION" android:value="1" />
<meta-data android:name="QUERY_LOG" android:value="true" />
<meta-data android:name="DOMAIN_PACKAGE_NAME" android:value="com.example" />
</application>
元數據標記都是可選的, 用途如下:
元數據 | 描述 |
---|---|
DATABASE | 生成的SQLite數據庫名稱 |
VERSION | 數據庫架構的版本 |
QUERT_LOG | 記錄日誌, true:保存;false:不保存 |
DOMAIN_PACKAGE_NAME | 指定存在域/實體類的包名稱 |
配置完以後會發現android:name=".App"
可能會報紅, Android Studio
下按下Alt+Enter
創建App
類, 這時候Android Studio
會自動生成一個App
類繼承於Application
改爲繼承於SugarApp
, 並重寫onCreate()
和onTerminate()
:
public class App extends SugarApp {
@Override
public void onCreate() {
super.onCreate();
SugarContext.init(this);
}
@Override
public void onTerminate() {
SugarContext.terminate();
super.onTerminate();
}
}
3. 創建一個實體
這裏有兩種寫法
public class Book extends SugarRecord<Book> {
String title;
String edition;
public Book(){
}
public Book(String title, String edition){
this.title = title;
this.edition = edition;
}
}
在網上搜一些資料, 有的第二種寫法是繼承的是Model
類, 但是我試了後不行, 我改成了繼承於SugarRecord
類, 試驗後發現也可以, 後來查資料發現這個Model
可能是SugarRecord.Model
, 可能是版本問題, 我用的Sugar ORM: 1.5
版本, 發現SugarRecord
下並沒有Model
, 這個還有待研究一下.
@Table(name = "Book")
public class Book extends SugarRecord {
@Column(name = "title")
String title;
@Column(name = "edition")
String edition;
public Book(){
}
public Book(String title, String edition){
this.title = title;
this.edition = edition;
}
}
4. 基本用法
Sugar ORM
執行CRUD操作非常簡單, 只需要簡單的效用類似於sava()
, delete()
, findById()
這類的方法, 當你存入數據的時候, 會自動生成一個索引, 我們截取SugarRecord
的一部分源代碼就可以看到會有一個id
:
public class SugarRecord {
public static final String SUGAR = "Sugar";
private Long id = null;
private static SQLiteDatabase getSugarDataBase() {
return getSugarContext().getSugarDb().getDB();
}
public static <T> int deleteAll(Class<T> type) {
return deleteAll(type, null);
}
public static <T> int deleteAll(Class<T> type, String whereClause, String... whereArgs) {
return getSugarDataBase().delete(NamingHelper.toSQLName(type), whereClause, whereArgs);
}
}
值得注意的是這個id
是long
類型的, 在用的時候應該沒有什麼太大的問題, 此外官網特別提醒了一下, Sugar ORM
的記錄索引值是從1開始的
保存實體:
Book book = new Book(ctx, "Title here", "2nd edition")
book.save();
加載實體:
Book book = Book.findById(Book.class, 1);
更新實體:
Book book = Book.findById(Book.class, 1);
book.title = "updated title here";
book.edition = "3rd edition";
book.save();
刪除實體:
Book book = Book.findById(Book.class, 1);
book.delete();
批量操作:
// 批量刪除
List<Book> books = Book.listAll(Book.class);
Book.deleteAll(Book.class);
// 批量查詢
Iterator<User> all = User.findAll(User.class);
while (all.hasNext()){
User next = all.next();
Log.i("Sugar ORM", "onCreate: " + next.toString() + ":"+next.getId()+":"+next.username+":"+next.password);
}