android的ORM

  orm框架 

   对象关系映射英语Object Relational Mapping,简称ORM,或O/RM,或O/R mapping),是一种程序技术,用于实现面向对象编程语言里不同类型系统的数据之间的转换。从效果上说,它其实是创建了一个可在编程语言里使用的“虚拟对象数据库”。

简单的说:ORM相当于中继数据。具体到产品上,例如ADO.NET Entity Framework。DLINQ中实体类的属性[Table]就算是一种中继数据。

 

    筛选一下,好用的有ormlite和greendao。简单看了一下,ormlite简单好用,比较符合JavaEE开发者使用习惯,注解真的很好用啊!再去 greendao官网 逛逛,他说我们的目标是:

    • 最牛掰的性能
    • 超好用的API
    • 为Android大大优化
    • 最小的内存使用
    • ******
再翻翻看,还有同ormlite的性能对比:
上面可以看到,greeendao的insert和update效率要比ormlite快两倍左右,load更是夸张到4倍多。尼玛也太厉害了吧,优化这么狠。这么一大堆好处,还不赶紧使使。

    我们可以在官网上直接下来,也可去github项目主页上下载源码。建议去下载github哈,因为有源码有列子,比较直观易懂。

    源码使用gradle构建,需要安装gradle插件。其实真正也只有依赖一个freemaker.jar,直接网上下载一个就好。

     下面新建一个java工程,注意是java工程不是android工程。导入freemaker.jar和greendao-generator.jar,加入到build path。建一个如下的类:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
public class DaoGenerator {
 
     public static void main(String[] args) throws Exception {
         // first parameter for version, <span></span> second for default generate package
         Schema schema = new Schema( 1 , "com.xckevin.example.mode<span></span>l" );
 
         addNote(schema);
         addCustomerOrder(schema);
         addUser(schema);
         // set dao class generate package
         schema.setDefaultJavaPackageDao( "com.xckevin.example.dao" );
         // keep custom code block
         schema.enableKeepSectionsByDefault();
         new DaoGenerator().generateAll(schema, "../GreenDaoExample/src" );
     }
 
     private static void addNote(Schema schema) {
         Entity note = schema.addEntity( "Note" );
         note.addIdProperty();
         note.addStringProperty( "text" ).notNull();
         note.addStringProperty( "comment" );
         note.addDateProperty( "date" );
     }
     
     private static void addUser(Schema schema) {
         Entity user = schema.addEntity( "User" );
         user.setTableName( "t_user" );
         user.addIdProperty();
         user.addStringProperty( "account" ).unique();
         user.addStringProperty( "password" );
         user.addDateProperty( "birthday" );
         user.addShortProperty( "gender" );
         user.addIntProperty( "height" );
         user.addFloatProperty( "weight" );
         user.addDateProperty( "registerTime" );
         user.implementsInterface( "Jsonable<User>" );
     }
 
     private static void addCustomerOrder(Schema schema) {
         Entity customer = schema.addEntity( "Customer" );
         customer.addIdProperty();
         customer.addStringProperty( "name" ).notNull();
 
         Entity order = schema.addEntity( "Order" );
         order.setTableName( "ORDERS" ); // "ORDER" is a reserved keyword
         order.addIdProperty();
         Property orderDate = order.addDateProperty( "date" ).getProperty();
         Property customerId = order.addLongProperty( "customerId" ).notNull().getProperty();
         order.addToOne(customer, customerId);
 
         ToMany customerToOrders = customer.addToMany(order, customerId);
         customerToOrders.setName( "orders" );
         customerToOrders.orderAsc(orderDate);
     }
 
}


代码号简单的话,看名字就知道是什么意思了。greendao支持各种类型的哇,还支持一对一、一对多、多对多的关系,很强悍!直接运行,代码生成

自动生成model和dao,倍儿爽!随便看一个model类:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package com.xckevin.example.model;
 
// THIS CODE IS GENERATED BY greenDAO, EDIT ONLY INSIDE THE "KEEP"-SECTIONS
 
// KEEP INCLUDES - put your custom includes here
import org.json.JSONException;
import org.json.JSONObject;
// KEEP INCLUDES END
/**
  * Entity mapped to table t_user.
  */
public class User implements Jsonable<User> {
 
     private Long id;
     private String account;
     private String password;
     private java.util.Date birthday;
     private Short gender;
     private Integer height;
     private Float weight;
     private java.util.Date registerTime;
 
     // KEEP FIELDS - put your custom fields here
     // KEEP FIELDS END
 
     public User() {
     }
 
     public User(Long id) {
         this .id = id;
     }
 
     public User(Long id, String account, String password, java.util.Date birthday, Short gender, Integer height, Float weight, java.util.Date registerTime) {
         this .id = id;
         this .account = account;
         this .password = password;
         this .birthday = birthday;
         this .gender = gender;
         this .height = height;
         this .weight = weight;
         this .registerTime = registerTime;
     }
 
     public Long getId() {
         return id;
     }
 
     public void setId(Long id) {
         this .id = id;
     }
 
     public String getAccount() {
         return account;
     }
 
     public void setAccount(String account) {
         this .account = account;
     }
 
     public String getPassword() {
         return password;
     }
 
     public void setPassword(String password) {
         this .password = password;
     }
 
     public java.util.Date getBirthday() {
         return birthday;
     }
 
     public void setBirthday(java.util.Date birthday) {
         this .birthday = birthday;
     }
 
     public Short getGender() {
         return gender;
     }
 
     public void setGender(Short gender) {
         this .gender = gender;
     }
 
     public Integer getHeight() {
         return height;
     }
 
     public void setHeight(Integer height) {
         this .height = height;
     }
 
     public Float getWeight() {
         return weight;
     }
 
     public void setWeight(Float weight) {
         this .weight = weight;
     }
 
     public java.util.Date getRegisterTime() {
         return registerTime;
     }
 
     public void setRegisterTime(java.util.Date registerTime) {
         this .registerTime = registerTime;
     }
 
     // KEEP METHODS - put your custom methods here
     @Override
     public User parse(JSONObject jsonObj) {
         // TODO Auto-generated method stub
         try {
             id = jsonObj.getLong( "id" );
             account = jsonObj.getString( "account" );
             return this ;
         } catch (JSONException e) {
             e.printStackTrace();
         }
         return null ;
     }
     // KEEP METHODS END
 
}

注意上面的// KEEP代码块中是手动加入了,当设置了后,该部分代码块在下次更新的时候会保留下来。

dao类中也有各种基本的方法,如insert,update,delete等等。

基本可能完成大部分需求了,终于不用写那么繁琐的数据库操作啦!

再看看怎么在client获取到dao,注意client要加入greendao.jar哦。有了dao就可以对数据库各种操作了!


?
1
2
3
4
5
         DevOpenHelper helper = new DaoMaster.DevOpenHelper( this , "notes-db" , null );
         db = helper.getWritableDatabase();
         daoMaster = new DaoMaster(db);
         daoSession = daoMaster.newSession();
         userDao = daoSession.getUserDao();


总体来说,ormlite使用简单,学习成本低,容易上手,效率比greendao偏慢一点greendao耦合性高,使用时要另外使用一个java工程创建,开始环境搭建比较麻烦,但是一旦上手还是十分容易使用的,并且效率最好。个人还是推荐使用greendao。


-------------------------------------------------------------------------------------------------------------------------

androrm, ormlite 的测试比较


简单测试一下这两个框架在同一设备下,插入1w(本来是想插100w,后来插入10w,接着就只能插1w,呵呵有兴趣的可以去测试一下…)行数据的时间吧,给大家做一个简单参考,真正要做比较的话,其实,测试,表查询才是最重要的,但是,关键我没有这样的数据源,要构建一个挺耗时间的.

测试用设备

 

设备名 原道N10
主控方案 RK2918
CPU 1 ghz
RAM 512 MB
系统版本 2.3.1
象限(quadrant stand) 2000分左右

 

影响整个测试的硬件指标估计就这几个了,测试的环境就是上表的数据了

图表

R语言生成的:

Rplot

最快当然是直接用sqlite…(废话),从表中我们可以比较出,就ORM框架而言androrm有一丁点的速度优势,可能由于ormlite用注解字段的方式,

导致ormlite性能有着一定的损失(注解其实也是利用了反射的原理),不过,对于熟悉j2ee的朋友来讲ormlite更容易上手,

而对于python程序员学习过django这个框架的朋友更容易上手androrm.

从这个简单的实验来看,官方推荐我们少用get/set方法也不是毫无道理的,对于一个类的反射的耗时,

以我的那台设备而言开销可能大约在2毫秒左右(这个以第三次androrm 与sqlite的相减再除与10000得出..),

注意…这只是简单的测试而已!!!真正要比较性能还要考虑到GC的问题,所以这里这是随便说说而已!

文档活跃度

androrm ormlite
文档 不完善 超级齐全
社区 不活跃,我提交到一个bug,到现在都还没有修复的消息 活跃
更新频率 很快!



个人建议,想研究怎么写orm框架的可以用androrm,想速度的开发产品,用ormlite,其实,啥都不用最好,呵呵~

有兴趣的朋友可以下载我用来测试的源代码试一下

http://www.kuaipan.cn/file/id_2622545685705265.html

http://my.oschina.net/u/724985/blog/208038
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值