android项目里面很多都会有使用sqlite来保存数据。原生api真心不好使啊,要写超多超多的代码,还要写顾虑很多细节问题。于是乎就想偷懒了,干脆去网上找个orm框架吧!
Ok,google it。筛选一下,就锁定了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
56publicclassDaoGenerator {
publicstaticvoidmain(String[] args)throwsException {
// first parameter for version, second for default generate package
Schema schema =newSchema(1,"com.xckevin.example.model");
addNote(schema);
addCustomerOrder(schema);
addUser(schema);
// set dao class generate package
schema.setDefaultJavaPackageDao("com.xckevin.example.dao");
// keep custom code block
schema.enableKeepSectionsByDefault();
newDaoGenerator().generateAll(schema,"../GreenDaoExample/src");
}
privatestaticvoidaddNote(Schema schema) {
Entity note = schema.addEntity("Note");
note.addIdProperty();
note.addStringProperty("text").notNull();
note.addStringProperty("comment");
note.addDateProperty("date");
}
privatestaticvoidaddUser(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");
}
privatestaticvoidaddCustomerOrder(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
123packagecom.xckevin.example.model;
// THIS CODE IS GENERATED BY greenDAO, EDIT ONLY INSIDE THE "KEEP"-SECTIONS
// KEEP INCLUDES - put your custom includes here
importorg.json.JSONException;
importorg.json.JSONObject;
// KEEP INCLUDES END
/**
* Entity mapped to table t_user.
*/
publicclassUserimplementsJsonable {
privateLong id;
privateString account;
privateString password;
privatejava.util.Date birthday;
privateShort gender;
privateInteger height;
privateFloat weight;
privatejava.util.Date registerTime;
// KEEP FIELDS - put your custom fields here
// KEEP FIELDS END
publicUser() {
}
publicUser(Long id) {
this.id = id;
}
publicUser(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;
}
publicLong getId() {
returnid;
}
publicvoidsetId(Long id) {
this.id = id;
}
publicString getAccount() {
returnaccount;
}
publicvoidsetAccount(String account) {
this.account = account;
}
publicString getPassword() {
returnpassword;
}
publicvoidsetPassword(String password) {
this.password = password;
}
publicjava.util.Date getBirthday() {
returnbirthday;
}
publicvoidsetBirthday(java.util.Date birthday) {
this.birthday = birthday;
}
publicShort getGender() {
returngender;
}
publicvoidsetGender(Short gender) {
this.gender = gender;
}
publicInteger getHeight() {
returnheight;
}
publicvoidsetHeight(Integer height) {
this.height = height;
}
publicFloat getWeight() {
returnweight;
}
publicvoidsetWeight(Float weight) {
this.weight = weight;
}
publicjava.util.Date getRegisterTime() {
returnregisterTime;
}
publicvoidsetRegisterTime(java.util.Date registerTime) {
this.registerTime = registerTime;
}
// KEEP METHODS - put your custom methods here
@Override
publicUser parse(JSONObject jsonObj) {
// TODO Auto-generated method stub
try{
id = jsonObj.getLong("id");
account = jsonObj.getString("account");
returnthis;
}catch(JSONException e) {
e.printStackTrace();
}
returnnull;
}
// KEEP METHODS END
}
注意上面的// KEEP代码块中是手动加入了,当设置了
1
schema.enableKeepSectionsByDefault
后,该部分代码块在下次更新的时候会保留下来。
dao类中也有各种基本的方法,如insert,update,delete等等。基本可能完成大部分需求了,终于不用写那么繁琐的数据库操作啦!
再看看怎么在client获取到dao,注意client要加入greendao.jar哦。有了dao就可以对数据库各种操作了!
1
2
3
4
5DevOpenHelper helper =newDaoMaster.DevOpenHelper(this,"notes-db",null);
db = helper.getWritableDatabase();
daoMaster =newDaoMaster(db);
daoSession = daoMaster.newSession();
userDao = daoSession.getUserDao();
总体来说,ormlite使用简单,学习成本低,容易上手,效率比greendao偏慢一点。greendao耦合性高,使用时要另外使用一个java工程创建,开始环境搭建比较麻烦,但是一旦上手还是十分容易使用的,并且效率最好。个人还是推荐使用greendao。