1. ORMLite
特性:
- 通過在類上添加注解設置類
- 強大抽象DAO類
- QueryBuilder 可以靈活的構造簡單和復雜的查詢語句
- 支持MySQL, Postgres, Microsoft SQL Server, H2, Derby, HSQLDB, and Sqlite, 並且能夠容易的擴展到其它的關系數據庫
- 臨時支持DB2, Oracle, ODBC and Netezza.
- 能防止編譯好的語句重復查詢
- 支持外鍵
- 支持基本的數據庫事務
- 自動生成創建和刪除SQL語句
- 支持不用注解配置表和字段
2. androidorm
主要功能特征:
1.實現數據庫與java對象之間的映射轉換
2.增加屬性時自動更新數據庫結構而不影響數據
3.支持sql直接操作,包括取對象列表
4.基於Apache License 2.0,可任意修改或二次開發
ActiveAndroid
主要功能特征:
1.實現數據庫與java對象之間的映射轉換
2.增加屬性時自動更新數據庫結構而不影響數據
3. 支持onetomany
不足:
1. 數據表中一定有一列是id
在項目正式發布進行代碼混淆時需要在proguard配置文件中添加以下部分:
-keep class com.activeandroid.** { *; } -dontwarn com.ikoding.app.biz.dataobject.** -keep public class com.ikoding.app.biz.dataobject.** { *;} -keepattributes *Annotation*
具體來說就是com.activeandroid包中的代碼不做混淆,標注有@Table注解的類不做混淆,並且不能去掉標注有@Table類的屬性上的@Column注解。
性能比較:
硬件環境:模擬器:nexus s(800*480) 內存:512M
插入20000條數據(兩個表有關聯關系,A表4個字段,B表2個字段)
第一次:
框架類型 | 用時(ms) |
ormlite | 38445 |
activeandroid | 132376 |
第二次:
框架類型 | 用時(ms) |
ormlite | 47384 |
activeandroid | 142627 |
第三次:
框架類型 | 用時(ms) |
ormlite | 41974 |
activeandroid | 133260 |
平均:
框架類型 | 用時(ms) |
ormlite | 42601 |
activeandroid | 136087.7 |
插入速度,activeandroid 明顯沒有ormlite 效率高, 都是批量插入。
從20000多條數據中查詢20條
第一次:
框架類型 | 用時(ms) |
ormlite | 310 |
activeandroid | 195 |
第二次:
框架類型 | 用時(ms) |
ormlite | 86 |
activeandroid | 72 |
第三次:
框架類型 | 用時(ms) |
ormlite | 89 |
activeandroid | 80 |
第四次:
框架類型 | 用時(ms) |
ormlite | 95 |
activeandroid | 53 |
第五次:
框架類型 | 用時(ms) |
ormlite | 89 |
activeandroid | 36 |
第六次:
框架類型 | 用時(ms) |
ormlite | 81 |
activeandroid | 61 |
第七次:
框架類型 | 用時(ms) |
ormlite | 87 |
activeandroid | 51 |
平均:除了第一次之外的平均值
框架類型 | 用時(ms) |
ormlite | 87 |
activeandroid | 58 |
現象:
1. 第一次普遍大,框架類型ormlite在300ms左右;而activeandroid在200ms左右。
可能原因是:
1. 第一次的原因是,在查詢是需要與數據庫建立連接,需要耗時長
2. 第二次時間短,不用再建立連接。
3. 在一次之后,某次查詢時間才,因為數據連接被關閉。需要重新建立連接
4. ormlite 使用完連接,很快會釋放, activeandroid 不會很快釋放 所有activeandroid的查詢速度比ormlite 快
備注 :如果讀者有提高ormlite 查詢速度的方案,請留言, 謝謝
測試代碼:
ormlite:
插入:
getHelper().getDao().callBatchTasks(new Callable<Void>(){
@Override
public Void call() throws Exception {
long t1 = System.currentTimeMillis() ;
for(int i = 3 ; i < 10003 ; i ++){
Classes cl = new Classes() ;
cl.setName("cl" + i) ;
getHelper().getClassesDao().create(cl);
Student student2 = new Student() ;
student2.setName("stu" + i) ;
student2.setAge(i) ;
student2.setClasses(cl) ;
getHelper().getDao().create(student2) ;
}
System.out.println(System.currentTimeMillis() - t1);
return null;
}}) ;
查詢:
long t1 = System.currentTimeMillis() ;
QueryBuilder<Student, Integer> builder = getHelper().getDao().queryBuilder() ;
builder.where().le("_id", 50) ;
builder.limit(20l).orderBy("_id", false);
List<Student> stus = getHelper().getDao().query(builder.prepare()) ;
System.out.println(System.currentTimeMillis() - t1);
activeandroid:
插入:
long t1 = System.currentTimeMillis() ;
ActiveAndroid.beginTransaction();
try {
for(int i = 4 ; i < 10004 ; i ++){
Classes cl = new Classes() ;
cl.setName("cl" + i) ;
cl.save();
Student student2 = new Student() ;
student2.setName("stu" + i) ;
student2.setAge(i) ;
student2.setClasses(cl) ;
student2.save();
}
ActiveAndroid.setTransactionSuccessful();
}
finally {
ActiveAndroid.endTransaction();
}
System.out.println(System.currentTimeMillis() - t1);
查詢:
long t1 = System.currentTimeMillis() ;
List<Student> students = new Select().from(Student.class).where("id <= ?", 50).limit(20).orderBy("id desc").execute() ;
System.out.println(System.currentTimeMillis() - t1);