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
|
package
de.greenrobot.daogenerator.gentest;
import
de.greenrobot.daogenerator.DaoGenerator;
import
de.greenrobot.daogenerator.Entity;
import
de.greenrobot.daogenerator.Property;
import
de.greenrobot.daogenerator.Schema;
import
de.greenrobot.daogenerator.ToMany;
/**
* Generates entities and DAOs for the example project DaoExample.
*
* Run it as a Java application (not Android).
*
* @author Markus
*/
public
class
ExampleDaoGenerator
{
public
static
void
main(String[] args)
throws
Exception
{
Schema schema =
new
Schema(
3
,
"de.greenrobot.daoexample"
);
addNote(schema);
addCustomerOrder(schema);
new
DaoGenerator().generateAll(schema,
"../DaoExample/src-gen"
);
}
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
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);
}
}
|
1
|
Schema schema =
new
Schema(
3
,
"de.greenrobot.daoexample"
);
|
1
2
3
|
addNote(schema);
addCustomerOrder(schema);
new
DaoGenerator().generateAll(schema,
"../DaoExample/src-gen"
);
|
1
2
3
4
5
|
Exception in thread
"main"
java.io.FileNotFoundException: Template
"dao.ftl"
not found.
at freemarker.template.Configuration.getTemplate(Configuration.java:
742
)
at freemarker.template.Configuration.getTemplate(Configuration.java:
665
)
at de.greenrobot.daogenerator.DaoGenerator.<init>(DaoGenerator.java:
68
)
at de.greenrobot.daogenerator.gentest.ExampleDaoGenerator.main(ExampleDaoGenerator.java:
41
)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
greenDAO Generator
Copyright
2011
-
2013
Markus Junginger, greenrobot.de. Licensed under GPL V3.
This program comes with ABSOLUTELY NO WARRANTY
Processing schema version
3
...
Written F:\Android_Ex\work_10\DaoExample\src-gen\de\greenrobot\daoexample\NoteDao.java
Written F:\Android_Ex\work_10\DaoExample\src-gen\de\greenrobot\daoexample\Note.java
Written F:\Android_Ex\work_10\DaoExample\src-gen\de\greenrobot\daoexample\CustomerDao.java
Written F:\Android_Ex\work_10\DaoExample\src-gen\de\greenrobot\daoexample\Customer.java
Written F:\Android_Ex\work_10\DaoExample\src-gen\de\greenrobot\daoexample\OrderDao.java
Written F:\Android_Ex\work_10\DaoExample\src-gen\de\greenrobot\daoexample\Order.java
Written F:\Android_Ex\work_10\DaoExample\src-gen\de\greenrobot\daoexample\DaoMaster.java
Written F:\Android_Ex\work_10\DaoExample\src-gen\de\greenrobot\daoexample\DaoSession.java
Processed
3
entities in 204ms
|
1
|
Entity note = schema.addEntity(
"Note"
);
|
1
|
dao.setTableName(
"NoteList"
);
|
1
|
dao.addIdProperty().primaryKey().autoincrement();
|
1
2
3
|
dao.addIntProperty(
"cityId"
);
dao.addStringProperty(
"infoType"
).notNull();
//非null字段
dao.addDoubleProperty(
"Id"
);
|
1
|
java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
|
1
2
3
4
5
6
7
|
public
boolean
isSaved(
int
ID)
{
QueryBuilder<SaveList> qb = saveListDao.queryBuilder();
qb.where(Properties.Id.eq(ID));
qb.buildCount().count();
return
qb.buildCount().count() >
0
?
true
:
false
;
}
|
1
2
3
4
|
public
List<PhotoGalleryDB> getPhotoGallery()
{
return
photoGalleryDao.loadAll();
// 获取图片相册
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
/** 通过图片id查找其目录id */
public
int
getTypeId(
int
picId)
{
QueryBuilder<PhotoGalleryDB> qb = photoGalleryDao.queryBuilder();
qb.where(Properties.Id.eq(picId));
if
(qb.list().size() >
0
)
{
return
qb.list().get(
0
).getTypeId();
}
else
{
return
-
1
;
}
}
|
1
2
3
4
|
List joes = userDao.queryBuilder()
.where(Properties.FirstName.eq(
"Joe"
))
.orderAsc(Properties.LastName)
.list();
|
1
2
3
4
5
6
7
|
public
List<CityInfoDB> getSupportingList(
int
cityId)
{
QueryBuilder<CityInfoDB> qb = cityInfoDao.queryBuilder();
qb.where(qb.and(Properties.CityId.eq(cityId),Properties.InfoType.eq(HBContant.CITYINFO_SL)));
qb.orderAsc(Properties.Id);
// 排序依据
return
qb.list();
}
|
1
2
3
4
5
|
QueryBuilder qb = userDao.queryBuilder();
qb.where(Properties.FirstName.eq(
"Joe"
),
qb.or(Properties.YearOfBirth.gt(
1970
),
qb.and(Properties.YearOfBirth.eq(
1970
), Properties.MonthOfBirth.ge(
10
))));
List youngJoes = qb.list();
|
1
|
picJsonDao.loadByRowId(picId);
|
1
2
3
4
|
public
void
addToPhotoTable(Photo p)
{
photoDao.insert(p);
}
|
1
2
3
4
5
6
7
|
DevOpenHelper helper =
new
DaoMaster.DevOpenHelper(
this
,
"notes-db"
,
null
);
db = helper.getWritableDatabase();
daoMaster =
new
DaoMaster(db);
daoSession = daoMaster.newSession();
noteDao = daoSession.getNoteDao();
Note note =
new
Note(
null
, noteText, comment,
new
Date());
noteDao.insert(note);
|
1
2
|
photoDao.insertOrReplace(photo);
photoDao.insertInTx(photo);
|
1
2
3
4
5
|
/** 清空相册图片列表的数据 */
public
void
clearPhoto()
{
photoDao.deleteAll();
}
|
1
2
3
4
5
6
|
public
void
deleteCityInfo(
int
cityId)
{
QueryBuilder<DBCityInfo> qb = cityInfoDao.queryBuilder();
DeleteQuery<DBCityInfo> bd = qb.where(Properties.CityId.eq(cityId)).buildDelete();
bd.executeDeleteWithoutDetachingEntities();
}
|
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
|
private
static
DaoMaster daoMaster;
private
static
DaoSession daoSession;
/**
* 取得DaoMaster
*
* @param context
* @return
*/
public
static
DaoMaster getDaoMaster(Context context)
{
if
(daoMaster ==
null
)
{
OpenHelper helper =
new
DaoMaster.DevOpenHelper(context, HBContant.DATABASE_NAME,
null
);
daoMaster =
new
DaoMaster(helper.getWritableDatabase());
}
return
daoMaster;
}
/**
* 取得DaoSession
*
* @param context
* @return
*/
public
static
DaoSession getDaoSession(Context context)
{
if
(daoSession ==
null
)
{
if
(daoMaster ==
null
)
{
daoMaster = getDaoMaster(context);
}
daoSession = daoMaster.newSession();
}
return
daoSession;
}
|
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
|
public
class
DBHelper
{
private
static
Context mContext;
private
static
DBHelper instance;
private
CityInfoDBDao cityInfoDao;
private
DBHelper()
{
}
public
static
DBHelper getInstance(Context context)
{
if
(instance ==
null
)
{
instance =
new
DBHelper();
if
(mContext ==
null
)
{
mContext = context;
}
// 数据库对象
DaoSession daoSession = HBApplication.getDaoSession(mContext);
instance.cityInfoDao = daoSession.getCityInfoDBDao();
}
return
instance;
}
/** 添加数据 */
public
void
addToCityInfoTable(CityInfo item)
{
cityInfoDao.insert(item);
}
/** 查询 */
public
List<EstateLoveListJson> getCityInfoList()
{
QueryBuilder<CityInfo> qb = cityInfoDao.queryBuilder();
return
qb.list();
}
/** 查询 */
public
List<CityInfo> getCityInfo()
{
return
cityInfoDao.loadAll();
// 查找图片相册
}
/** 查询 */
public
boolean
isSaved(
int
Id)
{
QueryBuilder<CityInfo> qb = cityInfoDao.queryBuilder();
qb.where(Properties.Id.eq(Id));
qb.buildCount().count();
return
qb.buildCount().count() >
0
?
true
:
false
;
// 查找收藏表
}
/** 删除 */
public
void
deleteCityInfoList(
int
Id)
{
QueryBuilder<CityInfo> qb = cityInfoDao.queryBuilder();
DeleteQuery<CityInfo> bd = qb.where(Properties.Id.eq(Id)).buildDelete();
bd.executeDeleteWithoutDetachingEntities();
}
/** 删除 */
public
void
clearCityInfo()
{
cityInfoDao.deleteAll();
}
/** 通过城市id查找其类型id */
public
int
getTypeId(
int
cityId)
{
QueryBuilder<CityInfo> qb = cityInfoDao.queryBuilder();
qb.where(Properties.Id.eq(cityId));
if
(qb.list().size() >
0
)
{
return
qb.list().get(
0
).getTypeId();
}
else
{
return
0
;
}
}
/** 多重查询 */
public
List<CityInfo> getIphRegionList(
int
cityId)
{
QueryBuilder<CityInfoDB> qb = cityInfoDao.queryBuilder();
qb.where(qb.and(Properties.CityId.eq(cityId), Properties.InfoType.eq(HBContant.CITYINFO_IR)));
qb.orderAsc(Properties.Id);
// 排序依据
return
qb.list();
}
}
|