AndroidAnnnotations注入架構使用之第三方框架組成OrmLite(十五)
(一).前言:
前面我們已經對於AndroidAnnotations框架組成Otto事件匯流排做了講解,今天我們開始具體學習一下第三方框架組成Ormlite資料庫持久化架構。主要為了我們更加方便對資料庫操作使用。
OrmLite的官網:http://ormlite.com/sqlite_java_android_orm.shtml
(二).使用介紹:
自AndroidAnnotations2.7開始, 我們可以使用@OrmLiteDao來進行註解OrmLite DAOs架構。
【注】最低支援的版本為ORMLite4.21
@OrmLiteDao有以下一個強制性的屬性:
helper應該持有databasehelper的引用(該類需要繼承自com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper)
【注】不要擷取和釋放該helper,我們使用的OpenHelperManager,是不能在同一事件同時處理不同的helpers。所以如果當你在使用多個databasehelpers,需要小心使用OrmLite註解。使用執行個體如下:
@EActivitypublic classMyActivity extends Activity { // UserDao is a Dao @OrmLiteDao(helper = DatabaseHelper.class) UserDao userDao; @OrmLiteDao(helper = DatabaseHelper.class) Dao carDao; }
在AndroidAnnotations4.0之前,在AndroidAnnotations早期的版本中,@OrmLiteDao有model第二個強制的屬性,就和和DAO相關model對象。
(三).DAO運行時異常:
自AndroidAnnotations3.0起,在3.0版本之前,所有DAO的子類都可以使用@OrmLiteDao進行註解。現在我們還是可以使用RuntimeExceptionDao的子類進行處理。
自AndroidAnnotations3.3起,現在可以註解擴充自RuntimeExceptionsDao的子類,該類必須要有一個建構函式,傳入和Dao模型。使用執行個體如下:
public classUserRuntimeExceptionDao extends RuntimeExceptionDao { public UserRuntimeExceptionDao(Dao dao) { super(dao); } } @EActivitypublic classMyActivity extends Activity { @OrmLiteDao(helper = DatabaseHelper.class) UserRuntimeExceptionDao userDao; }
到此位置關於AndroidAnnotations第三方框架組成之OrmLite整合已經全部講解完成了。