android 超輕量級的ORM架構

來源:互聯網
上載者:User

android 超輕量級的ORM架構
一、測試效能Demo首先給大家一個效能測試的Demo,看看效能如何。 Demo.apk

二、相關操作CRUDEntity:

import android.annotation.SuppressLint;//依賴注入配置@SuppressLint(UseValueOf)public class Entity {/** * 預設主鍵,自增。最好有id主鍵,便於debug和邏輯判斷。 *  * 暫不支援自訂主鍵 */int id;public boolean base_boolean;public double base_double;public float base_float;public int base_int;public long base_long;public Boolean mBoolean;public Double mDouble;public Float mFloat;public Integer mInteger;public Long mLong;public String mString;}

initDB:
private DB db= DB.getInstance(mContext);
//依賴注入
@SuppressLint(UseValueOf)
1.增加:
Entity entity = new Entity();entity.base_boolean = true;entity.base_double = 10.2;entity.base_float = 9.1f;entity.base_int = 99;entity.base_long = System.currentTimeMillis();entity.mBoolean = new Boolean(true);entity.mDouble = new Double(10.2);entity.mFloat = new Float(9.1);entity.mInteger = new Integer(99);entity.mLong = new Long(System.currentTimeMillis() / 1000);entity.mString = new String(test);db.save(entity);

2.刪除
List list = db.findAll(Entity.class);if (list != null && list.size() > 0) {Entity entity = list.get(0);db.delete(entity);}
//刪除所有// db.deleteAll(Entity.class);
//按條件刪除// db.deleteByWhere(Entity.class, );

3.更新
List list = db.findAll(Entity.class);if (list.size() > 0) {Entity entity = list.get(0);entity.base_int = 23482374;
//按條件更新// db.update(Entity, id=1);db.update(entity);}

4.查詢
List list = db.findAll(Entity.class);//條件查詢//db.findAllByWhere(Entity.class, id=1);

5.刪除表
db.dropTable(Entity.class);

6.添加1000條資料,添加事務
// 開始事務。提高效率(不用事務,插入1000條資料可能要十幾秒,用事務可能不到1秒,具體要看類是否複製)db.beginTrancation();
for (int i = 0; i < 1000; i++) {db.save(entity);}
db.endTrancation();

 














聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.