AndroidAnnnotations注入架構使用之第三方框架組成OrmLite(十五)

來源:互聯網
上載者:User

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整合已經全部講解完成了。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.