android項(xiàng)目里面很多都會(huì)有使用sqlite來保存數(shù)據(jù)碉京。原生api真心不好使啊,要寫超多超多的代碼螟深,還要寫顧慮很多細(xì)節(jié)問題谐宙。于是乎就想偷懶了,干脆去網(wǎng)上找個(gè)orm框架吧界弧!
Ok凡蜻,google it搭综。篩選一下,就鎖定了ormlite和greendao咽瓷。簡單看了一下设凹,ormlite簡單好用,比較符合JavaEE開發(fā)者使用習(xí)慣茅姜,注解真的很好用啊月匣!再去greendao官網(wǎng)逛逛钻洒,他說我們的目標(biāo)是:
最牛掰的性能
超好用的API
為Android大大優(yōu)化
最小的內(nèi)存使用
******
再翻翻看,還有同ormlite的性能對比:
上面可以看到锄开,greeendao的insert和update效率要比ormlite快兩倍左右素标,load更是夸張到4倍多。尼瑪也太厲害了吧萍悴,優(yōu)化這么狠头遭。這么一大堆好處,還不趕緊使使癣诱。
我們可以在官網(wǎng)上直接下來计维,也可去github項(xiàng)目主頁上下載源碼。建議去下載github哈撕予,因?yàn)橛性创a有列子鲫惶,比較直觀易懂。源碼使用gradle構(gòu)建实抡,需要安裝gradle插件欠母。其實(shí)真正也只有依賴一個(gè)freemaker.jar,直接網(wǎng)上下載一個(gè)就好吆寨。下面新建一個(gè)java工程赏淌,注意是java工程不是android工程。導(dǎo)入freemaker.jar和greendao-generator.jar啄清,加入到build path六水。建一個(gè)如下的類:
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);
}
}
代碼號(hào)簡單的話,看名字就知道是什么意思了盒延。greendao支持各種類型的哇缩擂,還支持一對一、一對多添寺、多對多的關(guān)系胯盯,很強(qiáng)悍!直接運(yùn)行计露,代碼生成
自動(dòng)生成model和dao博脑,倍兒爽憎乙!隨便看一個(gè)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代碼塊中是手動(dòng)加入了,當(dāng)設(shè)置了
1
schema.enableKeepSectionsByDefault
后叉趣,該部分代碼塊在下次更新的時(shí)候會(huì)保留下來泞边。
dao類中也有各種基本的方法,如insert,update,delete等等疗杉≌笱瑁基本可能完成大部分需求了,終于不用寫那么繁瑣的數(shù)據(jù)庫操作啦烟具!
再看看怎么在client獲取到dao梢什,注意client要加入greendao.jar哦。有了dao就可以對數(shù)據(jù)庫各種操作了朝聋!
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使用簡單,學(xué)習(xí)成本低冀痕,容易上手荔睹,效率比greendao偏慢一點(diǎn)。greendao耦合性高言蛇,使用時(shí)要另外使用一個(gè)java工程創(chuàng)建僻他,開始環(huán)境搭建比較麻煩,但是一旦上手還是十分容易使用的猜极,并且效率最好中姜。個(gè)人還是推薦使用greendao。